#!/bin/bash
###################################################
# fou4s - Fast Online Update for SuSE
# SuSE Security Announcement to Patch Description Converter
#
# Copyright (C) 2002 Markus Gaugusch <fou4s@gaugusch.at>
#
# SVN info:
# $Author: markus $
# $Date: 2005-02-09 17:18:35 +0100 (Wed, 09 Feb 2005) $
# $Rev: 164 $
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. 
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details. 
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##################################################

#include common functions from fou4s to get suse release, etc.
verbose=0
if [ "$1" = "-v" -o "$1" = "--verbose" ] ; then
	verbose=1
fi

if [ -x ./fou4s ] ; then
	. ./fou4s --commonfuncs
	FOUPATH=.
elif [ -x `dirname $0`/fou4s ] ; then # same dir as announcement2pkgdesc
	FOUPATH=`dirname $0`
	. `dirname $0`/fou4s --commonfuncs
elif [ -x `type -p fou4s` ] ; then # fou4s in path
	FOUPATH=/usr/lib/fou4s # from RPM installation
	. fou4s --commonfuncs
else
	echo "ERROR: Could not find fou4s!"
	exit 2
fi


TMPFILE=`mktemp /var/cache/fou4s/packagelist/fou4s_announcementXXXXXX`
if [ $? -ne 0 ] ; then
	echo "Error creating temp file - exiting!"
	exit 2
fi
cat > $TMPFILE # write stdin into temp file
#check signature and get build time from it
hasGoodSignature "`cat $TMPFILE | gpg --verify 2>&1`" RSA 3D25D3D9 "SuSE Security Team <security@suse.de>" 1
if [ $? -eq -1 ] ; then
	echo ERROR: Signature verification failed: 
	echo "$RETVAL"
	echo "Exiting ..."
	rm $TMPFILE
	exit 1
fi
BUILDTIME=$RETVAL

ANNID=`cat $TMPFILE | grep Announcement-ID`
ANNID=${ANNID##*SuSE-SA}
ANNID=`echo $ANNID | sed 's/://g'`
PKGDESCPATH="$PKGDESCPATH/server0/$SERVERPATH/patches"
FILES=`cat $TMPFILE | grep $ARCH\/update\/$SUSEVERSION | grep -v src\.rpm`
COUNT=0
LASTNAME=
if [ $verbose -ge 1 ] ; then
	echo Announcement-ID: $ANNID
	echo Buildtime: $BUILDTIME
	echo ============================
fi
for i in $FILES ; do
	getVersionFromFilename $i
	if [ $verbose -ge 1 ] ; then
		echo File: $i
		echo RPM: $RPM
		echo Release: $RELEASE
		echo Version: $VERSION
		echo PkgName: $PKGNAME
		echo Series: $SERIES
	fi
	if [ "$LASTNAME" = "$PKGNAME" ] ; then
		echo "Warning: Duplicate patch detected: $PKGNAME"
		if [[ "$i" == "*.patch.rpm" ]] ; then
			echo "Not using previous, because new is patch.rpm"
			COUNT=$((COUNT-1)) # overwrite old, because new is patch.rpm
		else
			echo "Skipping, because previous is a patch.rpm!"
			continue # skip this one
		fi
		echo ============================
	fi
	cat $FOUPATH/pkgdesc_template | sed "s#PKGSHORTNAME#$PKGNAME#g" | sed "s#VERSION_RELEASE#$VERSION-$RELEASE#g" | sed "s#SERIES#$SERIES#g" | sed "s#PKGFULLNAME#$i#g" | sed "s#RPMSHORTNAME#$PKGNAME#g" | sed "s#BUILDTIME#$BUILDTIME#g" | sed "s#ANNID#$ANNID#g" > $PKGDESCPATH/suse-announcement${ANNID}-${COUNT}-1
	if [ $verbose -ge 1 ] ; then
		echo Created $PKGDESCPATH/suse-announcement${ANNID}-${COUNT}-1 ....
		echo ============================
	fi
	LASTNAME=$PKGNAME  # name of last RPM
	COUNT=$((COUNT+1))
done
rm $TMPFILE
