#!/bin/sh
#
# $Id: spec-gen,v 1.41 2009/11/21 00:20:16 pawelz Exp $
#
# Automated fortunes-pl.spec and tarball generator
# originally by Michal Kochanowicz <mkochano@pld.org.pl>
#

VERSION="`date +%Y%m%d`"
RELEASE="1"

README="../README"

COOKIES="`find .. -maxdepth 1 -type f | egrep -v \(README\|cvsignore\|ChangeLog\|skrypt.awk\)|cut -b 4-`"

SPEC_NAME="fortunes-pl.spec"

[ -e "$SPEC_NAME" ] && rm "$SPEC_NAME"

wget -O- 'http://cvs.pld-linux.org/cgi-bin/cvsweb.cgi/packages/fortunes-pl/fortunes-pl.spec?rev=HEAD;content-type=text%2Fplain' | awk '/^%define date/ {p=1} {if (p==1) print}' > spec-template-footer

cat "spec-template-header" \
	| sed \
	-e "s/@@VERSION@@/$VERSION/g" \
	-e "s/@@RELEASE@@/$RELEASE/g" \
	> "$SPEC_NAME"

for f in $COOKIES; do
	NAME="$f"
	SUMMARY_PL="Zestaw fortunek: $f"
	SUMMARY_EN="Fortune mod: $f"

	grep "+ $f --" $README > /dev/null
	if [ $? -eq 0 ]
	then
		DONE=0
		LINES=10
		for c in `seq 0 10`; do
			if [ $DONE -eq 0 ]
			then
				grep "+ $f --" $README -A$c > spec-gen-desc
				LAST="`cat spec-gen-desc | tail -n 1`"
				echo "$LAST" | grep -Ev "[:alnum:]" > /dev/null
				if [ $? -eq 0 ]
				then
					DONE=1
					LINES=$c
				fi
			fi
		done
		grep "+ $f --" $README -A$LINES > spec-gen-desc
		DISABLE_EN_DESC=1
		DESCRIPTION_PL="`cut -b3- spec-gen-desc`"
		DESCRIPTION_EN="`cut -b3- spec-gen-desc`"
	else
		DISABLE_EN_DESC=0
		INITIAL_AUTHOR="`cd ..;cvs log $NAME|egrep ^revision\ 1.1\$ -A1|tail -n1|sed -e 's/.*author: \(.*\);  state.*/\1/'`"
		DESCRIPTION_PL="Zestaw fortunek: $f\n\nPrzykro nam, że nie ma bardziej dokładnego opisu,\nale $INITIAL_AUTHOR nie potrafi dodać opisu do pliku README.\nProśby prosimy kierować na adres $INITIAL_AUTHOR@pld-linux.org"
		DESCRIPTION_EN="Fortune mod: $f\n\nWe are sorry that there is no more useful description,\nbut $INITIAL_AUTHOR is too lame to add description to the README\nfile. Send your requests to $INITIAL_AUTHOR@pld-linux.org"
	fi

	cat "spec-template-subpackage-header" \
		| sed \
		-e "s/@@VERSION@@/$VERSION/g" \
		-e "s/@@RELEASE@@/$RELEASE/g" \
		-e "s/@@NAME@@/$NAME/g" \
		-e "s/@@SUMMARY_PL@@/$SUMMARY_PL/g" \
		-e "s/@@SUMMARY_EN@@/$SUMMARY_EN/g" \
		>> "$SPEC_NAME"
	
	echo >> "$SPEC_NAME"
	if [ $DISABLE_EN_DESC -eq 0 ]
	then
		echo "%description -n fortune-mod-pl-$NAME" >> "$SPEC_NAME"
		echo "$DESCRIPTION_EN" >> "$SPEC_NAME"
		echo >> "$SPEC_NAME"
	fi
	echo "%description -n fortune-mod-pl-$NAME -l pl.UTF-8" >> "$SPEC_NAME"
	echo "$DESCRIPTION_PL" >> "$SPEC_NAME"
	echo >> "$SPEC_NAME"
	
	cat "spec-template-files" \
		| sed \
		-e "s/@@NAME@@/$NAME/g" \
		>> "spec-generated-files"
done

#cat "spec-template-build" \
#	| sed \
#	-e "s/@@COOKIES@@/$COOKIES/g" \
#	>> $SPEC_NAME
cat "spec-template-build" >> $SPEC_NAME
cat "spec-generated-files" >> $SPEC_NAME
cat "spec-template-footer" >> $SPEC_NAME

rm -f "spec-generated-files"
rm -f "spec-gen-desc"

# TARBALL ##############################################################
mkdir fortunes-pl-$VERSION
cd ..
cp $COOKIES SPEC/fortunes-pl-$VERSION
cd SPEC
tar cf - fortunes-pl-$VERSION | bzip2 > "fortunes-pl-$VERSION.tar.bz2"
rm -rf fortunes-pl-$VERSION
MD5=$(md5sum fortunes-pl-$VERSION.tar.bz2 | cut -f1 -d\ )
sed -i s,@@SOURCEMD5@@,$MD5, $SPEC_NAME
# TARBALL ##############################################################
