#! /bin/sh
#ifdef __USAGE
#%C    - install a software package
#
#%C    [-u update.[tar.F|tar.gz|.tgz] | drive] [files...]
#Where:
#    -u update  unpack and install the file 'update'
#               
#    drive      is the device the software package is being installed from.
#               (default is /dev/fd0)
#    files      A pattern in the standard shell pattern-matching notation.
#               (default is *, which selects all files in the archive)
#Examples:
#    install
#        -installs the software package from /dev/fd0
#    install -u 930204.tar.F
#        -unpacks and installs the update 930204.tar.F
#    install -u 930204.tar.gz
#        -unpacks and installs the update 930204.tar.gz
#endif
#
# This general-purpose install script is intended for 
# installing and licensing any software package supplied 
# by QNX Software Systems for the QNX 4 Operating system.
#
# The script prompts the user for the first disk of the software 
# package, licenses the product (if necessary) for your system, 
# then installs the package. 
# If the software package has a 'setup' script, this script is copied 
# to '/tmp', executed, and then removed.
#
umask 022

getresp() {
	read resp
	if [ "X$resp" = "X" ] ; then
		resp=$1
	fi
}

drive=/dev/fd0
update=
package=
flags=
case $# in
    0)    ;;
    *)    case $1 in
   	      -u) update=$2; shift ; shift ;;
           *) drive=$1; shift ;;
   	  esac
esac

# check if cwd is "/"
if test "$(fullpath -t .)" != "$(fullpath -t /)" ; then
	echo ""
	echo "Your current working directory is '$(fullpath -t .)'."
	echo "All QNX Software Systems Ltd products should be installed under '/'."
	echo ""
	echo 'Do you wish to continue anyway? (y/n) '

	ans=""
	while [ "$ans" = "" ]; do
		getresp
		case $resp in
			y*|Y*)
				ans=yes
				;;

			n*|N*)
				echo ""
		        echo "The current working directory is not '/'."
				echo "install: aborted"
				exit 1
				;;

			*)
				echo 'Do you wish to continue anyway? (y/n) '
				;;
		esac
	done
fi

# check if user is "super"
if [ 0 != $(/usr/bin/id -u) ] ; then
	echo ""
	echo "You are about to install this archive with a userid of '$(/usr/bin/id -u -n)'.  All QNX"
	echo "Software Systems Ltd products should be installed as '$(/usr/bin/id -u -n 0)'."
	echo ""
	echo 'Do you wish to continue anyway? (y/n) '

	ans=""
	while [ "$ans" = "" ]; do
		getresp
		case $resp in
			y*|Y*)
				ans=yes
				;;

			n*|N*)
				echo ""
		        echo "The user id is not '$(/usr/bin/id -u -n 0)'."
				echo "install: aborted"
				exit 1
				;;

			*)
				echo 'Do you wish to continue anyway? (y/n) '
				;;
		esac
	done
fi

if test -z "$update"
then
    if test ! -r  $drive; then
        echo ""
        echo "Can't open $drive for reading."
        echo "install: aborted"
        echo ""
        exit 1;
    fi

    echo ""
    echo "The software package will be installed from:  $drive"
    echo ""
    read ans?'Please insert disk 1 and press <Enter>'
    echo ""
    echo "Invoking the license utility..."
    echo ""

    license -q $drive

    echo "Restoring the archive..."
    echo ""

    if hd -n2 -s1024 $drive | grep -q "1f 8b"
    then
    	dd if=$drive skip=2 | gunzip | pax -rv -s,install_msg,/dev/tty, -s,^setup$,/tmp/setup, $*
    elif hd -n5 -s1281 $drive | grep -q "75 73 74 61 72"
    then
    	dd if=$drive skip=2 | pax -rv -s,install_msg,/dev/tty, -s,^setup$,/tmp/setup, $*
    else
    	dd if=$drive skip=2 | melt | pax -rv -s,install_msg,/dev/tty, -s,^setup$,/tmp/setup, $*
    fi

    echo ""
    echo "Refreshing licensing information"
    echo ""

    license -r

    if test -x /tmp/setup; then
        echo "Running setup script..."
        /tmp/setup $drive
        rm -f /tmp/setup
    fi
else
    echo "Restoring the archive..."
    echo ""
    # get archive type 
    file_ext=`echo $update | awk -F "." '{print $NF;}'`

    # check for installing from QNX Package CD
    arc="`fullpath -t $update`"
    base="`basename $arc`"
    dir="`dirname $arc`"

    if test -r "$dir/../.install_info" -a -r "$dir/../.cd.information."
    then
	# check if archive is encrypted
	if test "$file_ext" = "X"; then
	echo "This is an encrypted archive. It can only be installed by"
       		echo "using the Photon utility \"pkginstall\"." 
		exit 1
	fi
	# check for Pre-setup script in products file
	flags="`grep $base $dir/../.install_info/products | cut -d"," -f 1`"
	if test `echo $flags | grep "S"`
	then
		"`echo $arc | sed -e"s/.tar.F//" -e "s/.tar//" -e "s/.tar.gz//"`".setup
	fi
    fi
    case $file_ext in
	tar) pax -f $update -rv -s,install_msg,/dev/tty, -s,^setup$,/tmp/setup, $* ;;
	gz|tgz) zcat $update | pax -rv -s,install_msg,/dev/tty, -s,^setup$,/tmp/setup, $* ;;
	F|*) fcat $update | pax -rv -s,install_msg,/dev/tty, -s,^setup$,/tmp/setup, $* ;;
    esac

    let RET=$?
    if test -x /tmp/setup; then
        echo "Running setup script..."
        /tmp/setup $drive
        rm -f /tmp/setup
    fi

    # If Installing the OS archive from the CD then run install_setup
    # from the floppy.

    if test `echo $flags | grep "O"`
    then
		if test -r "/fd0/bin/install_setup"
		then
			/fd0/bin/install_setup
		fi
    fi

fi

echo ""
echo "Installation complete"
echo ""

exit $RET

