#!/bin/bash -x
#generator  *.spec file pro RPM .
#Czech version
#Jiri Kouba 14-10-2000
SPECEDITOR='kedit'
echo $0
echo $*

if [ "$*" = "" ] ; then
echo 'Vloz titulek programu'
read title
echo 'Vloz jmeno spec souboru bez pripony'
read specfile
echo 'Vloz jmeno seznamu souboru'
read filelist

if [ ! -f $filelist ] ; then
echo 'Seznam souboru neexistuje'
exit 20
fi

echo 'Vloz jmeno programu'
read name
echo 'Vloz verzi programu'
read version
echo 'Vloz release programu'
read release
echo 'Vloz skupinu - jednu z nasledujicich
echo 'Vloz jmena konfiguracnich souboru'
read config

###listofgroups		
1)Amusements/Games 
2)Amusements/Graphics 
3)Applications/Archiving 
4)Applications/Communications 
5)Applications/Databases 
6)Applications/Editors 
7)Applications/Emulators 
8)Applications/Engineering 
9)Applications/File 
10)Applications/Internet 
11)Applications/Multimedia 
12)Applications/Productivity 
13)Applications/Publishing 
14)Applications/System 
15)Applications/Text 
16)Development/Debuggers 
17)Development/Languages 
18)Development/Libraries 
19)Development/System 
20)Development/Tools 
21)Documentation 
22)System Environment/Base 
23)System Environment/Daemons 
24)System Environment/Kernel 
25)System Environment/Libraries 
26)System Environment/Shells 
27)User Interface/Desktops 
28)User Interface/X 
29)User Interface/X Hardware Support 
'

read skupina

startlist=`grep -n '###listofgroups' <$0 |head -1 |cut -d":" -f1`
echo $startlist


sed -n $startlist,\$p <$0 |grep $skupina |cut -d")" -f2  >skupina-$$
group=`cat skupina-$$`
echo $group
echo 'Vloz jmeno zdrojoveho souboru'
read source
echo 'Vloz jmeno a E-mail toho, kdo balicek pripravuje'
read packager
echo 'Vloz podrobny popis'
read description
echo 'S jakymi pravy se ma instalovat ? Ciselne (755) '
read permis
echo 'Kdo ma byt vlastnik instalovanych souboru ?'
read owner
echo 'A skupina ?'
read groupf
if [ "${specfile}.spec" ] ; then
mv ${specfile}.spec ${specfile}.spec.1 
fi
else

title=$1
name=$2
specfile=$3
version=$4
release=$5
group=$6
packager=$7
source=$8
filelist=$9
shift
shift
shift
shift
descript=$6
permis=$7
owner=$8
groupf=$9
fi

echo  "Summary: " "$title" >${specfile}.spec
echo  "Name: " $name >>${specfile}.spec
echo  "Version: " $version >>${specfile}.spec
echo "Release: " "$release" >>${specfile}.spec
echo 'Copyright: GPL' >>${specfile}.spec
echo "Group: " "$group" >>${specfile}.spec
echo "Source: " "$source" >>${specfile}.spec
echo "Packager: " "$packager" >>${specfile}.spec
echo "BuildRoot: " "/var/tmp/%{name}-buildroot" >>${specfile}.spec
echo  "%description" >>${specfile}.spec
echo -n >>${specfile}.spec
echo "" "$descript" >>${specfile}.spec
echo -n >>${specfile}.spec
kedit ${specfile}.spec

echo '%prep
%setup -q
./configure
%build
make RPM_OPT_FLAGS="$RPM_OPT_FLAGS"


%install
make install' >>${specfile}.spec
#exit
#cd /usr/src/redhat/BUILD/
#tar zxvf /usr/src/redhat/BUILD/$source
#cd /usr/src/redhat/SPECS
#BUILD="/usr/src/redhat/BUILD/$name-$version"
BUILD="/usr/src/redhat/BUILD/"
echo $BUILD


for file in `cat $filelist `
do
basefile=`basename $file`
dirfile=`dirname $file`

echo 'mkdir -p $RPM_BUILD_ROOT'$dirfile >>${specfile}aux1-$$.spec
buildsource=`find $BUILD -name $basefile -type f -print `

if [ `echo $buildsource | wc -l ` != "1" ] ; then
echo 'Seznam souboru obsahuje duplicitni nazvy nebo naopak soubory, ktere se
nezkompiluji.'
exit 2
fi

buildsourceshorten=`echo $buildsource | sed -n 1,'$'s/^.*BUILD.*$version.//p`

echo $buildsourceshorten
echo 'install -m 644 ' $buildsourceshorten   '$RPM_BUILD_ROOT'$file  >>${specfile}aux2-$$.spec

done
cat ${specfile}aux1-$$.spec |sort |uniq >>${specfile}.spec
cat ${specfile}aux2-$$.spec >>${specfile}.spec


echo '
%clean
rm -rf $RPM_BUILD_ROOT

' >>${specfile}.spec
 
echo '
%files
' >>${specfile}.spec

echo '
%defattr('"$permis","$owner","$groupf"')
%doc README TODO COPYING ChangeLog
%config '"$config"'
' >>${specfile}.spec
cat $filelist >>${specfile}.spec

echo '
%changelog
* Sat Oct 07 2000 Jiri Kouba <jiri.kouba@volny.cz> 
- new build 
' >>${specfile}.spec

echo 'Mam smazat nepotrebne pomocne  soubory a spustit kompilaci a/n ?'
read Q
if [ "$Q" = "a" ] ; then
$SPECEDITOR ${specfile}.spec
rm ${specfile}aux*-$$.spec skupina-$$
rpm -ba ${specfile}.spec
fi
exit



