#!/bin/sh 
# PRODUCT_NAME: Data Explorer Visualization Software 
#
# (C) COPYRIGHT International Business Machines Corp. 1992-1997
# All Rights Reserved
# Licensed Material - Property of IBM
#
# FUNCTION: install script for NetLS that comes with Data Explorer 
#           USE ONLY IF THE NetLS THAT COMES WITH YOUR OPERATING SYSTEM
#           IS NOT AVAILABLE OR DOES NOT WORK WITH Data Explorer.
#
# USAGE : install_lsk [ -sun4 | -hp700 | -sgi | -aviion | -ibm6000 ] 
#		       [ <tar file > ]
#
# Determination of the current architecture is done automatically
# unless one of the above architectures is provided on the command line. 
#
# This script will work for both tape and CD installations.  It asks
# the user for the installation directory, tape device (if tape installation),
# whether or not this is a personal install, and the subsystems that are
# desired for de/installation. 
#
# The script assumes it is installing from CD if its name is some flavor
# of 'install_lsk', otherwise a tape install is assumed.
# When installing from CD, the following is assumed
#
# 	1) the CD is mounted (on for example /mnt)
# 	2) this script is found in /mnt/install_lsk (it may appear as 
#		uppercase and may have a ';1' as a suffix)
#	3) the user does a 'cd /mnt; ./install_lsk' to run the script.
# 
# When installing from a tar file the following are assumed
#
#	1) The tar file is a relative tar file with files tar'd from the 
#	    	directory above the netls directory, so that all netls 
#		files appear in the tar file as netls/$arch and 
#	2) If from a tar tape, then the relevant tar file is determined
#		based on the architecture. 
#
#


dbg=${DXDBG-off}
ECHO=
superuser=root
pwd=`pwd`
# Order of architecture specific files on tape
# Order of files on tape with position of an architecture name
# indicating the position of the architecture specific file on the tape
# boot and common are the first two tar files on the tape
# NOTE: in the case of 1/4" tape the solaris and sun4 compressed images
#       are on the 2nd tape.
headerfiles="boot common"
tape_map="$headerfiles alphax aviion hp700 ibm6000 sgi solaris sun4"
qic150files=7   # The # of files on the 1st 1/4" tape, the 2nd tape has the rest
                # Note that the 8mm should have all files on a single tape

if [ "$dbg" = "on" ]; then
    ECHO=echo
fi

arch=				# is optional, but allows override of 
				# auto determination of architecture 
suffix=
single=false
lscase=lower
media=${DXMEDIA-tape}
tarfile=
whoami=`id | sed -e 's/[^(]*(\([^)]*\).*/\1/'`
cat=cat				# cat or zcat

################################################################################
#################### Miscelaneous Helper routines ##############################
################################################################################
#
# Routine to ask user if we should proceed or quit.
#
ASK_ANSWER=
ask()
{
    ASK_ANSWER=
    while [ -z "$ASK_ANSWER"  ]; do
        echo ""
        question="$1 ? (y/n) : "
        /bin/csh -f -c "echo -n \"$question\""
        read ans
        case $ans in
            y|Y) ASK_ANSWER=yes;;
            n|N) ASK_ANSWER=no ;;
            *) echo "Please answer with 'y' or 'n'." 2>&1
                 ASK_ANSWER=
            ;;
        esac
    done
}
#
# Routine to ask user if we should proceed or quit.
#
ask_proceed()
{
    ask "Proceed"
    if [ $ASK_ANSWER = yes ]; then
        echo "Proceeding..."
    else
        echo "Installation terminated."
        exit 0
    fi
}
get_with_default()
{
   question="$1 [$2] : "
   /bin/csh -f  -c "echo -n \"$question\""
   read ASK_ANSWER
   if [ -z "$ASK_ANSWER" ]; then
        ASK_ANSWER=$2
   fi
}

#
# A routine to create a directory path.  For example, given /a/b/c/d/e/f,
# each directory will be created so that there is a path to directory f.
#
mkpath()
{
        d=
        dirs=`echo $1 | tr '/' ' '`
        for k in $dirs; do
            d="$d/$k"
            if [ ! -d $d ]; then
                $ECHO mkdir $d
            fi
        done
}
#
# Skip the rewindable tape device ahead one file
# $1 is architecture, $2 is device
#
fsf_tape()
{
    a=$1
    drive=$2
    files=$3
    if [ $files != 0 ]; then
        case $a in
            ibm6000)                    cmd="tctl -f";;
            sgi|hp700)                  cmd="mt -t";;
            solaris|alphax|sun4|aviion) cmd="mt -f";;
        esac
        echo "Skipping forward $files file(s) on $drive..."
        $ECHO $cmd $drive fsf $files
    fi
}

#
# Rewind the tape device
# $1 is architecture, $2 is device
#
rew_tape()
{
    a=$1
    drive=$2
    case $a in
        ibm6000)                        cmd="tctl -f";;
        sgi|hp700)          	        cmd="mt -t";;
        solaris|sun4|alphax|aviion)     cmd="mt -f";;
    esac
    echo Rewinding tape drive $drive...
    $ECHO $cmd $drive rewind
}
find_drive()
{
   # FIXME: determine these dynamically
   case $1 in
        ibm6000)        echo /dev/rmt0.1;;
        sun4)           echo /dev/nrst0;;
        solaris)        echo /dev/rmt/0n;;
        hp700)          echo /dev/rmt/0mn;;
        sgi*)           echo /dev/rmt/tps0d4nrns;;
        aviion)         echo /dev/rmt/0n;;
        alphax)         echo /dev/nrmt0h;;
   esac
}


################################################################################
######################### Begin CD/Tape Installation ##########################
################################################################################

if [ $media = CD ]; then
    # Determine the format of the names as listed on the CD. Names can
    # be in all caps and may have a trailing version number ';1'.
    capname=`echo $0 | tr '[a-z]' '[A-Z]'`
    if [ $0 = $capname -o $0 = ./$capname ]; then
        lscase=upper
    fi
    x=`echo $0 | grep ';'`
    if [ ! -z "$x" ]; then 
        suffix=';1'
    fi
fi

#
# Determine the architecture that the script is running on
#
if [ -z "$thisarch" ]; then
    # determine the machine type automatically
    thissys=`uname -s`
    case $thissys in
        AIX*)   thisarch=ibm6000 ;;
        IRIX*)  thisarch=sgi     ;;
        HP-UX*) thisarch=hp700   ;;
        dgux*)  thisarch=aviion  ;;
        OSF1*)  thisarch=alphax  ;;

        SunOS*)
            thisver=`/bin/uname  -r`
            case "$thisver" in
               4*) thisarch=sun4    ;;
               *)  thisarch=solaris ;;
            esac
        ;;

        *)
        cat << +++
    Your machine type (uname -s) is not recognized.
    This installation program is for use only on RS/6000, Sun, HP, SGI,
    DEC, or AViION workstations.
+++
        exit
        ;;
    esac
fi


#
# Parse the arguments
#
while [ ! "$*" = "" ]; do
    case $1 in
	-ibm6000)	arch=ibm6000 ;;
	-hp700)		arch=hp700 ;;
	-sun4)		arch=sun4 ;;
	-solaris)	arch=solaris;;
	-alphax)	arch=alphax;;
	-aviion)	arch=aviion ;;
        -sgi)   	arch=sgi ;;
	-single)	single=true;; # The tape has only one arch file on it
	-archmap)	tape_map="$headerfiles $2"; shift;;
        *)
		if [ -z "$tarfile" ]; then
		    tarfile=$1
		else 
		    echo Only one tarfile is allowed 
		    exit 1
		fi
        ;;
        *)
    esac
    shift
done


#
# Check the architecture 
#
if [ -z "$arch" ]; then
    arch=$thisarch;
fi

if [ $whoami != $superuser ]; then
    cat << +++

You can extract the installation kit without being root, but
you will have to be root to do the actual installation of the
license server software.

+++

fi
#
# Find out what tape drive or file to use if this is a tar installation 
#
if [ $media != CD ]; then
	if [ ! -z "$tarfile" -a ! -r "$tarfile" ]; then
	    echo Can not find $tarfile
	    tarfile=
	fi
	while [ -z "$tarfile" ]; do 
	    cat << +++

Enter the name of the tape device (or file) that contains the
DX installation tar file.  If entering a tape device, the device must
be not be auto-rewinding! For example,

        On AIX............../dev/rmt0.1
        On SunOS............/dev/nrst0
        On Solaris........../dev/rmt/0n
        On HP-UX............/dev/rmt/0mn
        On IRIX............./dev/rmt/tps0d4nrns
        On DG/UX............/dev/rmt/0n
        On OSF1............./dev/nrmt0h


Note: On IRIX, be sure to use the no-swap tape device (ns).

+++
            drive=`find_drive $thisarch`
            get_with_default "Enter device (or tar file name)" "$drive"
            tarfile=$ASK_ANSWER
	    if [ ! -r "$tarfile" ]; then
		echo Can not find $tarfile
		tarfile=
	    fi
	done
        x=`echo $tarfile | grep '^/dev'`
        if [ ! -z "$x" ]; then
            media=tape
	    cat=zcat    # As of 2.1 we compress #2 and #3 files to fit on 1/4" tape
        else
            media=disk
            x=`echo $tarfile | grep '\.Z$'`
            if [ ! -z "$x" ]; then
                # Tar file has .Z suffix and so is compressed
                cat=zcat
            fi
            x=`echo $tarfile | grep '^/'`
            if [ -z "$x" ]; then
                tarfile="$pwd/$tarfile"
            fi
        fi
else
	tarfile=tarfiles/$arch.tar$suffix
        if [ $lscase = upper ]; then
            tarfile=`echo "$tarfile" | tr '[a-z]' [A-Z]'`
        fi
        tarfile="$pwd/$tarfile"
        if [ ! -f "$tarfile" ]; then
            cat << +++

Can not find the following file:

$tarfile

This is probably due to improper mounting of the CD-ROM.
Please refer to the installation instructions provided with the
CD-ROM for instructions on how to properly access the CD-ROM.

+++
            exit 2;
        fi

fi

#
# Find out the directory to which we should install the license server.
#
tmpdir=
while [ "$tmpdir" = "" ]; do

    cat << +++

Enter the name of a temporary directory to which the NetLS license server
installation kit is to be installed (5-10 Mbytes may be required). 

+++
    get_with_default "Enter directory" "/tmp"
    tmpdir=$ASK_ANSWER
    if [ ! -w $tmpdir ]; then
	cat << +++

The $tmpdir directory is not writable.

+++
	tmpdir=
    fi
done



cat << +++

Installation script is configured to install NetLS
license server installation kit in $tmpdir/netls/$arch
Installation will read files from $tarfile.

+++

ask_proceed

if [ $media = tape ]; then
    echo This may take several minutes.
    rew_tape $thisarch $tarfile
    if [ $single != true ]; then
	#
	# Find out how many files to skip
	#
	filenum=1
	for i in $tape_map; do
	    if [ $i = $arch ]; then
		 break
	    fi
	    filenum=`expr $filenum + 1`
	done
	firsttape=true
	if [ $filenum -gt $qic150files ]; then
	    ask 'Is this a 1/4 inch (QIC-150) tape'
	    if [ $ASK_ANSWER = "yes" ]; then
		firsttape=false
	    fi     
	fi
	if [ $firsttape != true ]; then
	    rew_tape $thisarch $tarsrc1
	    echo 'Please replace tape #1 with tape #2 in the drive.'
	    ASK_ANSWER=no
	    while [ "$ASK_ANSWER" != "yes" ]; do
		ask 'Is tape #2 in the tape drive'
	    done
	    rew_tape $thisarch $tarsrc1
	    filenum=`expr $filenum - $qic150files`
	fi
	#
	# Unload the architecture specific files
	#
	skips=`expr $filenum - 1`
	fsf_tape $thisarch $tarfile $skips
	sleep 5		# Wait for tape drive to recover
    else
	cat=cat
    fi
fi

files="netls/$arch"
if [ "$ECHO" = "" ]; then
    # Use dd because IRIX sometimes coughs on the tape itself.
    (cd $tmpdir; dd if="$tarfile" bs=4k | $cat | tar xvf - $files)
    status=$?
else
    $ECHO "(cd $tmpdir;  dd if=$tarfile bs=4k | $cat | tar xvf - $files)"
    status=$?
fi

if [ $status != 0 ]; then
    cat << +++
'tar xvf ...' did not complete successfully and returned error code $status.
+++
    exit $status
fi

cat << +++

Extraction of the NetLS license server installation kit was successful.
Look at the Readme file in $tmpdir/netls/$arch for more information on
how to proceed.  Once installation is complete you can remove the
$tmpdir/netls directory.

+++

exit 0

