#!/bin/sh

TEXSRC="/users/heinz/src/tex/TeX-3.141/TeX3.141"
MAKEDIR=$TEXSRC
TEXDIR=$MAKEDIR/tex
WEBDIR=$MAKEDIR/web
WEB2CDIR=$MAKEDIR/web2c
SISISIDIR="/users/heinz/src/tex/sisisi/SiTeX3.141"

echo "***       SiTeX 3.141 Installation program"
echo "***       ================================"
echo "***"
echo "***       Do not forget to retrieve the files *.tex and *.sty"
echo "***       from eiunix.tuwien.ac.at:/pub/sisisi/COMMON and"
echo "***       copy them into your TEXINPUTS path."
echo "***"
echo "***"
echo "***       Before the installation can continue, you have to"
echo "***       follow the guidelines from the TeX3.141 distribution"
echo "***       and set the pathnames in the several Makefile.in"
echo "***       for your site's needs."
echo "***       Do not run configure, it is started by this script."
echo "***"
echo "***       If you want to stop the installation, press 'q' now."
echo "***       Press <cr> to continue."

read x
if [ "$x" = "q" ]
then
   exit
fi


echo
echo "Starting installation of SiTeX3.141"
echo

cd $TEXDIR
echo "Changing to $TEXDIR"
if [ ! -f tex.web.orig ]
then
	echo "Saving tex.web in tex.web.orig"
	mv tex.web tex.web.orig
fi
if [ ! -f tex.ch.orig ]
then
	echo "Saving tex.ch in tex.ch.orig"
	mv tex.ch tex.ch.orig
fi
if [ ! -f Makefile.orig ]
then
	echo "Saving Makefile in Makefile.orig"
	mv Makefile Makefile.orig
fi
echo "Modifying Makefile for tex9.c"
sed -e 's/^objs=/objs=tex9.o /' -e 's/tex9\.o: tex8\.c/tex9.o: tex9.c/'\
                                             < Makefile.orig > Makefile

cd $SISISIDIR
echo "Changing to $SISISIDIR"
echo "Uncompressing sitex.web.Z to $TEXDIR/tex.web"
uncompress <sitex.web.Z >$TEXDIR/tex.web
echo "Copying tex.ch to $TEXDIR/tex.ch"
cp tex.ch $TEXDIR/tex.ch

cd $WEBDIR
echo "Changing to $WEBDIR"
if [ ! -f tangle.ch.orig ]
then
	echo "Saving tangle.ch in tangle.ch.orig"
	mv tangle.ch tangle.ch.orig
fi
echo "Modifying tangle.ch for bigger name space"
sed -e '/@!stack_size=50/i\
@!max_names=4000; {number of identifiers, strings, module names;\
  must be less than 10240}\
@y\
@!max_names=9000; {number of identifiers, strings, module names;\
  must be less than 10240}\
@z\
\
@x' < tangle.ch.orig > tangle.ch
sleep 5
touch tangleboot.p
sleep 5
touch tangleboot.c tangleboot.h

cd $WEB2CDIR
echo "Changing to $WEB2CDIR"
if [ ! -f web2c.c.orig ]
then
	echo "Saving web2c.c in web2c.c.orig"
	mv web2c.c web2c.c.orig
fi
if [ ! -f web2c.h.orig ]
then
	echo "Saving web2c.h in web2c.h.orig"
	mv web2c.h web2c.h.orig
fi
echo "Modifying web2c.[ch] for bigger tables"
sed -e 's/var_list\[200]/var_list\[500]/'\
    -e 's/field_list\[200]/field_list\[500]/' < web2c.c.orig > web2c.c
sed -e 's/var_list\[200]/var_list\[500]/'\
    -e 's/field_list\[200]/field_list\[500]/' < web2c.h.orig > web2c.h

cd $MAKEDIR
echo "Changing to $MAKEDIR"
echo "Running configure"
./configure

echo "***       Now that configure has finished, you might want"
echo "***       to inspect or change the Makefiles, as suggested"
echo "***       in TeX3.141's INSTALL file (Step 4)."
echo "***       Press 's' to suspend the installation and start"
echo "***       an interactive shell, <cr> to continue."

read x
if [ "$x" = "s" ]
then
	echo "Type 'exit' to quit the shell and continue with the installation."
	${SHELL:-/bin/sh}
fi

cd $MAKEDIR
echo "Changing to $MAKEDIR"
echo "Now starting: \`make TeX'"
make TeX

cd $TEXDIR
echo "Now starting error correction"
echo "Changing to $TEXDIR"
echo "Correcting coerce.h"
sed -e 's/(string80)//g' -e 's/(sperrvektor)//g' -e 's/(wort)//g'\
    -e 's/(dudt)//g' -e 's/(intarr)//g' -e 's/(varr)//g'\
    -e 's/(trennvektor)//g' -e 's/(hashelement)//g'\
    -e 's/(infotype)//g' -e 's/(tableentry)//g' < coerce.h > coerce.h.new
mv coerce.h.new coerce.h
echo "Correcting texd.h"
sed -e 's/typedef integer verdschlue/typedef unsigned long verdschlue/'\
       < texd.h > texd.h.new
mv texd.h.new texd.h
echo "Correcting tex6.c"
sed -e 's/) ]/] )/g' -e 's/) ]/] )/g' -e 's/\(hashfile.*,.*\)"%ld"/\1"%4ld"/g'\
    -e 's/integer vacc/long vacc/' -e 's/\(vacc = [0-9][0-9]*\)/\1L/'\
    -e 's/\(indacc = [0-9][0-9]*\)/\1L/' -e 's/\(kacc = [0-9][0-9]*\)/\1L/'\
    -e '/read.*(.*hashfile.*,.*htab.*\[.*i.*].*\[.*0.*].*).*;/i\
               integer help;'\
    -e 's/read.*(.*hashfile.*,.*htab.*\[.*i.*].*\[.*\([0-9]\).*].*).*;/\
               fscanf ( hashfile, "%d", \&help );\
               htab [i][\1] = (eightbits) help;/' < tex6.c > tex6.c.new
mv tex6.c.new tex6.c

cd $MAKEDIR
echo "Returning to $MAKEDIR"
echo "Continuing: \`make TeX'"
make TeX

cd $TEXDIR
if [ ! -x virtex ]
then
	echo "***       There seems to be something wrong; virtex doesn't exist."
	echo "***       Obviously, the installation has failed."
	exit 1
fi

echo "Creating the formatfiles in $TEXDIR"
echo
echo "If siplain.tex, silplain.tex, sihyphen.tex and def_umlaut.tex are not"
echo "in one of the TEXINPUTS directories, you will now run into trouble."
echo "(You will be prompted for their pathnames and have to type in"
echo "the whole stuff.)"

mv tex.pool sitex.pool
TEXPOOL=. ./initex siplain.tex \\dump
if [ -f siplain.fmt ]
then
	mv siplain.fmt sitex.fmt
	echo "...sitex.fmt created"
else
	echo "siplain.fmt not created for unknown reasons."
fi
TEXPOOL=. ./initex silplain.tex \\dump
if [ -f silplain.fmt ]
then
	mv silplain.fmt silatex.fmt
	echo "...silatex.fmt created"
else
	echo "silplain.fmt not created for unknown reasons."
fi

echo "***       The installation is now complete."
echo "***       You should find the following files in $TEXDIR:"
echo "***"
echo "***           sitex.fmt, silatex.fmt, hf3,"
echo "***           sitex.pool, virtex and initex."
echo "***"
echo "***       Now copy sitex.fmt and silatex.fmt into a TEXFORMAT-Dir,"
echo "***       virtex and initex into a directory holding executables"
echo "***       (preferably as sivirtex and siinitex), hf3 into"
echo "***       a TEXINPUTDIR, and sitex.pool into the TEXPOOLDIR."
echo "***       Also create links silatex and sitex to sivirtex, and"
echo "***       a link named siplain.fmt to sitex.fmt, all in the"
echo "***       appropriate directories."

exit 0