#
# This is a strictly POSIX 1003.2 shell (Bourne shell) script 
# sketching the addition of LaTeX to the TeX Kernel system: kerTeX.
#
# No shebang, since there is a bootstrapping problem: this script has
# to run on whatever host the kerTeX system runs on.
# It has to be invoked with whatever Bourne shell like interpreter is
# present on the host.
#
# C) 2010-2012 Thierry Laronde <tlaronde@polynum.com>
# All rights reserved and absolutely no warranty! Use at your own 
# risks.
#

# Needed post action (build, apply, remove) routines.
#
pkg_post_build()
{
	return 0 # bash errors on empty function...
}

pkg_post_apply()
{
	pkg_log 
	pkg_log "You can check the installation by processing base/ltxcheck."
	pkg_log "(Note: EC fonts are not installed.)"
	pkg_log 
	pkg_log "You can generate a sample by processing sample2e."
}

pkg_post_remove()
{
	return 0 # bash errors on empty function...
}

#==================== AUTOMATIC PROCESSING
# First include the pecularities of the TeX kernel system host.
#
. which_kertex

# Then we now how to find the library that defines routines and does
# some checks, argument processing and initializations. See the file
# directly for explanations.
#
. $KERTEX_BINDIR/lib/pkglib.sh

#==================== CUSTOM PROCESSING: we are in TMPDIR
#
# Note: we download directly the TFM. We could generate the fonts with
# the .mf instead.
#

pkg_get

# Unpack the base (that creates a base/, so we move files afterwards).
# The base and doc unpacks in a dedicated subdirectory and this is OK.
#
cd $TMPDIR/lib/$PKG_NAME
$PKG_UNZIP base.zip
rm base.zip
$PKG_UNZIP doc.zip
rm doc.zip

KERTEXFONTS="$TMPDIR/lib/fonts/latex;KERTEXSYS"
export KERTEXFONTS

#===== TeX version
#
# The full instructions to install are in base::manifest.txt.
#
# We start by creating files from the "packed" version distributed.
#
cd $TMPDIR/lib/$PKG_NAME/base # where are the base files
echo '\input unpack.ins' | $KERTEX_BINDIR/initex >$TMPDIR/.log 2>&1

# Several files are created. You should give a look specially to the
# .cfg files. For us, the latex.ltx is the file to dump (to create the
# format):
#

#===== Edit config for ltxcheck.
#
cd $TMPDIR/lib/$PKG_NAME/base
echo '\let\@currdir\@empty' >>texsys.cfg

#===== TeX version
#
cd $TMPDIR/lib/$PKG_NAME/base
cat <<EOT | $KERTEX_BINDIR/initex
\input latex.ltx
\dump
EOT

# Put the dump where TeX (kerTeX) looks for.
#
mkdir $TMPDIR/bin/lib/
mv latex.fmt $TMPDIR/bin/lib/$PKG_NAME.fmt

# And name virtex $PKG_NAME so that he loads $PKG_NAME.fmt.
#
cp $KERTEX_BINDIR/virtex $TMPDIR/bin/$PKG_NAME

#===== e-TeX version
#
cd $TMPDIR/lib/$PKG_NAME/base # where the base files are
cat <<EOT | $KERTEX_BINDIR/einitex
*\input latex.ltx
\dump
EOT

mv latex.dgst $TMPDIR/bin/lib/$PKG_NAME.dgst

cp $KERTEX_BINDIR/evirtex $TMPDIR/bin/e$PKG_NAME

#===== Place the used and generated files at the root.
cd $TMPDIR/lib/$PKG_NAME/base # where the base files are
#grep -l "generated with the docstrip" *\
#  |sed 's/^[^.]*//'|sort|uniq
#
mv *.tex *.cfg *.clo *.cls *.def *.dfu *.fd *.ltx *.sty ..

#===== CUSTOM PROCESSING FINISHED
#
# Time to do whether the build or the install.
#
pkg_do_action

# not reached
exit 0

# Since we have exited above, no need to comment out the CID.

BEGIN_CID
NAME: latex
VERSION: 2e
KERTEX_VERSION: 0.999.1.5
SOURCES:
ftp://anonymous:kertex@ftp.dante.de/
	LCD HOME/
	GET /tex-archive/macros/latex/base.zip
	GET /tex-archive/macros/latex/doc.zip
	LCD /lib/fonts/latex/tfm
	GET fonts/tfm/	/\.tfm$/
END:
END_CID

