#!/bin/sh
# Copyright 1993 by Human Designed Systems, Inc. all right reserved. 
#
# No part of this material may be reproduced or transmitted in any form
# or by any means, electronic, mechanical or otherwise, without prior 
# written permission from Human Designed Systems, Inc.
#
# author: Michael Gerner, Peter Choma
# date:   July 16, 1993         rev 1.0
#
# 05/18/94 gregh, Modified to prompt to install config directory
# 09/15/94 JHD, Modified for installation from CDROM
#

PATH=/bin:/usr/bin:/etc:/usr/ucb:/usr/bsd
TERMCAP=/etc/termcap
if [ -d /usr/5bin ]
then
        PATH=/usr/5bin:$PATH
fi
MB=30
export PATH TERMCAP 


cleanup() {
    /bin/rm -f  /tmp/sel.$$.sel /tmp/sel.$$.wrk
	exit 1
}

PFX=./hds-fx
SEL_FILE=/tmp/sel.$$.sel
WRK=/tmp/sel.$$.wrk

trap "cleanup" 1 2 3 15

#...insert tape contents here

cat <<EOF | grep -v '^#' > $SEL_FILE
##############################################################################
#
# Source lines which begin with "#" are treated as comments.
#
# Data Format is "KEY SIZE DIRECTORY LEGEND"
#
# KEYs are:
# 	Y - prompt with YES default
#	n - prompt with NO default
#	@[A-Z] - Section header - prompt with YES default
#	@[A-Z] - end a section - SIZE must be set to "-"
#	@[a-z] - Section header - prompt with NO default
#	@[a-z] - end a section - SIZE must be set to "-"
#	a - auto-include this directory
#
# SIZE is approximate size in K (1024 byte chunks).
#
# DIRECTORY is a fontape directory name (NO ./hds-fx prefix!)
#	e.g. fonts/Asterx and NOT ./hds-fx/fonts/Asterx
#
# LEGEND is the user prompt string
#
#
##############################################################################
Y 3259	image			Compressed Viewstation Server Software\\\n\
	 Yes if loading Server Software from network\\\n\
	 No if using Server from Flash Prom only
n 3674  image_u			Uncompressed Viewstation Server Software\\\n\
	 Yes for optional network loading of Server Software\\\n\
	 No if you load Compressed Viewstation Server Software	 	
#
n  1561	extensions		HDS Server Extensions ( optional )\\\n\
	 Example ( DEC-XTrap or Multi-Buffering )
# #	#		#
@F 8467	fonts			All HDS-X11 Fonts including Lotus 123, WP5.1, etc...\\\n\
	 ( Enter N for individual choices )
Y 1316	fonts/100dpi		HDS-X11 100dpi Fonts *required*
Y 1047	fonts/75dpi		HDS-X11 75dpi Fonts *required*
Y 2382	fonts/misc		HDS-X11 Misc Fonts *required*
n  104	fonts/Xol		Additional OpenWindows Xol Fonts ( optional )\\\n\
	 Y if using OPEN Look Window Manager or OPEN Look clients
n 1582	fonts/Asterx2.1		Additional Applix V2.1 Fonts ( optional )\\\n\
	 Y if using Aster*x; N if not
n 1535	fonts/Asterx3.0		Additional Applix V3.0 Fonts ( optional )\\\n\
	 Y if using Aster*x; N if not
n  185	fonts/wp5.1		Additional WordPerfect 5.1 Fonts ( optional )\\\n\
	 Y if using WordPerfect; N if not
n   21  fonts/lotus123		Additional Lotus 123 Fonts ( optional )\\\n\
	 Y if using Lotus 123; N if not
n  294  fonts/softpc		Additional Soft-PC Fonts ( optional )\\\n\
	 Y if using Soft-PC; N if not
@F    -	-			"*END* of fonts group"
#
@U 1931	utils			ALL HDS Host Utilities ( program source & executables )\\\n\
	 ( Enter N for individual choices )
@E 1701	utils/execs		All HDS Host Executable Utilities SUN, IBM, etc...\\\n\
	 ( Enter N for individual choices )
n  249	utils/execs/DG_AViiON	Data General AViiON executable files\\\n\
	 Y if host is DG; N if other host type
n  338	utils/execs/HP700	HP700 executable files\\\n\
	 Y if host is HP; N if other host type 
n   66	utils/execs/IBM_RS6000	IBM_RS6000 executable files\\\n\
	 Y if host is IBM; N if other host type
n  161	utils/execs/Sun_Solaris	SUN4 Solaris 2.x executable files\\\n\
	 Y if host is SUN running Solaris 2.x; N if other host type
n  527	utils/execs/SunOS4	SUN4 SunOS 4.x executable files\\\n\
	 Y if host is SUN running SunOS 4.x; N if other host type
n  174	utils/execs/VAX_ultrix	VAX_ultrix executable files\\\n\
	 Y if host is VMS; N if other host type
n  194	utils/execs/sco386	SCO UNIX executable files\\\n\
	 Y if host is Intel-SCO; N if other host type
@E    -	-			"*END* of nested execs group"
@S  220	utils/src		Source code for HDS Font utilities\\\n\
	 Program code to compile on your host system.\\\n\
	 ( Enter N for individual choices )
n   68	utils/src/bdfto960	bdfto960 source code
n   39	utils/src/getbdf	getbdf source code
n   10	utils/src/hdscrypt	hdscrypt source code
n   18	utils/src/hdsfc960	hdsfc960 source code
n   75	utils/src/hdsmkdir	hdsmkdir source code
n    8	utils/src/flashfilesys	flashfilesys source code
@S    -	-			"*END* of source group"
@U    -	-			"*END* of utilities group"
#
a    1  VERSION                 HDSware version
Y   18	rgb			Colormap data files
#
Y  137	config			Configuration files
Y   40	include			HDS include files
Y 4584  lib                     Libraries & app-defaults for HDSperx clients                                    I show real total size of the lib & bin  on the bin option 
#
Y  749	man			HDSperx local client manual pages\\\n\
	 HDS client man pages
#
n 6436	contrib			Contributed programs\\\n\
	 Example ( Bootp, Xscope, usepty, etc ) 
#
Y 2306	bin			HDS Viewstation ( HDSperx ) local clients
n 35000 doc                     HDS Viewstation documentation
#
# 	*** END_OF_FILE ***
EOF

#
#set -x
#

#-----------------------------------------------------------------------------#
# Functions area                                                              #
#-----------------------------------------------------------------------------#



pause() {
        echo "\nPress Return to Continue \c"
 	read return
}


bad_val() {
	echo "\n***ERROR: $select is not a valid choice.\007"
	pause
}


yesno2() {
        YN=no
        until [ $YN = yes ]
        do
	    echo  
	    echo "Ctrl-C to exit installation"
	    echo "N ( no ) to modify entry " 
	    echo "Y ( yes ) to continue the installation"
	    echo
	    echo "Please enter your response ( Y, N or Ctrl-C ): \c"	
            read yno </dev/tty
            case "$yno"
            in
                y|Y) YN=yes ;;
                n|N) return ;;
                *)  echo "\nPlease respond Y, N or Ctrl-C" >&2
                	pause ;;
            esac
        done
}

contin() {
        YN=no
        until [ $YN = yes ]
        do
            echo "Press 'Y' to continue, or 'N' to return to Main Menu: \c"
            read yno </dev/tty
            case "$yno"
            in
                y|Y) YN=yes ;;
                n|N) return ;;
                *)  echo "\nPlease respond Y, N or Ctrl-C" >&2
                        pause ;;
            esac
        done
}

retrnmm() {
        YN=no
        until [ $YN = yes ]
        do
	    echo
            echo "Ctrl-C to exit installation"
	    echo "N ( no ) to return to Main Menu"
	    echo "Y ( yes ) to continue with the installation"
	    echo
	    echo "Please enter your response ( Y, N or Ctrl-C ): \c"
            read yno </dev/tty
            case "$yno"
            in
                y|Y) YN=yes ;;
                n|N) return ;;
                *)  echo "\nPlease respond Y, N or Ctrl-C" >&2
                        pause ;;
            esac
        done
}

goodbye() {
	echo "

Your installation is complete. Check the directory location to verify
the contents of the installation. You may need to change permissions
for this directory so the files can be handled correctly.

Enter the HDSware Host IP address and the HDSware Directory location 
*$HDSDIR*, 
in the HDSware menu in the ViewStation's Setup Mode (or Monitor Mode) so 
the ViewStation can find and load the files. Refer to the HDS ViewStation 
Owner's Manual for details."
	echo
	pause
}


error() {
	echo
	echo "There was an error reading the HDSware distribution."
	echo "Please check your distribution media and try again"
	cleanup
	MM=y
}

selectdv() {
	case $0
	in
	    *[Ii][Nn][Ss][Tt][Aa][Ll][Ll].[Cc][Dd]*)
		DEVICE=CDROM
		;;
	    *)
		DEVICE=TAPE 
		;;
	esac
}

selecthd() {
    HD=no
    until [ $HD = yes ]
    do
	clear
	echo
	echo "Please enter the directory on this host where the HDSware" 
	echo "distribution should be installed. This path must be root based."
	echo
	echo "Note: This corresponds to the HDSware directory in setup mode."
	echo
	echo "Example ( /usr, or /usr/lib/X11, etc. ): \c"
	read HDSDIR
	echo "\nYou have selected the "$HDSDIR" directory" 
	yesno2		
		if [ "$YN" != yes ]; then continue; fi
		if [ -d "$HDSDIR" -a  -w "$HDSDIR" -a "$HDSDIR" != "" ]
			then
			export HDSDIR
		else
			echo "
	Error: either $HDSDIR does not exist, is not a directory,
	       or is not writable."
			pause
			continue
		fi
	HD=yes
    done
}			 


selecttd() {
    TP=no
    until [ $TP = yes ]
    do
	echo "\nPlease enter the name of the your tape device."
	echo "Example ( /dev/rst0, or /dev/rct0 ): \c"
	read TAPE
	echo "\nYou have selected "$TAPE" for your tape device"
	yesno2
		if [ "$YN" != yes ]; then continue; fi
		if [ -c "$TAPE" ]
		then 
			export TAPE
		else
			echo "\nUnable to locate "$TAPE" device \007"
			pause
			continue
		fi
	TP=yes
    done
}


# function for partial tape installation

do_append() {
    APPEND=1
    echo >>${WRK} "$PFX/$DIR"
    SUM=`expr $SUM + $SIZE`
}


do_prompt() {
    # arg#1 = [yes|no] default
    echo "Install: $LEGEND (${SIZE}k)  [$1]? \c"
    if [ "$YN" = "exit" ]; then
	return 0
    elif [ "$YN" = "auto" ]; then
	echo "auto_reply=$1"
	if [ "$1" = "yes" ]; then do_append; fi
	return 0
    elif [ "$YN" = "all" ]; then
	echo "all"
	do_append
	return 0
    fi
    read ans </dev/tty
    case `echo "$ans$1"` in
	Y*|y*) 
	    YN=yes
	    do_append
	    ;;
	auto*)
	    YN=auto
	    do_prompt $1
	    ;;
	all*)
	    YN=all
	    do_append
	    ;;
	exit*)
	    YN=exit
	    ;;
	*) YN=no ;;
    esac 

    return 0
}


partial() {
    echo
    echo "                ... HDSware Selective Installation ..."
    echo
    echo
    echo
    echo "      This choice prompts you to include or delete individual parts"
    echo "      of HDSware from your installation. There are suggestions with"
    echo "      each entry to help you save space."
    echo
    echo "      Reply with either 'Y' (yes) or 'N' (no) to each question."
    echo
    echo "      Pressing the Enter key will select the [default] entry shown."
    echo
    echo "      You may enter 'all' at any time for a complete installation."
    echo
	pause
	clear
    YN=
    SUM=0; export SUM
    /bin/rm -f $WRK
    touch $WRK

    exec  3<&0  <$SEL_FILE	# save current stdin and then redirect stdin

    while true
    do
	read KEY SIZE DIR LEGEND
	if [ $? -ne 0 ]; then
	    YN=exit
	    break	# exit from while loop
	fi
	APPEND=0
	case $KEY in
	Y|y) do_prompt yes ;;
	N|n) do_prompt no ;;
	A|a) do_append
	    # echo "this automatically includes $LEGEND (${SIZE}k)"
	    ;;
	@[A-Z]|@[a-z])
	    if [ "$SIZE" = "-" ]; then continue; fi # ignore group end
	    case $KEY in
	    	*[A-Z]) do_prompt yes ;;
	    	*[a-z]) do_prompt no ;;
	    esac
	    if [ $APPEND -ne 0 ]; then
		# flush thru to group end
	    	FLUSH=$KEY
		while read KEY SIZE DIR LEGEND
		do
		    if [ "$KEY" = "$FLUSH" ]; then break; fi
		done
	    fi
	    ;;
	esac

    done

    # 
    # NOTE:
    # We need to redirect the read input (stdin) to come from the 
    # $SEL_FILE data file. This can be done in several ways. Do NOT use 
    # the form:
    #
    # 		while <list> do <list> done <$SEL_FILE
    #
    # because 'sh' will force the "while" command into a child process and 
    # we will not be able to access the value of the SUM variable as 
    # defined by the child process. Instead use the sh "exec" command 
    # to redirect stdin.
    #
    #

    echo ""
    echo "Your selections require approximately ${SUM}k of disk space."
    echo ""

    #sort <$WRK | uniq >${WRK}.tar-inc

    exec 0<&3		# restore stdin

    pause
    #
    # Add code here to test for enought disk space
    # 
    #
}



#
# Test for term type
#

while [ "$TERM" = "" ]
do
	clear
	echo "			ERROR - TERM Variable Undefined \007 








	"
  	echo "\nPlease Enter Terminal Type. Example ( ANSI, VT100 ...)  "
	echo "\nEnter Type Here: \c"
  		read TERM
  		export TERM
	clear
done

# *********** INTRODUCTION ***********
#
clear
echo "

	    Welcome to the HDSware Installation script


This script helps you install the HDSware files for your HDS ViewStation 
The script lets you install the entire distribution or selected parts, with 
individual queries for your selections. Its operation is simple, with 
options displayed at each question.

The HDSware distribution contains many necessary things, including font files,
the server files, the downloadable clients ( HDSperx ), and so on. Some 
things are necessary and some are optional; the script will suggest some 
choices for you.

You will be asked for the type of distribution media, tape or CDROM.
If the distribution media is CDROM you will be asked for the directory on
which it is mounted. If the distribution media is a magnetic tape you will be
asked for the name of your tape device. You will be asked for a directory on 
your host for placing the HDSware files. You should also check that there is 
sufficient space on your host for these files (sizes are given). Consult with 
your system administrator if you have questions about those entries.
"
echo
pause


MM=n
until [ $MM = y ]
do
	clear
	echo "			


		Welcome to the HDSware Installation Program

	
			1. Complete Installation
			2. Selective Installation
			3. Exit Installation

		Type the number of your selection (1-3)
		and press the Enter key to continue.  \c"

    read select
    case $select 
    in
	1) 
	clear
	echo
	echo   "You have selected Complete Installation. This installs"
 	echo   "the entire contents of the HDSware distribution, and requires " 
	echo   "approximately $MB MB of disk space." 
	echo
	echo   "If you are not sure there is enough disk space, return"
	echo   "to Main Menu, Exit, then issue the 'df' command."
	echo   "This will show which filesystems have enough free space."
	echo
	echo

		contin
			if [ "$YN" = yes ]
			then
				selectdv
			        if [ $DEVICE = "CDROM" ] 
				then
					CDROMDIR=`pwd`
				else
					selecttd
				fi
				selecthd
				clear
        if [ $DEVICE = "TAPE" ]
	then
		echo "\nPlease insert HDSware tape into "$TAPE""
        		pause
			clear
	fi
	if [ $DEVICE = "TAPE" ]
	then
		echo
       		echo "About to install HDSware tape using device $TAPE"
		
	else
		echo
		echo "About to install HDSware from CDROM mounted at $CDROMDIR"
	fi
	echo "in the directory. $HDSDIR"
	echo
		retrnmm
                	if [ $YN = yes ]
                	then
                            cd $HDSDIR
			    if [ $DEVICE = "TAPE" ] 
			    then
                               dd if=$TAPE bs=64k | tar xvf - >/dev/null 2>&1
			    else
                         tar xf $CDROMDIR/*[Tt][Aa][Rr]* >/dev/null 2>&1
			    fi
			    res=$?
			    if [ "$res" != "0" ]
			    then
				error
			    fi
			    pause
				goodbye	
				cleanup
				MM=y
			fi
		fi
	   ;;

	2) 
		clear
		echo
		echo "You have chosen "Selective Installation." \c"
		echo
		retrnmm
		clear
			if [ $YN = yes ]
        		then
                                selectdv
				partial
                                if [ $DEVICE = "CDROM" ]
                                then
                                        CDROMDIR=`pwd`
                                else
                                        selecttd
				fi
				selecthd
				clear
        	if [ $DEVICE = "TAPE" ]
		then
			echo "\nPlease insert HDSware tape into "$TAPE""
        			pause
				clear
		fi
		if [ $DEVICE = "TAPE" ]
		then
			echo
       			echo "About to install HDSware tape using device $TAPE"
		else
			echo
			echo "About to install HDSware from CDROM mounted at $CDROMDIR"
		fi
		echo "in the directory. $HDSDIR"
		echo
		retrnmm
                        if [ $YN = yes ]
                        then
	echo
echo "
Reading from Selected list this may take a while, please wait..."
				cd $HDSDIR
			        if [ $DEVICE = "TAPE" ] 
			        then
                                   dd if=$TAPE bs=64k | tar xvf - `cat $WRK` >/dev/null 2>&1
			        else
                                   tar xf $CDROMDIR/*[Tt][Aa][Rr]* `cat $WRK` >/dev/null 2 >&1
				fi
				res=$?
				if [ "$res" != "0" ]
				then
					error
				fi
				pause
					goodbye
					cleanup
					MM=y
			fi		
			fi
	   ;;

	3) 
			cleanup
	   ;;

	*) bad_val
	   ;;  
    esac

done
