#!/bin/sh
#
# This executes various media (file or device) commands remotely or locally.
# An message will be returned if there is an error.
#
# Supported commands are: tar rewind fsf 
#	

INSTALLDIR=$1
MEDIACMD=$2
MEDIAARG=$3
MEDIA=$4
REMHOST=$5
REMUSER=$6
AUDITLOG=$7

TEE="tee -a $AUDITLOG"

TmpCmdFile=$TEKXPINSTALLDIR/tmpCommand$$
CmdDevType=$TEKXPINSTALLDIR/tmpDevType$$

if [ "$MEDIA" = "defdev" ]
then
    MEDIA=""
fi

CMDPREFIX=
ISULTRIX=
if [ "$REMHOST" ]
then
    if [ -z "$MEDIA" ]
    then
	echo "**** Remote access needs a media source (file or device)"
	exit 1
    fi

    HPUX=
    if [ -f /bin/uname ]
    then
	HPUX=`/bin/uname | grep HP-UX`
    fi

    if [ "$REMUSER" ]
    then
	if [ ! -z "$HPUX" ]
	then
	    CMDTARPREFIX=" remsh  $REMHOST -l $REMUSER -n "
            CMDPREFIX=" remsh $REMHOST -l $REMUSER "
	else
	    CMDTARPREFIX=" rsh  $REMHOST -l $REMUSER -n "
	    CMDPREFIX=" rsh $REMHOST -l $REMUSER "
	fi
    else
	if [ ! -z "$HPUX" ]
	then
	    CMDTARPREFIX=" remsh  $REMHOST -n "
	    CMDPREFIX=" remsh  $REMHOST "
	else
	    CMDTARPREFIX=" rsh  $REMHOST -n "
	    CMDPREFIX=" rsh  $REMHOST "
	fi
    fi

    #
    # Do a quick test of the rsh command to see if we can continue
    #
    $CMDPREFIX echo "" 2> /dev/null
    if [ $? -ne 0 ]
    then
	echo "The remote shell execution failed, check your .rhosts file"
	echo "or the host.equiv file. The command format used was..."
	echo "        $CMDPREFIX"
	exit 1
    fi
    if [ ! -z "$HPUX" ]
    then
        TARCMD="tar pxvfb - 20 "
    else
	TARCMD="tar pxvBfb - 20 "
    fi

    ISULTRIX=`$CMDPREFIX /bin/uname | grep ULTRIX`
else
    if [ -z "$MEDIA" ]
    then
        TARCMD=" tar pxv "
    else
	TARCMD=" tar pxvf $MEDIA "
    fi

    ISULTRIX=`/bin/uname | grep ULTRIX`
fi

#
# Build up the command prefix
#
if [ "$MEDIA" ]
then
    echo "if [ ! -c \"$MEDIA\" ]" 	> $TmpCmdFile
    echo "then"			>> $TmpCmdFile
    echo "   echo 'not-device'" >> $TmpCmdFile
    echo "fi"			>> $TmpCmdFile
else 
    echo "" > $TmpCmdFile
fi

#
# First determine if the requested media source, if any, is a character
# special device
#

if [ "$ISULTRIX" ]
then
    $CMDPREFIX sh5 < $TmpCmdFile  > $CmdDevType
else
    $CMDPREFIX sh < $TmpCmdFile  > $CmdDevType
fi

if [ "$CMDPREFIX" -a -s "$CmdDevType" ]
then
    echo "**** Error - The remote source $MEDIA is not a "
    echo "     device. If it is a tar archive file, move"
    echo "     it onto the local machine and then run INSTALL"
    echo "     locally."
    rm -rf $CmdDevType
    rm -rf $TmpCmdFile
    exit 1
fi

umask 0022

if [ "$MEDIACMD" = "tar" -a "$MEDIAARG" ]
then
    TARARG=`cat $MEDIAARG`
    if [ "$REMHOST" ]
    then 
	$CMDTARPREFIX dd if=$MEDIA bs=20b | $TARCMD $TARARG 
    else
        $TARCMD $TARARG 
    fi
    if [ $? -ne 0 ]
    then
	echo "" | $TEE
        if [ -z "$MEDIA" ]
        then
            echo "Error: The 'tar' command failed. Use the '-f' switch" | $TEE
            echo "       and give the specific no-rewind device or tar" | $TEE
            echo "       file to install from..." | $TEE
	else
            echo "Error: The 'tar' command failed. Try again and ensure the" | $TEE
            echo "       the device or tar file is correct when entering" | $TEE
            echo "       the INSTALL command..." | $TEE
	fi	
	echo "" | $TEE
	echo "       INSTALL -f <device or tar file>" | $TEE
	echo "" | $TEE
        rm -rf $CmdDevType
        rm -rf $TmpCmdFile
	exit 1
    else
	echo "" >> $AUDITLOG
	if [ -z "$TARARG" ]
        then
	    echo "Extracted entire contents of installation tape" >> $AUDITLOG
	else 
	    echo "Extracted the following directories from the tape..." >> $AUDITLOG
	    echo "" >> $AUDITLOG
	    cat $MEDIAARG >> $AUDITLOG
        fi
	echo "" >> $AUDITLOG
    fi
else
    # 
    # the source has to be a character special file so the mt command
    # can be tried
    #
    if [ ! -s "$CmdDevType" -a "$MEDIACMD" ] 
    then
	if [ "$MEDIACMD" ]
	then
	    $CMDPREFIX mt 2> $TmpCmdFile

	    grep mt $TmpCmdFile > $TmpCmdFile.grep
	    rm $TmpCmdFile
	    mv $TmpCmdFile.grep $TmpCmdFile

            MTCOMMAND=`awk '{
                devi = index($0,"-");
                if (devi > 0) {
		    if (length(dev) > 0) 
                        print "mt " substr($0,devi,2) " " dev " " cmd " " arg
		    else
			print "mt " cmd " " arg
                }
              }' dev="$MEDIA" cmd="$MEDIACMD" arg="$MEDIAARG" $TmpCmdFile`
	
	    $CMDPREFIX $MTCOMMAND 2> $TmpCmdFile
	    if [ -s "$TmpCmdFile" ]
	    then
		echo "" >> $AUDITLOG
		cat $TmpCmdFile >> $AUDITLOG
		echo ""
	 	echo "Warning - positioning command failed on device $MEDIA" | $TEE
		echo "" | $TEE
		if [ -z "$MEDIA" ]
		then
		    echo "    If the device is not positioned correctly the" | $TEE
		    echo "    installation may fail. Try using the '-f' switch" | $TEE
		    echo "    to specify an appropriate no-rewind device." | $TEE
		    echo "" | $TEE
		    echo "	   INSTALL -f <device or tar file>" | $TEE
		fi
		echo "" | $TEE
	    fi
	else
	    echo "Error - invaild command on $MEDIA: $MEDIACMD" | $TEE
	    rm -rf $CmdDevType
            rm -rf $TmpCmdFile
	    exit 1
	fi
    fi
fi

rm -rf $CmdDevType
rm -rf $TmpCmdFile

echo "" >> $AUDITLOG
exit 0

