#!/bin/sh
##############################################################################
#
#   install.unx - Installation script for system software from PCDOS format
#                diskettes.
#
#   Copyright (c) 1993 by cisco Systems, Inc.
#
#   Modification History
#   01/25/93  Robert Poling     Created.
#   01/25/93  Robert Poling     Added banner; changed file permissions to 644.
#
##############################################################################
MOUNT="mount -rt pcfs"
DEV="/dev/fd0"
DEVPATH="/pcfs"
for args in $*;
do
        case "$args" in
        -m*)
            MOUNT=`expr $args : "-m\(.*\)"`;;
        -d*)
            DEV=`expr $args : "-d\(.*\)"`;;
        -p*)
            DEVPATH=`expr $args : "-p\(.*\)"`;;
        esac
done
##############################################################################
#
#   FUNCTION Prompt_RESPONSE <promptstring> <defaultstring>
#       Prompt user for input and store response in variable RESPONSE.
#
##############################################################################
Prompt_RESPONSE()
{
    echo -n "$1 "
    if [ "$2" != "" ] ; then
        echo -n "[$2] "
    fi
    read RESPONSE
    if [ "$RESPONSE" = "" ] ; then
        RESPONSE=$2
    fi
}
##############################################################################
#
#   FUNCTION Prompt_YN <promptstring> <defaultstring>
#       Prompt user for y/n and store response in variable YN.
#
##############################################################################
Prompt_YN()
{
    YN=""
    until [ "$YN" != "" ] ; do
        echo -n "$1 (y/n)? "
        if [ "$2" != "" ] ; then
            echo -n "[$2] "
        fi
        read YN
        case "$YN" in
            "")                YN=$2;;
            [Yy]|[Yy][Ee][Ss]) YN="y";;
            [Nn]|[Nn][Oo])     YN="n";;
            *)                 YN="";;
        esac
    done
}

#   Check for super-user capability.  
CURUSER=`whoami`
if [ "$CURUSER" != "root" ] ; then
    echo
    echo "You must be logged into the system as root to run this script!"
    exit 1
fi

#   If running from diskette, load into /tmp and re-execute.
if [ "$0" != "/tmp/install.unix" ]; then
    cp /pcfs/install.unx /tmp/install.unix
    exec /tmp/install.unix
fi

#   Display banner.
echo
echo "***********************************************************************"
echo "UNIX INSTALL(1.1)                      Copyright (c) 1993 Cisco Systems"
echo "***********************************************************************"

#   Prompt for installation device and path.
SRC_DEV=""
SRC_PATH=""
until [ "$SRC_PATH" != "" ]; do

#   Check for valid device.
    echo
    Prompt_RESPONSE "Specify device for software being installed:" "$DEV"
    eject -q $DEV > /dev/null
    if [ "$?" != 0 ]; then
        echo
        echo "Wrong device!  Diskette not available on $RESPONSE"
        RESPONSE=""
    else
        SRC_DEV="$RESPONSE"
        echo
        Prompt_RESPONSE "Specify path for software being installed:" "$DEVPATH"
    fi

#   Check that directory exists.
    if [ "$RESPONSE" != "" ]; then
        if [ -f $RESPONSE ]; then
            echo "$RESPONSE is not a directory!"
            RESPONSE=""
        elif [ ! -d $RESPONSE ]; then
            echo "Directory $RESPONSE does not exist!"
            RESPONSE=""
        fi
    fi

#   Check that DISKINFO file exists and parse out IMAGE and DISKS.
    if [ "$RESPONSE" != "" ]; then
        if [ ! -f $RESPONSE/diskinfo ]; then
            echo "Invalid installation directory!"
            echo "Unable to find file $RESPONSE/diskinfo"
            RESPONSE=""
        else
            IMAGE=`awk -F: '{if ($1=="IMAGE") {
                s=substr($2,1,8)
                print s
            } }' $RESPONSE/diskinfo`
            DISKS=`awk -F: '{if ($1=="DISKS") {
                s=substr($2,1,1)
                print s
            } }' $RESPONSE/diskinfo`
        fi
    fi

#   Check that README file exists.
    if [ "$RESPONSE" != "" ]; then
        if [ ! -f $RESPONSE/readme ]; then
            echo "Invalid installation directory!"
            echo "Unable to find file $RESPONSE/readme"
            RESPONSE=""
        fi
    fi

#   Check for .001 file.
    if [ "$RESPONSE" != "" ]; then
        if [ ! -f "${RESPONSE}/${IMAGE}.001" ]; then
            echo "Invalid installation directory!"
            echo "Unable to find file $RESPONSE/$IMAGE.001"
        else
            SRC_PATH="$RESPONSE"
        fi
    fi
done

#   Display readme file.
echo 
echo "The following system software image will be installed:"
echo
cat $SRC_PATH/readme
echo
Prompt_YN "Do you wish to continue" "y"
if [ "$YN" = "n" ] ; then
    exit 0
fi

#   TFTP check.
TFTP_PATH=""
TFTPERR=0
echo 
echo "Checking for TFTP directory and correct TFTP service configuration ..."
#
# look for the tftp port instead of checking inetd.conf.
#
netstat -an | awk '{print $1,$4}' | grep "^udp \*\.69$" > /dev/null
if [ $? != 0 ]; then
        echo "The TFTP service is disabled in /etc/inetd.conf!"
        TFTPERR=1
fi
TFTPDIR=`grep tftp /etc/inetd.conf | awk '{print $NF}'`
if [ ! -d $TFTPDIR ]; then
        echo "The TFTP directory \"${TFTPDIR}\" doesn't exist!"
        TFTPERR=1
else
    ls -ld $TFTPDIR | awk '{print $1}' | grep '-' > /dev/null
    if [ $? != 1 ]; then
        echo "The TFTP directory \"${TFTPDIR}\" has incorrect permissions."
        TFTPERR=1
    else
        TFTP_PATH="$TFTPDIR"
    fi
fi
if [ $TFTPERR != 0 ]; then
    echo 
    echo "If you want to use your TFTP server for upload/download of system"
    echo "software images, please refer to your computer system manuals for"
    echo "information on configuring and enabling TFTP."
else
    echo "TFTP is correctly configured."
fi

#   Prompt for destination directory.
echo
DEST_PATH=""
until [ "$DEST_PATH" != "" ]; do
    echo "Please specify a destination directory which has sufficient"
    echo "file space to accomodate this system software image."
    Prompt_RESPONSE "Where will files be installed?" "$TFTP_PATH"
    if [ "$RESPONSE" != "" ]; then
        if [ -f $RESPONSE ]; then
            echo 
            echo "$RESPONSE is not a directory!"
        elif [ ! -d $RESPONSE ]; then
            Prompt_YN "Directory $RESPONSE does not exist!  Create it" "y"
            if [ "$YN" = "y" ] ; then
                if `mkdir $RESPONSE > /dev/null 2>&1`; then
                    echo 
                    echo "Directory $RESPONSE has been created."
                    DEST_PATH="$RESPONSE"
                else
                    echo 
                    echo "Unable to create directory $RESPONSE"
                fi
            fi
        elif [ ! -w $RESPONSE ]; then
            echo 
            echo "Directory $RESPONSE is not writable!"
        else
            DEST_PATH="$RESPONSE"
        fi
    fi
done

#   Copy README file.
echo 
echo "Installing README file as ${DEST_PATH}/${IMAGE}.DOC ..."
cp "${SRC_PATH}/readme" "${DEST_PATH}/${IMAGE}.DOC"

#   Copy system software image.
echo
echo "Installing system software image $IMAGE as $DEST_PATH/$IMAGE ..."
n=1
until [ $n -gt $DISKS ]; do
    echo "Copying ${SRC_PATH}/${IMAGE}.00$n ..."
    if [ n -eq 1 ]; then
        cp "${SRC_PATH}/${IMAGE}.00$n" "${DEST_PATH}/${IMAGE}"
    else
        cat "${SRC_PATH}/${IMAGE}.00$n" >> "${DEST_PATH}/${IMAGE}"
    fi
    n=`expr $n + 1`
    if [ $n -le $DISKS ]; then
        DONE="false"
        until [ "$DONE" = "true" ]; do
            eject $SRC_DEV > /dev/null
            echo
            echo "Insert Disk $n in device $SRC_DEV"
            echo "Press RETURN when ready ..."
            read ANY
            $MOUNT $SRC_DEV $SRC_PATH
            eject -q $SRC_DEV
            if [ "$?" != 0 ]; then
                echo
                echo "No disk detected in device $SRC_DEV"
            elif [ ! -f "${SRC_PATH}/${IMAGE}.00$n" ]; then
                echo
                echo "Unable to find file $SRC_PATH/$IMAGE.00$n"
                echo "Please check that appropriate disk is installed."
            else
                DONE="true"
            fi
        done
    fi
done
eject $SRC_DEV

#   Change file protections to 644.
chmod 644 "${DEST_PATH}/${IMAGE}"
chmod 644 "${DEST_PATH}/${IMAGE}.DOC"

echo 
echo "Installation for $IMAGE complete!"
