#!/bin/sh
#
# This script creates texlive.spec from generated specs by tl2rpm
# and generates local testing repository from built packages.
# Written by Jindrich Novy (novyjindrich@gmail.com).
#
gcc tl2rpm.c -o tl2rpm -O2 -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=native -march=native
[ "$?" != "0" ] && exit 1
rm -f _mkdirs.spec _unpack.spec _sources.spec _remove.spec _packages.spec _font.spec _files.spec _dirs.spec
touch _mkdirs.spec
./tl2rpm
mv _mkdirs.spec _mkdirs.spec.tmp
sort _mkdirs.spec.tmp > _mkdirs.spec
rm -f _mkdirs.spec.tmp
mv _dirs.spec _dirs.spec.tmp
sort _dirs.spec.tmp > _dirs.spec
rm -f _dirs.spec.tmp
mv _sources.spec _sources.spec.tmp
sort _sources.spec.tmp > _sources.spec
rm -f _sources.spec.tmp
while read LINE; do
       if [ "${LINE%% *}" = "%include" ]; then
               cat ${LINE:9}
       else
               echo "$LINE"
       fi
done < texlive.spec.template > texlive.spec
#exit 0
RPMBUILDDIR=`rpm --eval "%_topdir"`
rm -f $RPMBUILDDIR/RPMS/noarch/texlive*
rm -f $RPMBUILDDIR/RPMS/x86_64/texlive*
for i in `ls texlive/archive`; do
  rm -f $RPMBUILDDIR/SOURCES/$i
done
ACTDIR=`pwd`
for i in `ls texlive/archive`; do
  ln -s $ACTDIR/texlive/archive/$i $RPMBUILDDIR/SOURCES/$i
done
LANG=C time rpmbuild -bb texlive.spec > buildlog-srpm 2>&1
[ "$?" != "0" ] && exit 1
mv buildlog-srpm buildlog-srpm.full
rm -rf packages
mkdir packages
mv RPMS/noarch/texlive* packages
mv RPMS/x86_64/texlive* packages
createrepo packages
