l "program '$this_bin' not found in PATH"
    done
}

man_fix_texmf()
{
    file=$TETEXDIR/man/man1/bibtex.1
    test -f "$file" || return
    man_texmf_old=`grep texmf/bibtex/bst $file 2>/dev/null | sed 's@.*:@@; s@/bibtex/bst@@'`
    test -z "$man_texmf_old" && man_texmf_old=$man_texmf_old_fallback
    man_tetexdir_old=`dirname "$man_texmf_old"`
    test -z "$man_tetexdir_old" && man_tetexdir_old=$man_tetexdir_old_fallback

    if test "$opt_savespace_man_rm" = X; then
        rm -f $TETEXDIR/man/man*/*
        rmdir $TETEXDIR/man/man*
    fi 2>/dev/null

    if test "$TEXMF" != "$man_texmf_old" ||
       test "$man_tetexdir_old" != "$TETEXDIR"; then
        $echon "Fixing pathnames in the manualpages... "
        for mpage in $TETEXDIR/man/*/*[0-9]; do
          sed s@$man_texmf_old@$TEXMF@g\;s@$man_tetexdir_old@$TETEXDIR@g \
            $mpage > $TMPDIR/man.tmp.$$
          test -s $TMPDIR/man.tmp.$$ && mv $TMPDIR/man.tmp.$$ $mpage
        done
        echo "Done."
    fi
    test "$opt_savespace_man_gzip" = X && gzip $TETEXDIR/man/cat*/*.[1.9]
    touch $TETEXDIR/man/cat*/* 2>/dev/null
}

maketex_setopt()
{
    mtsite=$TEXMF/$mt_site_loc
    test -w "$mtsite" || return
    test -w "$TETEXDIR/texmf.cnf" || return
    test "$opt_varfonts" = X || return

    ed "$TETEXDIR/texmf.cnf" >$ERRLOG 2>&1 <<eof
/^VARFONTS.*=/
c
VARFONTS	= $opt_varfonts_dir
.
w
q
eof
    show_error

    ed "$mtsite" >$ERRLOG 2>&1 <<'eof'
/^: \${MT_FEATURES=/
s/}/:varfonts}/
w
q
eof
    show_error
}

texmfcnf_fix_texmf()
{
    test "$TETEXDIR/texmf" = "$TEXMF" && return
    test -w "$TETEXDIR/texmf.cnf" || return
    ed "$TETEXDIR/texmf.cnf" >$ERRLOG 2>&1 <<-eof
/^TEXMF.*=/
c
TEXMF		= $TEXMF
.
w
q
eof
    show_error
    rm -f $TETEXDIR/texmf
}

texmfcnf_fix_tetexdir()
{
    test -f $TETEXDIR/texmf.cnf || return
    if test ! -w "$TETEXDIR/texmf.cnf"; then
        warning "Warning: cannot write to file $TETEXDIR/texmf.cnf"
        readln
        return
    fi
    if $platform_subdir_strip = true; then
        newtexmf='$SELFAUTODIR'
    else
        newtexmf='$SELFAUTOPARENT'
    fi
    ed "$TETEXDIR/texmf.cnf" >$ERRLOG 2>&1 <<-eof
/^TETEXDIR.*=/
c
TETEXDIR           = $newtexmf
.
w
q
eof
    show_error
}

# fill a string with blanks:
setlength()
{
    var=$1
    length=$2
    eval value=\"\$$var\"
    l=`expr "$value" : '.*'`
    test $l -lt $length || return
    d=`expr $length - $l`
    OLDIFS=$IFS; IFS=''
    substr=`awk 'END {print substr("                                         ", 1, ANZ)}' ANZ=$d </dev/null`
    eval $var='$substr$value'
    IFS=$OLDIFS
}

# NEW PLATFORM: add a 'case'
platform_guess()
{
   UNAME_MACHINE=`uname -m 2>/dev/null` || UNAME_MACHINE=unknown
   UNAME_RELEASE=`uname -r 2>/dev/null` || UNAME_RELEASE=unknown
   UNAME_SYSTEM=`uname -s  2>/dev/null` || UNAME_SYSTEM=unknown
   UNAME_VERSION=`uname -v 2>/dev/null` || UNAME_VERSION=unknown

   case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in
     *86:FreeBSD:*:*)
       echo i386_freebsd205;;
     *86:NetBSD:*:*)
       echo i386_netbsd10;;
     m68k:Linux:*:*)
       { /sbin/ldconfig -v | grep libc.so.5; } >/dev/null 2>&1
       if test $? = 0; then
         echo m68k_linux
       else
         echo m68k_linuxaout
       fi;;
     *:Linux:*:*)
       { /sbin/ldconfig -v | grep libc.so.5; } >/dev/null 2>&1
       if test $? = 0; then
         echo i486_linux
       else
         echo i486_linuxaout
       fi;;
     *:IRIX*:*:*)
       echo mips_irix53;;
     *:AIX:2:3)
       echo rs6000_aix32;;
     sun4*:SunOS:5.*:*)
       echo sparc_solaris24;;
     sun4*:SunOS:*:*)
       echo sparc_sunos413;;
     alpha:OSF1:V3*:*)
       echo alpha_osf32;;
     alpha:OSF1:*:*)
       echo alpha_osf20;;
     9000*:HP-UX:*:*)
       echo hppa11_hpux901;;
     *:ULTRIX:*:*)
       echo mips_ultrix_44;;
   esac
}

unset_vars()
{
    for var in $envvars; do
        unset $var
    done
}

mkdirhier()
{
    case $1 in
        /*) cd /;;
    esac
    OLDIFS=$IFS; IFS=/; eval set $1; IFS=$OLDIFS
    for i
    do
        test -d $i || mkdir $i || break
        cd $i || break
    done
}

# [t]ry again, [r]econfigure, [a] abort
tra()
{
    msg=$1
    cls
    echo "Oops, I am in trouble here! The error was:"
    echo ">> $msg <<"
    echo
    case `getopt TRQ 'What shall we do now: <t>ry again, <r>econfigure, <q>uit'` in
        T) return;;
        R) menu_main;;
        Q) exit 1;;
    esac
}

warn()
{
    msg=$1
    cls
    echo "Oops, I am in trouble here! The problem was:"
    echo ">> $msg <<"
    echo
    case `getopt CQ 'What shall we do now: <c>continue, <q>uit'` in
        C) return;;
        Q) exit 1;;
    esac
}

fatal()
{
    echo; echo
    echo "ERROR: $@."
    echo "This was a fatal error, my friend. Installation aborted."
    exit 1
}

show_error()
{
    test $? = 0 && return
    cls
    echo 'WARNING: the last command returned an error.'
    if yesno 'Do you want to see the errorlog'; then
        cat $ERRLOG | $PAGER
        readln
    fi
    cls
}

# this is a hard job... trying to anticipiate trouble...
sanity_checks()
{
    while test -d $TMPDIR; do
        tra "temporal directory $TMPDIR already exists"
    done
    while test -d $TETEXDIR; do
        tra "directory TETEXDIR=$TETEXDIR already exists"
    done
    while test -d $TEXMF; do
        tra "directory TEXMF=$TEXMF already exists"
    done
    while test "$opt_symlink" = X &&
          test ! -z "$opt_symlinks_bin" &&
          test -d "$opt_symlinks_bin" &&
          test ! -w "$opt_symlinks_bin"; do
        tra "cannot write to directory $opt_symlinks_bin"
    done
    while test "$opt_symlink" = X &&
          test ! -z "$opt_symlinks_man" &&
          test -d "$opt_symlinks_man" &&
          test ! -w "$opt_symlinks_man"; do
        tra "cannot write to directory $opt_symlinks_man"
    done
    while test "$opt_symlink" = X &&
          test ! -z "$opt_symlinks_info" &&
          test -d "$opt_symlinks_info" &&
          test ! -w "$opt_symlinks_info"; do
        tra "cannot write to directory $opt_symlinks_info"
    done
    while test ! -z "$opt_varfonts_dir" &&
          test -d "$opt_varfonts_dir" &&
          test ! -w "$opt_varfonts_dir"; do
        tra "cannot write to directory $opt_varfonts_dir"
    done
}

prepare_directories()
{
    trap 'cd /; rm -rf "$TMPDIR"; rm -f "$TEXMF" "$ERRLOG" 2>/dev/null; trap '' 0; exit 0' 0 1 2 15
    alldirs="$TMPDIR $TETEXDIR $TEXMF
             $opt_symlinks_bin $opt_symlinks_info"
    test -z "$opt_symlinks_man" ||
             alldirs="$alldirs $opt_symlinks_man/man1 $opt_symlinks_man/man5 $opt_symlinks_man/cat1 $opt_symlinks_man/cat5"
    test -z "$opt_varfonts_dir" ||
             alldirs="$alldirs $opt_varfonts_dir/pk $opt_varfonts_dir/tfm"
    for dir in $alldirs; do
        while test ! -d $dir || test ! -w $dir; do
          mkdirhier $dir
          test -d $dir || { tra "could no