#!/bin/sh
#
# This script checks out upstream archives from CTAN, removes unneeded files,
# expands files required for dependency resolution by tl2rpm.
# Written by Jindrich Novy (novyjindrich@gmail.com).
#
echo "Syncing with remote server..."
[ -e texlive/archive/asana-math.tar.xz ] && mv texlive/archive/asana-math.tar.xz texlive/archive/Asana-Math.tar.xz
[ -e texlive/archive/asana-math.doc.tar.xz ] && mv texlive/archive/asana-math.doc.tar.xz texlive/archive/Asana-Math.doc.tar.xz
[ -e texlive/archive/lineara.tar.xz ] && mv texlive/archive/lineara.tar.xz texlive/archive/linearA.tar.xz
[ -e texlive/archive/lineara.doc.tar.xz ] && mv texlive/archive/lineara.doc.tar.xz texlive/archive/linearA.doc.tar.xz
[ -e texlive/archive/lineara.source.tar.xz ] && mv texlive/archive/lineara.source.tar.xz texlive/archive/linearA.source.tar.xz
[ -e texlive/archive/musixtex-fnts.tar.xz ] && mv texlive/archive/musixtex-fnts.tar.xz texlive/archive/musixtex-fonts.tar.xz
[ -e texlive/archive/musixtex-fnts.doc.tar.xz ] && mv texlive/archive/musixtex-fnts.doc.tar.xz texlive/archive/musixtex-fonts.doc.tar.xz
#rsync -av --delete ftp.cstug.cz::pub/tex/local/tlpretest/archive texlive --delete-excluded \
#rsync -av --delete rsync://ftp.ctex.org/mirrors/texlive/tlpretest/ texlive --delete-excluded \
#rsync -av --delete ftp.cstug.cz::pub/tex/local/tlpretest/archive texlive --delete-excluded \
#rsync -av --delete rsync://ftp.fernuni-hagen.de/ctan/systems/texlive/tlnet/archive texlive --delete-excluded \
rsync -av --delete rsync://rsync.dante.ctan.org/CTAN/systems/texlive/tlnet/archive texlive --delete-excluded \
--exclude '*mactex*' \
--exclude '*.win32*' \
--exclude '*.i386-freebsd*' \
--exclude '*.i386-cygwin*' \
--exclude '*.i386-solaris*' \
--exclude '*.i386-netbsd*' \
--exclude '*.i386-kfreebsd*' \
--exclude '*.armel-linux*' \
--exclude '*.mipsel-linux*' \
--exclude '*.mips-irix*' \
--exclude '*.powerpc-*' \
--exclude '*.sparc-*' \
--exclude '*.amd64-*' \
--exclude '*.alpha-*' \
--exclude '*.x86_64-*' \
--exclude '*.universal-darwin*' \
--exclude '*.armhf-linux*' \
--exclude '*.exe.*' \
--exclude '*.exe' \
--exclude 'install-tl*' \
--exclude 'breakcites*' \
--exclude 'psutils*' \
--exclude 'rst.*' \
--exclude 't1utils*' \
--exclude 'xindy*' \
--exclude 'asymptote*' \
--exclude 'tlpkg/*' \
--exclude 'update-tlmgr*' | tee /tmp/rsynclogCTAN
EXIT=$?
[ -e texlive/archive/Asana-Math.tar.xz ] && mv texlive/archive/Asana-Math.tar.xz texlive/archive/asana-math.tar.xz
[ -e texlive/archive/Asana-Math.doc.tar.xz ] && mv texlive/archive/Asana-Math.doc.tar.xz texlive/archive/asana-math.doc.tar.xz
[ -e texlive/archive/linearA.tar.xz ] && mv texlive/archive/linearA.tar.xz texlive/archive/lineara.tar.xz
[ -e texlive/archive/linearA.doc.tar.xz ] && mv texlive/archive/linearA.doc.tar.xz texlive/archive/lineara.doc.tar.xz
[ -e texlive/archive/linearA.source.tar.xz ] && mv texlive/archive/linearA.source.tar.xz texlive/archive/lineara.source.tar.xz
[ -e texlive/archive/musixtex-fonts.tar.xz ] && mv texlive/archive/musixtex-fonts.tar.xz texlive/archive/musixtex-fnts.tar.xz
[ -e texlive/archive/musixtex-fonts.doc.tar.xz ] && mv texlive/archive/musixtex-fonts.doc.tar.xz texlive/archive/musixtex-fnts.doc.tar.xz
UPDATES=`grep 'tar.xz$' /tmp/rsynclogCTAN`
if [ "$UPDATES" == "" ]; then
  rm -f /tmp/rsynclogCTAN
  exit 0
else
  UPDATES=`grep 'tar.xz$' /tmp/rsynclogCTAN | grep -v 'deleting'`
  for i in $UPDATES; do
    echo "+ $i"
  done
fi
rm -f /tmp/rsynclogCTAN
[ $EXIT != 0 ] && exit 1

echo "Creating symlinks..."
for i in `ls texlive/archive`; do
  rm -f ~/rpmbuild/SOURCES/$i
done
RPMBUILDDIR=`rpm --eval "%_topdir"`
ACTDIR=`pwd`
for i in `ls texlive/archive`; do
  ln -s $ACTDIR/texlive/archive/$i $RPMBUILDDIR/SOURCES/$i
done

echo "Expanding..."
rm -rf texlive.expanded.old
if [ -d texlive.expanded ]; then
  mv texlive.expanded texlive.expanded.old
else
  mkdir -p texlive.expanded.old
fi
mkdir texlive.expanded
for i in texlive/archive/*.tar.xz; do
  tar xf $i -C texlive.expanded '*.sty' '*.tex' '*.cls' '*.ldf' '*.bbx' '*.cbx' '*.def' '*.tlpobj' '*.clo' '*.bug' '*.bg2' '*i386-linux*' '*texmf/scripts*' '*texmf-dist/scripts*' > /dev/null 2>&1
done
diff -ru texlive.expanded.old texlive.expanded > archive.diff

echo "Moving..."
pushd texlive.expanded
cp -pr doc source tex texmf-dist
rm -rf doc source tex
popd

echo "Generating tlpdb..."
pushd ./texlive.expanded/tlpkg/tlpobj
OUT=../../../texlive.tlpdb
mv $OUT $OUT.bck
for i in `ls *.tlpobj | grep -v '\.i386-linux.tlpobj$' | grep -v '\.doc.tlpobj$' | grep -v '\.source.tlpobj$'`; do
  NAME=`echo $i | sed 's/.tlpobj$//g'`
  grep -v '^catalogue-' $i >> $OUT
  if [ -e $NAME.doc.tlpobj ]; then
    grep '^docfiles' $NAME.doc.tlpobj >> $OUT
    grep '^ ' $NAME.doc.tlpobj >> $OUT
  fi
  if [ -e $NAME.source.tlpobj ]; then
    grep '^srcfiles' $NAME.source.tlpobj >> $OUT
    grep '^ ' $NAME.source.tlpobj >> $OUT
  fi
  grep '^catalogue-' $i >> $OUT
  echo >> $OUT
  if [ -e $NAME.i386-linux.tlpobj ]; then
    cat $NAME.i386-linux.tlpobj >> $OUT
    echo >> $OUT
  fi
done
popd
patch texlive.tlpdb < tlpdb.patch
[ "$?" = "1" ] && exit 1
diff -u texlive.tlpdb.orig texlive.tlpdb > tlpdb.patch
