#!/bin/sh

DIRPERM=755
FILEPERM=644
umask 022

# FROMWHAT: directory/floppy
# MOUNTPOINT: where are the *.tgz files when installing from floppy
# MOUNTCMD: how to mount
# SOURCEDIR: where are the *.tgz files when installing from disk

# TETEXDEST: where do the programms go
# BINDIR: where to create links for binaries
# MANDIR: where to create links for manpages
	
# BASE, GOODIES, SOURCE: install them?
# BASEPROMPT, GOODIESPROMPT: prompt mode?

TETEXDIR=/usr/local/tex
TEXMF=$TETEXDIR/texmf
BINDIR=/usr/local/bin
MANDIR=/usr/local/man

BASE="X"
BASEPROMPT=" "
GOODIES="X"
GOODIESPROMPT="X"
FROMFLOPPY="*"
FROMDIR=" "
FS=msdos
DEVICE=/dev/fd0
MOUNTPOINT=/mnt
MOUNTCMD="mount -t $FS $DEVICE $MOUNTPOINT"
floppy=true
SOURCEDIR=/src
TEXMF_WITH_LINK=false

help()
{
	o=$IFS
	IFS=''
	clear
	echo $help | less -M
	IFS=$o
}

gets()
{
	eval old=\$$1
	eval echo -n \"New value for $1 [\$$1]\: \"
	read $1
	eval test -z \"\$$1\" && eval $1=\'$old\'
}

toggle()
{
	eval old=\$$1
	if [ "$old" = "X" ]; then
		eval $1=\" \"
	else
		eval $1="X"
	fi
}

assert-dir()
{
    for i; do
	if test ! -d "$i"
	then
		echo "Fatal error. Not a directory $i. Installation aborted."
		exit
	fi
    done
}


assert-write()
{
    for i; do
	if test ! -w "$i"
	then
		echo "Fatal error. Cannot write to $i. Installation aborted."
		exit
	fi
    done
}

do-umount()
{
	# Parameters: the command to mount !!!
	eval umount \$$# >& /dev/null
}

testmnt()
{
	clear
	mp=$1
	mc=$2

	do-umount $mc
	if [ ! -d "$mp" ]; then
		echo -n "Trying to make $mp"...
		mkdir -p "$mp"
	fi
	if eval $mc; then
		echo "'$mc' succeeded".
		do-umount $mc
	else
		echo "'$mc' failed".
	fi
	echo -n "Press return to continue ..."
	read foo
}

getnum()
{
	max=$1; msg=$2
	ans=""
	while [ -z "$ans" ] || [ "$ans" -le 0 ] || [ ! "$ans" -le "$max" ]; do
		echo -n "$msg "
		read ans
		echo $ans | grep -v '^[0-9]*$' >&/dev/null && ans=""
	done
}

setup-source()
{
    while true; do
        if $floppy; then
		FROMWHAT=floppy
	else
		FROMWHAT=directory
	fi
	clear
	echo "Current 'media' setup":
	echo "========================================================================"
	echo "    source media:	$FROMWHAT"
	if $floppy; then
		echo "    mount point:	$MOUNTPOINT"
		echo "    floppy device:	$DEVICE"
		echo "    filesystem type:	$FS"
		echo "    command to mount:	$MOUNTCMD"
	else
		echo "    install from:	$SOURCEDIR"
		echo
	fi
	echo; echo
	echo "Available options:"
	if $floppy; then
		echo "    1) toggle source media (floppy/directory)"
		echo "    2) change mount point"
		echo "    3) change floppy device"
		echo "    4) change filesystem type"
		echo "    5) change command to mount"
		echo "    6) test to mount a floppy"
		echo "    7) done with source setup"
		echo "    8) exit program"
		echo
		getnum 8 "Enter option:"
		case $ans in
		  1)	floppy=false;;
		  2)	gets MOUNTPOINT; MOUNTCMD="mount -t $FS $DEVICE $MOUNTPOINT";;
		  3)	gets DEVICE; MOUNTCMD="mount -t $FS $DEVICE $MOUNTPOINT";;
		  4)	gets FS; MOUNTCMD="mount -t $FS $DEVICE $MOUNTPOINT";;
		  5)	gets MOUNTCMD
			set $MOUNTCMD
			eval dir_or_dev=\$$#
			if test -b "$dir_or_dev"; then
				DEVICE=$dir_or_dev
			else
				MOUNTPOINT=$dir_or_dev
			fi
			while [ ! -z "$1" ]; do
				test -b "$1" && DEVICE=$1
				shift
			done
			;;
		  6)	testmnt "$MOUNTPOINT" "$MOUNTCMD";;
		  7)	return;;
		  8)	exit;;
		esac
	else
		echo "    1) toggle source media (floppy/directory)"
		echo "    2) change source directory"
		echo "    3) done with source setup"
		echo "    4) exit program"
		echo
		getnum 4 "Enter option:"
		case $ans in
		  1)	floppy=true;;
		  2)	gets SOURCEDIR;;
		  3)	return;;
		  4)	exit;;
		esac
	fi
    done
}

setup-dest()
{
    while true; do
	clear
	echo "Current 'destination directories' setup":
	echo "========================================================================"
	echo "    TETEXDIR:		$TETEXDIR"
	echo "    TEXMF:		$TEXMF"
	echo "    links for binaries:	$BINDIR"
	echo "    links for manpages:	$MANDIR"

	echo; echo
	echo "Available options:"
	echo "    1) change TETEXDIR directory"
	echo "    2) change TEXMF directory"
	echo "    3) change directory for binary-links"
	echo "    4) change directory for manpage-links"
	echo "    5) done with 'destination directories' setup"
	echo "    6) exit program"
	echo
	getnum 7 "Enter option:"
	case $ans in
	  1) gets TETEXDIR
	     d=`dirname $TETEXDIR`
	     TEXMF=$TETEXDIR/texmf; BINDIR=$d/bin; MANDIR=$d/man;;
	  2) gets TEXMF;;
	  3) gets BINDIR;;
	  4) gets MANDIR;;
	  5) return;;
	  6) exit;;
	esac

    done
}

setup-series()
{
    while true; do
	clear
	echo "Current 'series' setup":
	echo "========================================================================"
	echo "   series             	(install)	(prompt-mode)"
	echo "   BASE			   [$BASE]		    [$BASEPROMPT]"
	echo "   GOODIES		   [$GOODIES]		    [$GOODIESPROMPT]"

	echo; echo
	echo "Available options:"
	echo "    1) toggle installation of series 'BASE'"
	echo "    2) toggle installation of series 'GOODIES'"
	echo "    3) toggle prompt mode for series 'BASE'"
	echo "    4) toggle prompt mode for series 'GOODIES'"
	echo "    5) done with 'series' setup"
	echo "    6) exit program"
	echo
	getnum 6 "Enter option:"
	case $ans in
	  1) toggle BASE;;
	  2) toggle GOODIES;;
	  3) toggle BASEPROMPT;;
	  4) toggle GOODIESPROMPT;;
	  5) return;;
	  6) exit;;
	esac

    done
}



screen()
{

help='==================== teTeX GENERAL HELP ======================

		====> MEDIA SECTION <====

teTeX can be installed from floppy disks or from a directory in your
filesystem. Enter "1" in the installation procedure to set up your
"sources". If you want to install from a mounted directory, you need to
have the subdirectories base1, base2, goodies1, goodies2, goodies3 that
contain the .tgz packages and the disk labels (e.g. BASE1, BASE2, ...).


		====> DESTINATION SECTION <====

On installing the teTeX distribution, you need to specify a TETEXDIR
directory. The various system dependent packages (e.g. tex, mf, dvipsk)
will be installed in

	$TETEXDIR/$package-$version

where "$package" stands for the corresponding package (e.g.  dvipsk,
xdvik) and $version for its version number. The system independent
packages (e.g. inputs for TeX, BibTeX, MetaFont and font metrics), will
be installed in

	$TETEXDIR/texmf

by default. If you specify a different location, make sure that the value
for TEXMF in $TETEXDIR/texmf.cnf points to the correct directory.

To avoid to specify each package in your PATH and MANPATH, the
installation procedure will create symbolic links for binaries and
manpages (e.g. in /usr/local/bin resp.  /usr/local/man). You can change
the default destination directories in section "2".


		====> SERIES SECTION <====

The teTeX distribution is separated into 3 series: BASE, GOODIES and
SOURCE. The BASE series contains the most basic packages. For example,
the tex/mf system, the most basic fonts, one previewer (xdvi) and one
program for printing (dvips). You propably should install the whole
base series. The GOODIES series contains packages that are useful, but
less not necessarily needed. For example, the XTeXShell, some more
dvi-drivers (e.g. dvilj to print on HP Laserjets), more fonts and
other nice things are on the GOODIES series.

After you enter "3" in the installation procedure, you are prompted
for each of the three packages if you want to install and in that case
whether you want to use prompt mode. When you install a series with
prompt mode, the installation procedure will ask for each package
in the series whether you want to install or skip this package. If
prompt mode is not activated for a series, all packages it contains
will be installed automatically.

Type "q" to exit the help screen ...
'

	clear;
	if $floppy; then
		FROMWHAT=floppy
	else
		FROMWHAT=directory
	fi
	echo "===================== teTeX 0.2 installation procedure ======================="
	echo
	echo "==> (1) media section"
	echo "   source media:	$FROMWHAT"
	if $floppy; then
		echo "   mount point:		$MOUNTPOINT"
		echo "   command to mount:	$MOUNTCMD"
	else
		echo "   install from:	$SOURCEDIR"
		echo
	fi

	echo
	echo "==> (2) destination directories"
	echo "   TETEXDIR:		$TETEXDIR"
	echo "   TEXMF:		$TEXMF"
	echo "   links for binaries:	$BINDIR"
	echo "   links for manpages:	$MANDIR"

	echo
	echo "==> (3) series section	(install)	(prompt-mode)"
	echo "   BASE			   [$BASE]		    [$BASEPROMPT]"
	echo "   GOODIES		   [$GOODIES]		    [$GOODIESPROMPT]"

	echo
	echo "==> (4) help    ==> (5) start installation    ==> (6) exit setup"
	echo
}

config-all()
{
	while true; do
		screen;
		getnum 6 "Enter option (1-6):"
		case "$ans" in
		  1)	setup-source;;
		  2)	setup-dest;;
		  3)	setup-series;;
		  4)	help;;
		  5)	return;;
		  6)	exit;;
		esac
	done
}


setup-destdirs()
{
	rm -f "$BINDIR" "$MANDIR" "$TETEXDIR" "$TEXMF" >& /dev/null
	mkdir -p "$BINDIR" "$MANDIR" "$TETEXDIR" "$TEXMF" >& /dev/null

	if [ "$TETEXDIR/texmf" != "$TEXMF" ]
	then
		rmdir "$TETEXDIR/texmf"
		rm -f "$TETEXDIR/texmf"
		ln -s "$TEXMF" "$TETEXDIR/texmf"
		TEXMF_WITH_LINK=true
	fi >& /dev/null

	assert-write "$BINDIR" "$MANDIR" "$TETEXDIR" "$TEXMF"
	assert-dir "$BINDIR" "$MANDIR" "$TETEXDIR" "$TEXMF"
	chmod $DIRPERM "$BINDIR" "$MANDIR" "$TETEXDIR" "$TEXMF"
	cd $TETEXDIR || exit

	touch "$TETEXDIR/texmf/linktest$$"
	rm -f "$TEXMF/linktest$$"
	if test -f "$TETEXDIR/texmf/linktest$$"; then
		rm -f "$TETEXDIR/texmf/linktest$$"
		echo "OOps! You have chosen \$TEXMF=$TEXMF,"
		echo "but \$TETEXDIR/texmf exists and if different."
		echo "Please remove the directory $TETEXDIR/texmf and start again"
		exit
	fi
}

prepare()
{
	if $floppy; then
		src_dir=$MOUNTPOINT
		while [ ! -f $src_dir/$current_series$number ]; do
			do-umount $MOUNTCMD 2>&-
			echo -n "Prepare for disk $current_series$number. Press enter when ready. "
			read wait
			eval $MOUNTCMD
		done
	else
		src_dir=$SOURCEDIR/$current_series$number
		if [ ! -f $src_dir/$current_series$number ]; then
			echo "Error: $src_dir/$current_series$number does not exist."
			return 1
		fi
	fi
	source $src_dir/$current_series$number
}

install-if()
{
	clear
	eval $1
	if test "$2" != "X"; then
		echo
		echo "Auto-installing package $package."
		return 0
	fi
	echo
	while true; do
		echo -n "Install package $package? (Y/N) "
		read ans
		case $ans in
		y|Y)	return 0;;
		n|N)	return 1;;
		esac
	done
	
}

install-one-series()
{
	current_series=$1; prompt="$2"
	number=0
	while true; do
	    number=`expr $number + 1`
	    prepare || exit
	    for i in $packages; do
		if install-if "$i-help" "$prompt"; then
			echo -n "Extracting ... "
			tar zxpvf $src_dir/$i >& /dev/null
			if [ $? != 0 ]; then
				echo
				echo "tar terminated with an error. This may cause some trouble later."
				echo -n "Press any key to continue... "
				read dummy
			else
				echo "Done."
			fi
		fi
	    done
	    test $last = true && return
	done
}

install-series()
{
	test "$BASE" = X && install-one-series base $BASEPROMPT
	test "$GOODIES" = X && install-one-series goodies $GOODIESPROMPT
}

setup-symlinks()
{
	echo -n "Creating symlinks in $BINDIR and $MANDIR. "
	test -d $MANDIR/man1 || mkdir $MANDIR/man1
	test -d $MANDIR/man5 || mkdir $MANDIR/man5
	ln -sf $TETEXDIR/*/bin/* $BINDIR
	ln -sf $TETEXDIR/*/man/man1/* $MANDIR/man1
	ln -sf $TETEXDIR/*/man/man5/* $MANDIR/man5
	rm -f $TETEXDIR/*/man/man?/\* $BINDIR/\*
	echo "Done."
	echo
}

setup-permissions()
{
	echo -n "Setting up permissions in $TETEXDIR. "
	find $TETEXDIR -type d -print | xargs chmod 755
	find $TETEXDIR -type d \( -name pk -o -name tfm \) -print | xargs chmod 1777
	if [ "`whoami`" = root ]; then
		chown -R root.root $TETEXDIR
		if $TEXMF_WITH_LINK; then
			chown -R root.root $TEXMF
		fi
	fi
	echo "Done."
	echo
}

setup-x11-bins()
{
	xdvi=`echo $TETEXDIR/*/bin/xdvi-X11R[56]`
	virmf=`echo $TETEXDIR/*/bin/virmf-X11R[56]`
	if [ `echo "$xdvi" | wc |awk '{print $2}'` -gt 1 ] ||
	   [ `echo "$virmf" | wc |awk '{print $2}'` -gt 1 ]; then
		echo "virmf/xdvi can use shared libraries from X11R5 (XFree86 up to 2.1.1)"
		echo "or from X11R6 (since XFree86-3.0)."
		ver=0
		while [ "$ver" != 5 ] && [ "$ver" != 6 ]; do
		  echo -n "Which version do you like to use? (5/6) "
		  read ver
		done
		other=`expr 11 - $ver`
		ans=x
		while [ "$ans" != y ] && [ "$ans" != Y ] && [ "$ans" != n ] && [ "$ans" != N ]; do
		  echo
		  echo -n "Can I remove the versions of xdvi/virmf for X11R$other? (Y/N) "
		  read ans
		done
		case "$ans" in
		y|Y)	rm -f $TETEXDIR/*/bin/xdvi-X11R$other $TETEXDIR/*/bin/virmf-X11R$other ;;
		esac
		if test -f $TETEXDIR/*/bin/xdvi-X11R$ver; then
			cd `dirname $TETEXDIR/*/bin/xdvi-X11R$ver`
			ln -sf xdvi-X11R$ver xdvi
		fi
		if test -f $TETEXDIR/*/bin/virmf-X11R$ver; then
			cd `dirname $TETEXDIR/*/bin/virmf-X11R$ver`
			ln -sf virmf-X11R$ver virmf
		fi
	fi
}

setup-xtexsh()
{
	test -x $TETEXDIR/XTeXShell*/bin/xtexsh || return
	if [ ! -x `sed -n '/^#!/s/#! *//;s/ .*//;q' $TETEXDIR/XTeXShell*/bin/xtexsh` ]; then
		wishx=`type -p wishx`
		test -x $wishx || return
		ed -s $TETEXDIR/XTeXShell*/bin/xtexsh > /dev/null <<-eof
			/#!
			c
			#!$wishx -f
			.
			wq
		eof
	fi
}

echo-lastmsg()
{
clear
echo -n "------------------------------------------------------------------------
Now, it's time to run texconfig to set up teTeX to suit to your pleasure
and to perform the last post-installation steps.

The program provides some online-help. For additional information, refer
to the teTeX manual (tetexdoc.dvi).
------------------------------------------------------------------------

Press return to continue ..."

read dummy
}

setup-formats()
{
	test -d $TETEXDIR/web2c-6.1/lib/formats || return
	cd $TETEXDIR/web2c-6.1/lib/formats
	echo
	echo -n "Running initex to create format files. "
	initex tex.ini < /dev/null >& /dev/null
	initex latex.ltx < /dev/null >& /dev/null
	test -f tex.fmt && ln -sf tex.fmt plain.fmt >& /dev/null
	echo "Done."
}

setup-bases()
{
	test -d $TETEXDIR/web2c-6.1/lib/bases || return
	cd $TETEXDIR/web2c-6.1/lib/bases
	echo
	echo -n "Running inimf to create base files. "
	inimf mf.ini < /dev/null >& /dev/null
	inimf cmmf.ini < /dev/null >& /dev/null
	test -f mf.base && ln -sf mf.base plain.base
	echo "Done."
}

setup-fix-texmf()
{
	test "$TEXMF_WITH_LINK" = true || return
	test -w "$TETEXDIR/texmf.cnf" || return
	ed -s "$TETEXDIR/texmf.cnf" <<-eof
		/TEXMF.*=/
		c
		TEXMF		= $TEXMF
		.
		wq
	eof
}

# MAIN program
config-all
PATH=${BINDIR}:${PATH}
export PATH
if test "$BASE $GOODIES"  = "   "; then
	echo "Funny :-) Nothing to install."
	exit
fi

clear
setup-destdirs 2>/dev/null
install-series 2>/dev/null
setup-fix-texmf >& /dev/null

clear
setup-permissions 2>/dev/null
setup-x11-bins
setup-xtexsh
setup-symlinks 2>/dev/null
test -x $BINDIR/texhash && eval $BINDIR/texhash
setup-formats
setup-bases
echo-lastmsg
texconfig
