#!/bin/sh
#
# $Id: spec-gen,v 1.28 2003/06/01 19:29:21 djrzulf Exp $
#
# Automated fortunes-pl.spec and tarball generator
# by Michal Kochanowicz <mkochano@pld.org.pl>
#
# To add new cookie file sinply add it to COOKIES 
# variable and rerun script.
#

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

README="../README"

COOKIES="adamg advocacy apcoh argante bajki baseciq blug bok cows cnp djurban dowcipy dowcipy-feministyczne dowcipy-niskopoziomowe dwimc dzieci gra_polslowek haiku http icpusers imiona_facetow ipv6.pl kloczkish komputery konikbujany koscielne lcamtuf linux linuxfr linuxpl matura microsoft milosc misc nauka nowe ospl ospl-ad pcol perl pingwinaria pld pld-offensive pldhelp-offensive plug polish_manpages pratchett puchatek pug pug-slc2001 sad sapkowski seneka sigpl sigvirus slogany spolem sport stachura radio_maryja rywingate wieza-pilot zen znaki_zodiaku"

SPEC_NAME="fortunes-pl.spec"

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

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"
	DESCRIPTION_PL="Zestaw fortunek: $f"
	DESCRIPTION_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
		DESCRIPTION_PL="`cat spec-gen-desc`"
		DESCRIPTION_EN="`cat spec-gen-desc`"
	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"
	echo "%description -n fortune-mod-pl-$NAME" >> "$SPEC_NAME"
	echo "$DESCRIPTION_EN" >> "$SPEC_NAME"
	echo >> "$SPEC_NAME"
	echo "%description -n fortune-mod-pl-$NAME -l pl" >> "$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-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
# TARBALL ##############################################################
