#!/bin/sh -ex
# 2013 Marco B Caminati
# texlive script to package tcz extensions from texlive-xxx.iso
# I arbitrarily made a tradeoff between completeness and size in deciding what to include in the profile.
# An alternative approach would be to install only collection-basic, and then using tlmgr to install individual packages rather than collections.
# This script is dangerous; due to lack of flexibility of the whole texlive directory structure, it stages the install process into real hfs spot, then moves it into tolzm directory. Be sure to know what you're doing upon using it.
# Note that symlinking the installation directory into another location poses some problem, don't know how much substantial, because some files (eg .map .fmt and fonconfig.conf) generated inside texmf-var contain the real path to themselves.
# Usage example: 
# sudo ./texlive.tcebuild /PATH/texlive2012-20120701.iso
# cat /PATH/texlive2012-20120701.iso.md5 | ./texlive.tceinfo texlive.tcz

b=busybox; [ $mtp ] || mtp=/mnt/iso; [ $tmp ] || tmp=/tmp/new
[ $installdir ] || installdir=/usr/local; modulename=texlive
scmname="texlive-fonts"
scmname2="texlive-recommended"
reservedir="texlive"
texmfroot="${reservedir}/2012"
prefixdir=${tmp}/tolzm
_installdir=${prefixdir}/${installdir}
_texmfroot=${_installdir}/${texmfroot}
mkdir -p ${_installdir}/bin
tc="$(cat /etc/sysconfig/tcuser)"

mkdir "${mtp}"
$b mount -o ro "${1}" "${mtp}"
sudo -u "${tc}" tce-load -i perl5 binutils wget squashfs-tools-4.x file || true

( cat <<'EOF'
selected_scheme scheme-custom
TEXDIR /usr/local/texlive/2012
TEXMFCONFIG $TEXMFSYSCONFIG
TEXMFHOME $TEXMFLOCAL
TEXMFLOCAL /usr/local/texlive/2012/texmf-local
TEXMFSYSCONFIG /usr/local/texlive/2012/texmf-config
TEXMFSYSVAR /usr/local/texlive/2012/texmf-var
TEXMFVAR $TEXMFSYSVAR
binary_i386-linux 1
collection-basic 1
collection-latex 1
collection-latexrecommended 0
collection-latexextra 0
# The real package waster
collection-fontsrecommended 0
# The real byte waster
collection-mathextra 0
# depend collection-fontsrecommended
collection-metapost 0
collection-fontutils 0
in_place 0
option_adjustrepo 0
option_autobackup 0
option_backupdir tlpkg/backups
option_desktop_integration 
option_doc 0
option_fmt 1
option_letter 0
option_post_code 1
option_src 0
option_sys_bin /usr/local/bin
# option_sys_info /usr/local/info
# option_sys_man /usr/local/man
option_w32_multi_user 0
option_write18_restricted 1
portable 1
# option_path 0
option_file_assocs 0
# only significant for windows
EOF
)| sed "s_/usr/local_${installdir}_g" | ${mtp}/install-tl -gui=text -profile=/dev/stdin -v -v

# The real problem for separate packages are .map font map files

${installdir}/${texmfroot}/bin/i386-linux/tlmgr remove --force collection-documentation-base
${installdir}/${texmfroot}/bin/i386-linux/tlmgr option repository http://mirror.ctan.org/systems/texlive/tlnet
${installdir}/${texmfroot}/bin/i386-linux/tlmgr conf texmf texmf.cnf "${installdir}/${texmfroot}/texmf.cnf"
cp -a "${installdir}/${reservedir}" "${_installdir}/"
# mv or cp; the latter for diagnosing
umount "${mtp}"
rmdir "${mtp}"
(cd ${_texmfroot} && rm -r readme-* tlpkg/translations && rm install-tl* index.html README*) || exit 37
# tlpkg/tlpobj 

cat <<'EOF' | busybox patch -p0 "${_texmfroot}/texmf/scripts/texlive/tlmgr.pl" || true
--- tlmgr.pl	2010-07-13 03:01:27.000000000 +0000
+++ tlmgr.pl.patch	2011-02-17 10:00:50.000000000 +0000
@@ -62,6 +62,12 @@
     # if not in bootstrapping mode => kpsewhich exists, so use it to get $Master
     chomp($Master = `kpsewhich -var-value=SELFAUTOPARENT`);
   }
+  
+  $Master = "/usr/local/texlive/2012";
+  $bindir = $Master;
+  $ENV{"PATH"} = "$bindir:$ENV{PATH}";
+  # Dirtiest hack for tinycorelinux
+  
   $::installerdir = $Master;
 
   #
EOF

(
cd ${_texmfroot}/texmf/web2c/
sed -i -e "/^ *TEXMFLOCAL *=/ d" "../../texmf.cnf"
sed -i -e "/^ *TEXMFHOME *=/ d" ../../texmf.cnf
sed -i -e "/^ *TEXMFVAR *=/ d" ../../texmf.cnf
# sed -i -e "/^ *TEXMFCONFIG *=/ d" ../../texmf.cnf
#sed -i -e "/^ *%/ !d" ../../texmf.cnf
#sed -i -e "s_(^ *TEXMFLOCAL *=.*)( *$)_\1:/apps/${scmname}/texmf-local:/apps/${scmname2}/texmf-local_g" "../../texmf.cnf"
# echo "$(egrep "^ *TEXMFLOCAL *=" "texmf.cnf"):/apps/${scmname}/texmf-local:/apps/${scmname2}/texmf-local" >> ../../texmf.cnf
# Support separation into scms via TEXMFLOCAL
# Note that we leave TEXMFVAR alone, because it must point to ONE SINGLE path (no :,;{}). 
# echo "$(egrep "^ *TEXMFCONFIG *=" "texmf.cnf"):/apps/${scmname}/texmf-local:/apps/${scmname2}/texmf-local" >> ../../texmf.cnf
echo "TEXMFROOT = ${installdir}/${texmfroot}" >> ../../texmf.cnf
echo "TEXMFLOCAL = \$TEXMFROOT/texmf-local:/apps/${scmname}/texmf-local:/apps/${scmname2}/texmf-local" >> ../../texmf.cnf
echo "TEXMFHOME = ~/texmf:/apps/${scmname}/texmf-var" >> ../../texmf.cnf
echo "CLUAINPUTS = .;${installdir}/lib/{\$progname,\$engine,}/lua//" >> ../../texmf.cnf
echo TEXMFDBS = >> ../../texmf.cnf
egrep "^ *TEXMF *=.*!!" texmf.cnf | sed "s/!!//g" >> ../../texmf.cnf
sed -i -e "/^ *%/ !s/!!//g" "texmf.cnf"
#Disable ls-R everywhere
cd "${_installdir}/bin"
ln -s ../${texmfroot}/bin/i386-linux/* .
) || exit 39


find "${_texmfroot}" -name "ls-R" | xargs rm
# if a ls-R is wrong (as after separation below), troubles arise, so...
# moreover, file lookup slowdown motivating ls-R is mitigated by squashfs 
# (which is faster than a standard filesystem: e.g., compare ~ $ time find)
# see also http://kerneltrap.org/node/4410#comment-15387
# find "${storedir}/${texmfroot}/texmf-var/" -type f -iname "*.log" |xargs -n 1 rm 

find ${prefixdir} -type d | xargs -n 1 chmod -v 755

#rm -rf `find ${storedir} -type d -iname source -or -iname doc`

mksquashfs ${prefixdir} ${modulename}.tcz
# md5sum "${1}"

