#!/bin/sh
#
# This script creates TEKXP_INST.LST
#

UserCmd="?"
UserList=

InstallDir=$1
DestDir=$2
PackageList=$3
PackageSizes=$4
ExtractMode=$5

InstallFileList=$InstallDir/TEKXP_INST.LST
InstallSizes=$InstallDir/tmpInstSz$$

#
#These should match entries in the TEKXP_PKGS.LST file
#

BOOTGRPS="xp10 xp330 xp350 pex"
COMMONGRPS="boot xpbinaries fonts doc man mgmt obsolete examples"
HOSTGRPS=" bin src" 
MAINGRPS=$COMMONGRPS$HOSTGRPS
ACTIVEGRP=
FIRSTTIME=
XP300=

ans=n

if [ $# -lt 3 ]
then
    echo "Usage: $0 [install directory] [package list] [file list]"
    exit 1
fi

SelectionList=$InstallDir/tmpSelLst$
Selection="Full"
	
while [ "$ans" -a "$ans" != "y" -a "$ans" != "Y" ]
do
    XP300=""

    if [ -z "$FIRSTTIME" ]
    then
	echo "Installation file selection is done by answering one or"
	echo "more yes/no questions. The default answer is 'y' or yes."
	echo "The first question will ask if you want to do a full install"
	echo "If you answer 'y' to this question, no more questions will"
	echo "be asked."
        if [ "$ExtractMode" = "tcio" ]
	then
	    echo ""
	    echo "Doing partial installations of a tcio tape can take an"
	    echo "extremely long time. This is because the tape has to be read"
	    echo "once for each subset of the tape being installed. For this"
	    echo "reason it is recommended that if space is not a problem the"
	    echo "entire tape should be installed."
	fi
	echo ""
    else
	echo "------ Beginning of Installation Questions -------"
    fi

    if [ "$PackageSizes" -a -f "$PackageSizes" ]
    then
	awk '{ if ($2 == "tekxp") print msg $1 " kilobytes";
	}' msg="Size of the entire installation is " $PackageSizes
    fi

    echo "" > $InstallFileList

    echo "Do you wish to do a full install (y/n) [y]"
    read ans

    if [ "$ans" -a "$ans" != "y" -a "$ans" != "Y" ]
    then	
	echo ""
        echo "" > $InstallFileList

 	if [ -z "$FIRSTTIME" ]
    	then
    	    echo "The xpbinaries and fonts sections will allow you to install"
	    echo "all, none, or any part of them. The other sections have "
	    echo "to be installed in their entirety."
	    echo ""
    	fi
        for grp in $MAINGRPS
        do	
	    MAJORGRP=""
    	    if [ "$ACTIVEGRP" = xpbinaries ]
    	    then
    	        for bgrp in $BOOTGRPS
    	        do
        	    if [ "$PackageSizes" -a -f "$PackageSizes" ]
            	    then
			awkmsg="Total size of all $bgrp files is "
			if [ "$bgrp" = "xp330" -o "$bgrp" = "xp350" ]
			then 
			    if [ "$bgrp" = "xp350" ]
			    then
			        awkmsg="Total size of all XP100 and XP350 files is "
			    fi
			    bgrp2="xp300"
			else
			    bgrp2="xxx"
			fi
			    
			awk '{ 
			      if (grp2 == "xp300" && index($2, grp2) > 0) 
				   size300 = $1
				
			      if (index($2, grp1) > 0 || index($2, grp2) > 0) 
				   total = total + $1
			     }
			     END { print msg " " total " kilobytes"
				if (grp2 == "xp300") {
				  print "This includes " size300 " kilobytes shared by XP330 and XP350"
				}
			     }' msg="$awkmsg" grp1=$bgrp grp2=$bgrp2 $PackageSizes
    	            fi
		    if [ "$bgrp" = "xp350" ]
		    then
    	                echo "....Do you wish to install the XP100 and XP350 files (y/n) [y]"
		    else
    	                echo "....Do you wish to install the $bgrp files (y/n) [y]"
		    fi
    	            read ans
    	            if [ -z "$ans" -o "$ans" = "y" -o "$ans" = "Y" ]
    	            then
			if [ "$bgrp" = "xp330" -o "$bgrp" = "xp350" ]
			then
			    XP300="true"
			    awk -f $InstallDir/inst_sets.awk grp="boot.xp300" mgrp="$MAJORGRP" $PackageList >> $InstallFileList
			fi
    		        awk -f $InstallDir/inst_sets.awk grp="boot.$bgrp" mgrp="$MAJORGRP" $PackageList >> $InstallFileList
			MAJORGRP=$bgrp
    	            fi
    	        done
  	        ACTIVEGRP=""
    	    fi
    	    if [ "$ACTIVEGRP" = fonts ]
    	    then
    	        for fgrp in $FONTGRPS
    	        do
        	    if [ "$PackageSizes" -a -f "$PackageSizes" ]
            	    then
			awk '{ if (index($2, grp) > 0) print msg $1 " kilobytes";
			}' msg="Total size of '$fgrp' directory is " grp=/$fgrp $PackageSizes
    	            fi
    	            echo "....Do you wish to install the $fgrp files (y/n) [y]"
    	            read ans
    	            if [ -z "$ans" -o "$ans" = "y" -o "$ans" = "Y" ]
    	            then
			awk -f $InstallDir/inst_sets.awk grp="fonts.$fgrp" mgrp="$MAJORGRP" $PackageList >> $InstallFileList
			MAJORGRP=$fgrp
    	            fi
    	        done
		ACTIVEGRP=""
    	    fi

            if [ -z "$ACTIVEGRP" ]
            then

    		case "$grp" in
            	  xpbinaries)
		    echo "------- tekxp/boot/(XP binaries) ------"
     	    	    if [ -z "$FIRSTTIME" ]
    	    	    then
    	        	echo "The 'xpbinaries' portion of the tape contain the "
    	        	echo "downloadable binaries for the XP10, XP100, XP330,"
			echo "XP350 and pex. Respond with an 'y if you want all,"
			echo "'n' if you want none, 'p' if you want a partial"
			echo "install. If a partial install is desired, you will" 
			echo "be asked whether you want a specific product's files"
			echo "installed."
			echo ""
    	    	    fi
        	    if [ "$PackageSizes" -a -f "$PackageSizes" ]
            	    then
			awk ' BEGIN {
			size = 0 }
			{ if (index($2,"boot_packages/") > 0 && index($2, "fonts") == 0) size = size + $1 }
			END {
			print msg size " kilobytes";
			}' msg="Total size of all XP binary files is " grp=tekxp/$grp $PackageSizes
    	    	    fi
    	    	    echo "Do you wish to install the boot files (y/n/p) [y]"
    	    	    read ans
    	    	    if [ -z "$ans" -o "$ans" = "y" -o "$ans" = "Y" ]
    	    	    then
    			awk -f $InstallDir/inst_sets.awk grp="boot." mgrp="$MAJORGRP" $PackageList >> $InstallFileList
    	    	    else 
			if [ "$ans" = "p" -o "$ans" = "P" ]
    	    	        then
    			    ACTIVEGRP=xpbinaries
		        else
			    ACTIVEGRP=
			fi
    	    	    fi;;

    	  	  fonts)
		    echo "------- tekxp/boot/fonts ------"
     	    	    if [ -z "$FIRSTTIME" ]
    	    	    then
    	        	echo "The fonts portion of the tape includes a"
    	        	echo "large number of fonts from which a subset"
    			echo "may be chosen. Respond with an 'y if you want"
			echo "all of the fonts, 'n' if you want none, 'p' if"
			echo "you want a partial installation. A partial "
			echo "installation will ask confirmation for the"
			echo "installation of each type of font."
			echo ""
    	    	    fi
        	    if [ "$PackageSizes" -a -f "$PackageSizes" ]
            	    then
			awk '{ if ($2 == grp) print msg $1 " kilobytes";
			}' msg="Total size of all font directories is " grp=tekxp/boot_packages/fonts $PackageSizes
    	    	    fi
    	    	    echo "Do you wish to install the font directories (y/n/p) [y]"
    	    	    read ans
    	    	    if [ -z "$ans" -o "$ans" = "y" -o "$ans" = "Y" ]
    	    	    then
    			awk -f $InstallDir/inst_sets.awk grp="fonts." mgrp="$MAJORGRP" $PackageList >> $InstallFileList
    	    	    else
			if [ "$ans" = "p" -o "$ans" = "P" ]
    	    	        then
    			    ACTIVEGRP=fonts
		        else
			    ACTIVEGRP=
			fi
    	    	    fi;;
    	  	  *)
		    if [ "$grp" = "Xie" ]
		    then
		        echo "------- tekxp/src/lib/$grp ------"
		    else
		        echo "------- tekxp/$grp ------"
		    fi
    	    	    if [ -z "$FIRSTTIME" ]
    	    	    then
    		        case $grp in
			  boot)
    		    	    echo "The 'boot' group contains files used to "
			    echo "configure the X-terminal, translation files"
			    echo "used by the local clients and a small number"
		 	    echo "of fonts."
			    echo "";;			
			  bin)
			    echo "The 'bin' directory contains host based"
			    echo "client and utility binaries. It is highly"
			    echo "recommended that this directory be installed"
			    echo "so that 'xpsh' will be available to execute"
			    echo "clients that run locally on the X-terminal"
			    echo "";;
    		  	  doc)
    		    	    echo "The 'doc' directory contains a PostScript"
    		    	    echo "version of the User's Manual"
			    echo "";;
    		  	  man)
    		    	    echo "The 'man' directory contains man pages for"
    		    	    echo "host based binaries and clients supplied on"
    		    	    echo "the installation tape."
			    echo "";;
			  mgmt)
			    echo "The 'mgmt' directory contains the MIB-II"
			    echo "file that can be used by various network"
			    echo "management programs to interact with "
			    echo "network and terminal specific objects"
			    echo "via SNMP"
			    echo "";;
    		  	  examples)
    		    	    echo "The 'examples' directory contains examples of"    
    		    	    echo "of an Xsession file, mwm key bindings, data_xp"    
    		    	    echo "source example, etc..."
			    echo "";;
    		  	  obsolete)
    		    	    echo "The 'obsolete' directory contains files that"
    		    	    echo "are no longer needed but are supplied for the"
    		    	    echo "purposes of backward compatibility"
			    echo "";;
    		  	  src)
    		    	    echo "The 'src' directory contains the sources for"
    		    	    echo "a number of useful clients and utilities."
			    echo "The libraries and include files needed to"
			    echo "build clients that use the X Imaging"
			    echo "Extension (XIE) are also in this group."
			    echo "";;
    			esac
    	    	    fi
        	    if [ "$PackageSizes" -a -f "$PackageSizes" ]
            	    then
                        if [ "$grp" = "Xie" ]
                        then
                            grpdir=src/lib/$grp
                        else
                            grpdir=$grp
                        fi
                        awk '{ if ($2 == grp) print msg $1 " kilobytes";
                        }' msg="Total size of all $grp files is " grp=tekxp/$grpdir $PackageSizes
    	    	    fi
    	    	    echo "Do you wish to install the $grp files (y/n) [y]"
    	    	    read ans
    	    	    if [ -z "$ans" -o "$ans" = "y" -o "$ans" = "Y" ]
    	    	    then
    			awk -f $InstallDir/inst_sets.awk grp="$grp" mgrp="$MAJORGRP" $PackageList >> $InstallFileList
    	    	    fi;;
    		esac
            fi
        done
	
	#
	# this picks up the mandatory files
	#
	awk -f $InstallDir/inst_sets.awk grp="*" mgrp="$MAJORGRP" $PackageList >> $InstallFileList

	#
	# Build a file that can be used to calculate the total install size
	#
	cat $InstallFileList > $InstallSizes	
	echo "END_FILE" >> $InstallSizes	
	cat $PackageSizes >> $InstallSizes	

	echo "-------------------------------------------"
	echo ""
	INSTALLFILES=`cat $InstallFileList`
	if [ "$INSTALLFILES" ]
	then
	    Selection="Partial"
            echo "These are the installation choices that you have made"
	    cat $InstallFileList
	    echo ""
	    awk -f $InstallDir/inst_size.awk $InstallSizes
	    echo ""
	    rm -f $InstallSizes	
            echo "Do you wish to do the installation (y/n) [y]"
        else
	    Selection=""
	    echo "No installation selections were made. Do you wish to"
	    echo "abort the installation (y/n) [y]"
	fi
        read ans
    fi
    FIRSTTIME="no"
   
done

#
# If a partial installation was desired, but no group was selected, exit
# with a non-zero
#
if [ -z "$Selection" ]
then
    exit 1
else 
    exit 0
fi



