#!/bin/sh
#
#### Axcelis Technologies, Solaris INSTALL file ####
####	(the preceding line must not be removed!!)
#
#
#		     Copyright 2001 Axcelis Technologies
#
#   Brief Description:
#	INSTALL -- generic install script to be used with tape
#	    All it does it extract the install module, which is
#	    in tar format, from tape file #2, and executes the
#	    self-install.pl perl script
#
#   Author & Date:
#	930709	VSHarris
#
#   Modification History:
#
#	021028	RNoonan	added CD-ROM support
#	011023	SHarris	use /ics/tmp as tmp dir (shared w/ aux suns)
#	000202	SHarris	Created solaris version (Solaris cookie string)
#			use init-funcs.sh
#			change TAPE variable to /dev/rmt/0cn
#
#	981203	SHarris	Do NOT install unless running SunOS 4.1.3_U1
#	960115	SHarris	Don't call check-avail-space.sh unless on sun1
#	951215	SHarris	Added call to run check-avail-space.sh, to
#			determine if there is enough room to do the install,
#			and to do a "replace-install" if necessary.
#	950929	D.Young	Changed ~uucp to /usr/spool/uucppublic
#			Added code to remove default robot files.
#	950719	D.Young	Added go+rwx for ~uucp (per ddts SEDaa01696)
#	950628	D.Young	moved rdate stuff from rc.local to ~ics/.login 
#	950620	D.Young	added grep rdate rc.local (all suns) to provide 
#			time/date syncronization with sun1.
#	950605	D.Young	added code to determine computer system model
#			 (ipc,classic,ss5)
#	941115	D.Young	reworked the vmunix code section.
#	941114	D.Young	Added SPC installation and new code to support
#			a primitive auto install on remote suns
#	940712	D.Young	Added crontab task for asr 
#	940324	D.Young	fixed /usr/ics/data link test. added tests for
#			/usr/var/ics and /usr/ics/etc.
#	940111	D.Young	added a `arch -k` to the mv vmunix.pen vmunix.
#			This to accomodate the "Classic" note: the IPC
#			kernel and the "classic" kernel  are not compatible
#	931020	D.Young	chmod go+rwx on /usr/tmp /tmp 
#	930909	D.Young	Added test - run activate on sun1 only
#	930907	D.Young	Changed test for running activate
#	930827	D.Young	Added kernel change over to support light pen.
#			activate can be called from this script to save 
#			an additional reboot.
#
#****************************************************************************

umask 002

[ "x$init_funcs_done" = x ] && {
    [ "x$lib_dir" = x ] && lib_dir=$ROOT_DIR/usr/local/lib
    init_funcs_sh=$lib_dir/init-funcs.sh
    [ -f $init_funcs_sh ] || {
	echo "*** FATAL ERROR:  file $init_funcs_sh does not exist" 1>&2
	exit 1
    }
    . $init_funcs_sh
}

#----------------------------------------------------------------------------
# Init vars
#----------------------------------------------------------------------------
Ifs="$IFS"
Rootdir=${SELF_INSTALL_ROOT:-/}
Medium="$1"
Prog="self-install.sh"
Tmpdir=$SELF_INSTALL_ROOT/ics/tmp
[ "$SELF_INSTALL_TAPE" ] && TAPE=$SELF_INSTALL_TAPE || TAPE=/dev/rmt/0cn; export TAPE
Installer=self-install.pl
Toolsball=install_tools.tar
Cddir=/tmp/cdrom
if [ $CDDIR ]; then
        Cddir=$CDDIR
fi


#----------------------------------------------------------------------------
# extract rhost name from $Medium
#----------------------------------------------------------------------------
get_rhost () {
    rflag=1
    IFS="@"
    set $Medium
    IFS="$Ifs"
    shift
    rhost="$1"
}

#----------------------------------------------------------------------------
# make sure rhost is alive
#----------------------------------------------------------------------------
check_rhost () {
    [ "$rhost" ] || fatal "no remote host specified"
    ping "$rhost" 9 > /dev/null 2>&1 && \
    ping "$rhost" 9 > /dev/null 2>&1 && \
    ping "$rhost" 9 > /dev/null 2>&1 || \
	fatal "unable to ping $rhost"
}

#----------------------------------------------------------------------------
#  tape access functions
#----------------------------------------------------------------------------

position_tape () {
    pos=${1-1}
    [ $pos -gt 0 ] && \
	echo "positioning tape to file number $pos" || \
	echo "rewinding tape"
    sleep 5
    if [ "$rhost" ]
    then
	rsh $rhost -n " mt -f $TAPE asf $pos; "
    else
	mt asf $pos
    fi
    sleep 5
}

extract_tape () {
    echo "Extracting files from tape"
    echo "  (ignore warnings about tmp directories existing)"
    echo ""
    if [ "$rhost" ]
    then
	rsh $rhost -n dd if=$TAPE ibs=1024b obs=8b | gtar -xpUmBf - ${SELF_INSTALL_VERBOSE:+"-v"} -b 8
    else
	gtar -xpUmB ${SELF_INSTALL_VERBOSE:+"-v"} -b 1024
    fi
    sleep 5
}

extract_ball () {
	echo "Extracting file from tarball on CD-ROM"
	echo "  (ignore warnings about tmp directories existing)"
	echo ""
	gtar -xpUm ${SELF_INSTALL_VERBOSE:+"-v"} -f $Cddir/$Toolsball
}

do_tape () {
	_cd $Rootdir
	position_tape 1
	extract_tape
	position_tape 0
	[ -f "$Tmpdir/$Installer" ] || fatal "$Tmpdir/$Installer not extracted from tape"
	_cd $Tmpdir
	if [ "$rhost" ]
	then
    	perl ./$Installer -r $rhost
	else
    	perl ./$Installer
	fi
}

do_cdrom () {
	_cd $Rootdir
	extract_ball
	[ -f "$Tmpdir/$Installer" ] || fatal "$Tmpdir/$Installer not extracted from tape"
	_cd $Tmpdir
	perl ./$Installer -c
}

#----------------------------------------------------------------------------
#  mainline code starts here
#----------------------------------------------------------------------------


[ "$iam" ] || iam=`whoami`
[ "$SELF_INSTALL_DEBUG" -lt 1 ] && [ "$iam" != root ] && fatal "You must be superuser to run this script"

usage=\
"Usage:
    $Prog tape
or:
    $Prog tape@host
or:
    $Prog cdrom
"

echo ""

#----------------------------------------------------------------------------
#  work starts here
#----------------------------------------------------------------------------

# get computer system model
case `prtconf` in
    *SPARCstation-5*)	SYSTEM_TYPE=ss5 ;;
    *)			SYSTEM_TYPE=unknown ;;
esac


case "$Medium" in
    tape)	do_tape ;;
    tape@*)	get_rhost; check_rhost; do_tape ;;
    cdrom)	do_cdrom ;;
    *)	fatal "This command received an invalid argument or no argument was given." "$usage" ;;
esac

[ $? -eq 0 ] || fatal "Errors extracting or installing modules from specified media"
