#!/bin/bash
#
# This script imports the teTeX system from the same
# server from which /usr/share/texmf is mounted (nfs).
# This script should work with standard bourne shell.
#
# Copyright (c) 1998,2000 SuSE GmbH Nuernberg, Germany.
# please send bugfixes or comments to feedback@suse.de.
#
# Author: Werner Fink
#

export LC_CTYPE=POSIX

test `whoami` = root || { echo "Permission denied" 1>&2 ; exit 1; }

#
# The teTeX system, main date, variable data, fonts cache,
# and configuration data.
#
texroot=/usr/share/texmf
texvars=/var/lib/texmf
texfont=/var/cache/fonts
texconf=/etc/texmf

#
# Mount options and Internal Field Separator(s)
#
mntopt="exec,nosuid,nodev,hard,bg,intr,rsize=8192,wsize=8192"
action="import"  # otherwise "remove"
OLDIFS="$IFS"
NL='
'

#
# Remember the options given to us
#
we="${0##*/}"
tab="`echo ${we}:| tr '[:print:]' ' '`"
argv="$@"
args=$#

#
# Place holder for the nfs server(s) of the teTeX system
#
server=""
rootserv=""
varsserv=""
fontserv=""
confserv=""
etcfstab=""
num=0

#
# The current system: architecture, operating system, libc
#
  arch="`uname -m| sed 's%i.86%i586%g'`"
system="`uname -s| tr '[:upper:]' '[:lower:]'`"
set -- /lib/libc.so.[0-9]
eval libc=\${${#}##*.}
if test $libc -ge 6 ; then
    libc=libc$libc
else
    libc=libc5
fi
if test $system = linux ; then
    binary=$arch-$system-$libc
else
    binary=$arch-$system
fi

#
# Test the teTeX system: Something is already mounted?
#
test_system ()
{
    IFS="$NL"
    set -- `df -P -t nfs`
    IFS="$OLDIFS"
    shift

    for line ; do
	case "$line" in
	    *:${texroot}*) rootserv=${line%%:*} ;;
	    *:${texvars}*) varsserv=${line%%:*} ;;
	    *:${texfont}*) fontserv=${line%%:*} ;;
	    *:${texconf}*) confserv=${line%%:*} ;;
	    *)  ;;
	esac
    done
}

#
# At the first time we may need a mount point,
# just do it.
#
mount_dir ()
{
    origin=$1
    mpoint=$2
    rwro=$3

    test -d $mpoint || mkdir -p $mpoint
    set -- `mount -v -t nfs -o ${rwro},$mntopt $origin $mpoint`
    set -- $1 $3 $5 ${6#"("}
    if test -n "$1" ; then
	etcfstab="${etcfstab}`echo $1 $2 $3 ${4%')'} 0 0`\n"
	num=`expr $num + 1`
    fi
}

#
# Try to mount the system from the server
#
mount_system ()
{
    test_system

    IFS="$NL"
    set -- `showmount -e $server`
    IFS="$OLDIFS"

    for line ; do
    fs=${server}:${line%%\ *}
	case "$line" in
	    *${texroot}\ *) test -z "$rootserv" && mount_dir ${fs} ${texroot} ro ;;
	    *${texvars}\ *) test -z "$varsserv" && mount_dir ${fs} ${texvars} ro ;;
	    *${texfont}\ *) test -z "$fontserv" && mount_dir ${fs} ${texfont} rw ;;
	    *${texconf}\ *) test -z "$confserv" && mount_dir ${fs} ${texconf} ro ;;
	    *)  ;;
	esac
    done

    test_system
    test -z "$rootserv" && echo "${we}: Warning, can not mount ${texroot}" 1>&2
    test -z "$varsserv" && echo "${we}: Warning, can not mount ${texvars}" 1>&2
    test -z "$fontserv" && echo "${we}: Warning, can not mount ${texfont}" 1>&2
    test -z "$confserv" && echo "${we}: Warning, can not mount ${texconf}" 1>&2
}

#
# Umount the teTeX system
#
umount_system ()
{
    test_system
    test -n "$rootserv" && umount ${texroot}
    test -n "$varsserv" && umount ${texvars}
    test -n "$fontserv" && umount ${texfont}
    test -n "$confserv" && umount ${texconf}
}

#
# Do not overlink/remove existing files or foreign links.
#
test_file ()
{
    tfl=$1
    tfp=$2
    if test -f $tfl -a ! -L $tfl ; then
	echo "${we}: File $tfl already exists, skipping"  1>&2
	echo "${tab} $tfp"  1>&2
	return 1
    fi
    if test -L $tfl ; then
	set -- `ls -iL $tfl $tfp`
	if test $1 -ne $3 ; then
	    echo -e "${we}:\033[1m WARNING symbolic link $tfl does not have the" 1>&2
	    echo -e "${tab} source $tfp\033[m" 1>&2
	    return 1
	fi
    fi
    return 0
}

#
# Integrate the binaries, manual pages, and info descriptions
# into the normal file system hierarchy.
#
linkin ()
{
    relbin=`relpath ${texroot}/teTeX/bin/$binary /usr/bin`
    for prg in ${texroot}/teTeX/bin/$binary/* ; do
	lprg=/usr/bin/${prg##*/}
	test -x $prg || continue
	test_file $lprg $prg || continue
	rm -f $lprg
	ln -sf $relbin/${prg##*/} $lprg
    done
    relman=`relpath ${texroot}/teTeX/man/man1 /usr/share/man/man1`
    for man in ${texroot}/teTeX/man/man1/*.1* ; do
	lman=/usr/share/man/man1/${man##*/}
	test -f $man || continue
	test_file $lman $man || continue
	rm -f $lman
	ln -sf $relman/${man##*/} $lman
    done
    relinf=`relpath ${texroot}/teTeX/info /usr/share/info`
    for inf in ${texroot}/teTeX/info/*info* ; do
	linf=/usr/share/info/${inf##*/}
	test -f $inf || continue
	test_file $linf $inf || continue
	rm -f $linf
	ln -sf $relinf/${inf##*/} $linf
    done
}

#
# Deintegrate the binaries, manual pages, and info descriptions.
#
linkout ()
{
    for prg in ${texroot}/teTeX/bin/$binary/* ; do
	lprg=/usr/bin/${prg##*/}
	test -x $prg || continue
	test_file $lprg $prg || continue
	rm -f $lprg
    done
    for man in ${texroot}/teTeX/man/man1/*.1* ; do
	lman=/usr/share/man/man1/${man##*/}
	test -f $man || continue
	test_file $lman $man || continue
	rm -f $lman
    done
    for inf in ${texroot}/teTeX/info/*info* ; do
	linf=/usr/share/info/${inf##*/}
	test -f $inf || continue
	test_file $linf $inf || continue
	rm -f $linf
    done
}

#
# This calculates the relative path of the source path
# which depends on the destination path. The function
# takes two arguments:
#
#     the full directory path to the source file/dir
#     the full directory path to the destination file/dir
#
# both without the file or directory name its self.
#
# Author: Werner Fink
#
relpath ()
{
    # called by linkin: top down
    IFS='/'
    orgwords="`echo ${1}`"
    locwords="`echo ${2}`"
    IFS="$OLDIFS"

    newpath=""
    symlink="$2"
    relp=0
    deep=0
    set -- `echo $locwords`
    for p in $orgwords ; do
	deep=`expr $deep + 1`
	eval l=\${$deep}
	if test "$l" = "$p" -a $relp -eq 0 ; then
	    : nothing
	else
	    relp=`expr $relp + 1`
	    newpath="${newpath}/$p"
	    if test -n "$l" ; then
		if test $relp -eq 1 ; then
		    newpath="..${newpath}"
		else
		    newpath="../${newpath}"
		fi
	    fi
	fi
    done
    if test $deep -lt $# ; then
	shift $deep
	while test $# -gt 0  ; do
	    newpath="../${newpath}"
	    shift
	done
    fi
    echo $newpath
}

#
# Usage
#
usage ()
{
    echo "${we}: Usage:" 1>&2
    echo "${tab} ${we} <NFS server of TeTeX> [-i|-d] -[o <mount options>]" 1>&2
}

#
# Option handling goes here
#
set -- $argv
while test -n "$1" ; do
    case "$1" in
	-o) test -z "$2" && { usage ; exit 1; }
            mntopt="$2"     ;;
	-m) test -z "$2" && { usage ; exit 1; }
            binary="$2"     ;;
	-i) action="import" ;;
	-d) action="remove" ;;
	-h) usage ; exit 0  ;;
	-*) usage ; exit 1  ;;
	 *) server="$1"   ;;
    esac
    shift
done

test -z "$server" && { usage ; exit 1; }

if    test "$action" = "import" ; then
    mount_system
    linkin
    if test -n "${etcfstab}" ; then
	echo -e "\033[1m${we}: Please add the following $num lines to /etc/fstab\033[m" 1>&2
	echo
        echo -e "${etcfstab}"
	sleep 2
    fi
elif  test "$action" = "remove" ; then
    linkout
    umount_system
    while read line ; do
	set -- $line
	case "$1 $2 $3 $4 $5 $6" in
	    *:${texroot}\ *) etcfstab="${etcfstab}`echo $line`\n" ;;
	    *:${texvars}\ *) etcfstab="${etcfstab}`echo $line`\n" ;;
	    *:${texfont}\ *) etcfstab="${etcfstab}`echo $line`\n" ;;
	    *:${texconf}\ *) etcfstab="${etcfstab}`echo $line`\n" ;;
	esac
    done < /etc/fstab
    if test -n "${etcfstab}" ; then
	echo -e "\033[1m${we}: Please remove the following lines from /etc/fstab\033[m" 1>&2
	echo
        echo -e "${etcfstab}"
	sleep 2
    fi
else
    echo "${we}: Ohmm ... something strange happens" 1>&2
    exit 1
fi

##
exit 0
