#!/bin/sh
#
#######################################################################
#
# This script performs, at most, the following major functions...
#
#   1) Protect 'save' files from being overwritten
#   2) Allow the extraction of subsets of the contents of the
#	install media
#   3) Allow installation from a remote device
#
#######################################################################
#
#   The above is done by using the following scripts
#
#   proc_save_file	- does the save and restore of files specified
#			    by the TEKXP_SAVE.LIST
#
#   ext_inst_lst - extract the specified files from the tar file
#			    or distribution media.
#
#   build_inst_lst   - query the user about what portions of the install
#			    media should be installed.
#
#   exec_media_cmd - handles the media/archive commands: rewind,
#		 	    mt, tar, etc....
#
#######################################################################
#
#

#
# Determine the install and default destination directory
#

MANUALTAR=
ABSOLUTE=`expr  $0 : '\/'`

PWD=`pwd`
 
CURDIR=$PWD

if [ $ABSOLUTE -gt 0 ]
then
    FULLPATH=$0
else
    
    if [ "$CURDIR" = "/" ]
    then
        FULLPATH=/$0
    else
        FULLPATH=$CURDIR/$0
    fi
fi

TEKXPDEST=`expr  $FULLPATH : '\(.*\)/tekxp'`
if [ -z "$TEKXPDEST" ]
then
    TEKXPDEST=/
fi

TEKXPINSTALLDIR=`expr  $FULLPATH : '\(.*\)/INSTALL'`

BOOTGRPS="xp10 xp300 xp330 xp350 pex"
FONTGRPS="misc 100dpi 75dpi japanese oldx11 openlook tek100dpi Speedo Type1"

#These need to be kept in sync with 'sel_file_pkgs'
export FONTGRPS

BOOT_PACKAGE_DIRS="fonts"

TEKXPAUDIT=
TEKXPSAVE=
XPQUICKDIR=
XPUTILDIR=
TEKXPPKGS=
TEKXPFILES=
TEKXPDEV=
TEKXPMODE=
TEKXPHOST=
TEKXPUSER=

DateStamp=`date +%m%d%y%H%M`

umask 0022

#
# Make the log file
#
TEKXPLOG="$TEKXPINSTALLDIR/INSTALL.log"

if [ -f "$TEKXPLOG" ]
then
    TEKXPOLD=$TEKXPLOG"_"$DateStamp
    mv $TEKXPLOG $TEKXPOLD
fi

touch $TEKXPLOG
if [ $? -ne 0 ]
then
   echo "Cannot create log file.  Output to standard out only"
   TEKXPLOG='/dev/null'
else
   TEKXPLOG="$TEKXPINSTALLDIR/INSTALL.log"
fi


do_usage()
{
echo ""
echo "Usage: $0 -f <device> [other flags]"
echo ""
echo "  Device flag..."
echo ""
echo "    -f dev    Media device or file, use 'default' for default, otherwise"
echo "              use specific device name /dev/nrst1, /dev/nrmt0, etc..."
echo "		    A tar archive may also be named here."
echo "              Since there are more than one archives on the tape,"
echo "              a no-rewind device will be needed. This device is"
echo "              often indicated by an 'n' either before or after the"
echo "              device name as in the examples above, 'nrst1' and "
echo "              'nrmt0' or '/dev/rmt/0mn' as an example where the 'n'"
echo "              is at the end of the device name."
echo ""
echo "  Other flags <use none, one,  or more>..."
echo ""
echo "    -host name    Name of remote host the device is associated with"
echo "    -user name    Name to be used in a remote device access"
echo ""
echo "    -oldq dir     Old style Quick Install directory"
echo "    -oldu dir     Old style XP Utilities directory"
echo ""
echo "    -move		Used to place files that were removed from the"
echo "                  tape with a 'tar' run from outside the INSTALL."
echo ""
}

do_bad_usage()
{
echo ""
echo "Invalid flag: " $1

do_usage
}


do_display_device()
{
echo ""

if [ "$TEKXPHOST" ]
then
   HOSTMSG="from remote host $TEKXPHOST"
   if [ "$TEKXPUSER" ]
   then
       HOSTMSG="from host: $TEKXPHOST, user: $TEKXPUSER"
   fi
fi

if [ "$TEKXPDEV" = "defdev" ]
then
    echo "Using $TEKXPMODE command with default device $HOSTMSG"
else
    echo "Using $TEKXPMODE command with $TEKXPDEV $HOSTMSG"
fi
echo ""
}

do_display_info()
{
#
# Build the audit file path
#
TEKXPTEMP=$TEKXPINSTALLDIR/$TEKXPAUDIT
TEKXPAUDIT=$TEKXPTEMP

echo ""
echo "The installation is proceeding based on the following..."
echo ""
echo "    Installation Files:" $TEKXPINSTALLDIR
echo "    Installation Log:  " $TEKXPLOG
echo "    Destination:       " $TEKXPDEST
echo "    Audit File:        " $TEKXPAUDIT

if [ "$TEKXPMODE" ]
then
    echo "    Install Mode:      " $TEKXPMODE
fi

if [ "$TEKXPDEV" ]
then
    echo "    Media Device Name: " $TEKXPDEV
fi

if [ "$TEKXPSAVE" ]
then
    TEKXPTEMP=$TEKXPINSTALLDIR/$TEKXPSAVE
    TEKXPSAVE=$TEKXPTEMP
    echo "    Save File List:    " $TEKXPSAVE
fi
if [ "$XPQUICKDIR" ]
then
    echo "    Old Quick Install: " $XPQUICKDIR
fi
if [ "$XPUTILDIR" ]
then
    echo "    Old XP Utilities:  " $XPUTILDIR
fi
echo ""
echo "Do you wish to continue [y/n]?"
read ans
if [ $ans != "y" -a $ans != "Y" ]
then
    echo "Abortting Installation."
    exit 1
fi
}


#
# Set up the default values for all undefined file and directory variables
# If the variables already have
#
do_defaults()
{
if [ "$TEKXPMODE" ]
then
    if [ "$TEKXPMODE" != "tar" -a "$TEKXPMODE" != "tcio" ]
    then
    	echo "ERROR: Unknown method of file extraction: $TEKXPMODE"
    	exit 1
    fi
else
    TEKXPMODE="tar"
fi


if [ "$TEKXPDEST" ]
then
    if [ ! -d "$TEKXPDEST" ]
    then
	echo "ERROR: Destination $1 does not exist or is not a directory"
    	exit 1
    fi
else
    echo "ERROR: Unable to determine destination directory"
    exit 1
fi

#
# If an audit file is specified, use it, if not use the default name. Then
# if the name auditfile exists, rename it with a current date-time Stamp
#

if [ -z "$TEKXPAUDIT" ]
then
    TEKXPAUDIT=$TEKXPLOG
fi

#
# If an save file is specified, use it, if not use the default name. Then
# if the name savefile exists, rename it with a current date-time Stamp
#

if [ -z "$TEKXPSAVE" ]
then
    if [ -f "$TEKXPINSTALLDIR/TEKXP_SAVE.LIST" ]
    then
    	TEKXPSAVE=$TEKXPINSTALLDIR/TEKXP_SAVE.LIST
    fi
else
    if [ ! -f "$TEKXPSAVE" ]
    then
    	echo "ERROR: Specified save file, $TEKXPSAVE, does not exist..."
        echo "       remove -s switch, or specify existing save file."
    	echo ""
    	exit 1
    fi
fi

#
# If an package file is specified, use it, if not use the default name. 
#

if [ -z "$TEKXPPKGS" ]
then
    TEKXPPKGS=$TEKXPINSTALLDIR/TEKXP_PKGS.LST
fi

#
# Check for the specified existing quick install directory
#

if [ "$XPQUICKDIR" ]
then
    if [ ! -d "$XPQUICKDIR" ]
    then
    	echo "ERROR: Specified Quick Install directory, $XPUTILDIR, does"
    	echo "	     not exist. Remove -oldq switch, or specify an existing"
	echo "	     Quick Install directory"
    	echo ""
    	exit 1
    fi
fi

#
# Check for the specified existing quick install directory
#

if [ "$XPUTILDIR" ]
then
    if [ ! -d "$XPUTILDIR" ]
    then
    	echo "ERROR: Specified XP Utilities directory, $XPUTILDIR,"
    	echo "	     does not exist. Remove -oldq switch, or specify an "
	echo "	     existing XP Utilities directory"
    	echo ""
    	exit 1
    fi
fi

}

####################################
#
# Main part of Installation script
#
####################################

#
# Get the switches and set the appropriate variables
#

while [ $# -gt 0 ]
do
    case $1 in
      -m) if [ $# -gt 1 ]; then
    	     TEKXPMODE=$2
    	     shift
    	  fi
    	  shift;;
      -d) if [ $# -gt 1 ]; then
    	     TEKXPDEST=$2
    	     shift
    	  fi
    	  shift;;
      -host) if [ $# -gt 1 ]; then
    	         TEKXPHOST=$2
    	         shift
    	     fi
    	     shift;;
      -user|-l) if [ $# -gt 1 ]; then
    	         TEKXPUSER=$2
    	         shift
    	     fi
    	     shift;;
      -f) if [ $# -gt 1 ]; then
	    if [ $2 = "default" ]; then
    	    	TEKXPDEV="defdev"
	    else
		TEKXPDEV=$2
	    fi
    	    shift
	  else
	    do_bad_usage $1
    	    exit 1
    	  fi
    	  shift ;;
      -oldq) if [ $# -gt 1 ]; then
    	    	XPQUICKDIR=$2
    	    	shift
    	     fi
    	     shift ;;
      -oldu) if [ $# -gt 1 ]; then
    	    	XPUTILDIR=$2
    	    	shift
    	     fi
    	     shift ;;
      -move) MANUALTAR="YES"
    	     shift ;;
      -*) BADFLAG=$1; do_bad_usage $1; exit 1 ;;
      *) BADFLAG=$1; do_bad_usage $1; exit 1 ;;
    esac
done

if [ "$TEKXPDEV" -a -z "$TEKXPHOST" -a ! -f "$TEKXPDEV" -a ! -c "$TEKXPDEV" ]
then
    echo "$TEKXPDEV is not a device or a file"
    exit 1
fi

if [ -z "$TEKXPDEV" ]
then
    TEKXPDEV=defdev
fi

#
# Set defaults if none exist...
#
do_defaults

export TEKXPINSTALLDIR

#
# Display the varialbe values that will be used in this installation
#
#do_display_info
do_display_device

#
# Start the install
#
echo "INSTALL started `date`" | tee -a $TEKXPLOG
if [ $? -ne 0 ]
then
   echo "Cannot create log file.  Output to stdout only"
   TEE='cat'
else
   TEE="tee -a $TEKXPAUDIT"
fi
export TEE


if [ "$TEKXPMODE" = "tar" -a -z "$MANUALTAR" ]
then
    
    #
    # Multiple tar archives require the mt command to read over the EOF
    # of the first tar. Since the flags can be different from os to os,
    # run the usage message through awk to get the device flags. For
    # remote tape installations it is a little trickier but the principle
    # is the same, do an rsh of the mt command to get the usage. The
    # exec_media_cmd is used to execute any needed commands, if
        # the commands are not needed, they will not be executed.
    #

    $TEKXPINSTALLDIR/exec_media_cmd $TEKXPINSTALLDIR rewind "" "$TEKXPDEV" "$TEKXPHOST" "$TEKXPUSER" "$TEKXPAUDIT"
    if [ $? -ne 0 ]
    then 
        exit 1
    fi

    $TEKXPINSTALLDIR/exec_media_cmd $TEKXPINSTALLDIR fsf 1 "$TEKXPDEV" "$TEKXPHOST" "$TEKXPUSER" "$TEKXPAUDIT"
    if [ $? -ne 0 ]
    then 
        exit 1
    fi

fi

if [ -z "$MANUALTAR" ]
then

    #
    # Build and execute the script that creates the list of files to be
    # installed from the media
    #
    #
    echo "------- Build Install List ------------ " | $TEE
    echo ""

    if [ -f "$TEKXPINSTALLDIR/TEKXP_SIZE.LST" ]
    then
        TEKXPSIZES=$TEKXPINSTALLDIR/TEKXP_SIZE.LST
    fi

    $TEKXPINSTALLDIR/build_inst_lst $TEKXPINSTALLDIR "$TEKXPDEST" $TEKXPPKGS "$TEKXPSIZES" "$TEKXPAUDIT" "$TEKXPMODE"

    #
    # 'build_inst_lst' exits with a non-zero if a paritial installation is
    # desired but no selection is made.
    #
    if [ $? -ne 0 ]
    then
        rm -f tmp*
        echo "No files have been selected for installation."
	exit 0
    else

        #
        # Save (or remove) any duplicates of packages to be installed.
        #

        if [ "$TEKXPSAVE" ]
        then
            $TEKXPINSTALLDIR/proc_save_file -s $TEKXPSAVE "$TEKXPDEST" $TEKXPINSTALLDIR/TEKXP_INST.LST $TEKXPAUDIT | $TEE
        fi

        echo "------- Extract Install List ------------ "| $TEE 
        #
        # Use the TEKXP_PKGS.LST list and allow the user to select from it
        #

        $TEKXPINSTALLDIR/ext_inst_lst "$TEKXPMODE" "$TEKXPDEV" $TEKXPINSTALLDIR/TEKXP_INST.LST "$TEKXPDEST" $TEKXPINSTALLDIR "$TEKXPHOST" "$TEKXPUSER" "$TEKXPAUDIT"

        if [ $? -ne 0 ]
        then
            rm -f tmp*
            exit 1
        fi
    fi
fi

#
# Ensure that a tekxp/boot directory exists. 
#
if [ ! -d "$TEKXPDEST/tekxp/boot" ]
then
    mkdir -p $TEKXPDEST/tekxp/boot
fi

#
# Copy over any of the boot_packages that have been installed
#
echo "Moving all installed boot packages to the boot directory" | $TEE
if [ -d "$TEKXPDEST/tekxp/boot_packages" ]
then
    for dir in $BOOTGRPS
    do
        if [ -d "$TEKXPDEST/tekxp/boot_packages/$dir" ]
        then
            cp $TEKXPDEST/tekxp/boot_packages/$dir/* $TEKXPDEST/tekxp/boot | $TEE
            rm -rf $TEKXPDEST/tekxp/boot_packages/$dir | $TEE
        fi
    done

    for dir in $FONTGRPS
    do
        if [ ! -d $TEKXPDEST/tekxp/boot/fonts ]
        then
            mkdir -p $TEKXPDEST/tekxp/boot/fonts
        fi
 
        if [ -d "$TEKXPDEST/tekxp/boot_packages/fonts/$dir" ]
        then
            if [ -d "$TEKXPDEST/tekxp/boot/fonts/$dir" ]
            then
                rm -rf $TEKXPDEST/tekxp/boot/fonts/$dir
            fi
            mv $TEKXPDEST/tekxp/boot_packages/fonts/$dir $TEKXPDEST/tekxp/boot/fonts/$dir | $TEE
        fi
    done
 
    # Remove the boot_packages directory (Nothing should be left in it)
    rm -rf $TEKXPDEST/tekxp/boot_packages | $TEE
fi

#
# If we are still running, restore the saved files
#
if [ "$TEKXPSAVE" ]
then
    $TEKXPINSTALLDIR/proc_save_file -r $TEKXPSAVE $TEKXPDEST $TEKXPINSTALLDIR/TEKXP_INST.LST $TEKXPAUDIT | $TEE
fi

if [ ! -z "$XPQUICKDIR" ]
then
    echo "------ Process Old Install Area: $XPQUICKDIR ------" | $TEE 
    $TEKXPINSTALLDIR/proc_old_inst $XPQUICKDIR $TEKXPINSTALLDIR $TEKXPDEST $TEKXPAUDIT
else
    $TEKXPINSTALLDIR/mod_conf_files $TEKXPINSTALLDIR $TEKXPDEST $TEKXPAUDIT
fi

if [ ! -z "$XPUTILDIR" ]
then
    echo "------ Process Old Utilities Area: $XPUTILDIR ------" | $TEE 
    $TEKXPINSTALLDIR/proc_old_util $XPUTILDIR $TEKXPINSTALLDIR $TEKXPDEST $TEKXPAUDIT
fi

#
# Get the inetd.conf file location
#
if [ -f /etc/inetd.conf ]
then
   INETD="/etc/inetd.conf"
elif [ -f /usr/etc/inetd.conf ]
then
   INETD="/usr/etc/inetd.conf"
else
   INETD=""
fi
#
# Determine the mode tftp is running in and get the secure directory.
#
BOOTDIR=$TEKXPDEST/tekxp/boot
if [ -z "$INETD" ]
then
   SECURE=""
   INSECURE="$condir"
else
   SECURE=`awk '{if($1=="tftp") {
                   secure = 0;
                   for(i=1;i<=NF;i++) {
                       if (secure) {
                          len=length($i);
                          if (substr(boot,1,len) == $i) {
                              print boot;
			      exit;
                          }
                       }
                       if ($i == "-s") {
                          secure = 1;
                       }
                    }
		    if (secure == 0) {
			print "not secure";
		    }
                 }
              }' boot=$BOOTDIR $INETD`

   if [ ! -n "$SECURE" ]
   then
 
      echo " " | $TEE
      echo "WARNING - The boot directory, $BOOTDIR," | $TEE
      echo "        is not located in the secure directory.  You may" | $TEE
      echo "        have to disable secure tftp in $INETD to allow your" | $TEE
      echo "        X terminal to access the boot files. " | $TEE
   fi
fi

chmod 755 $TEKXPDEST/tekxp

echo "INSTALL finished  `date`" | tee -a $TEKXPLOG

exit 0






