#!/bin/sh 
#
#   Description: HP X Station generic installation script  
#


PATH=/sbin:/bin:/usr/bin:/etc:/usr/etc:/usr/local/bin:/usr/ucb
export PATH

set -a
set -u


trap finish  2  # cntrl-c handler
trap ""   23


GetYN ()
{
  echo "$1"
  while true
  do
    read  "reply" 
    case $reply in
     y|yes) return 1
           ;;
     n|no)  return 0
           ;;
     *)    echo ' Please answer y or n '
           ;;
    esac
  done
}

finish()
{
    echo 
    echo 
    echo  "Aborting Installation.."
    echo 
    rm -rf $TMP/HPXT-*
    exit 0
}

userInput()
{
    echo "$1: $NL" 1>&2
    read x
    echo $x
}

IsSelected()
{

   SEL_FS=$1
   THIS_FS=$2

   if [ "X$SEL_FS" = "X" ]
   then 
       return 1
   else
       echo $SEL_FS |  \
       egrep  "(^| )$THIS_FS($| )" > /dev/null   
       return $?  #       SELECTED_FILESETS="$SELECTED_FILESETS HPXT-ASIANFONTS"
   fi
    
} 

#
# Checks the size requirements for chosen filesets 
# gives user the opportunity to bail out if short in disk space
#

CheckSizeReq()
{

   S_F=$1
   I_D=$2 
   req=0
   t=0

   echo "Checking size requirements, this may take a while.....$NL"
     ( cd $TMP
     for fileset in $SELECTED_FILESETS
     do
            tar xf $TAPEDEV $fileset.sz 
            if [ -s $fileset.sz ]
            then
               t=`cat $fileset.sz`
               req=`expr $req + $t` 
               rm -f $fileset.sz
            fi
           
     done

     echo 
     echo Disk space required to install chosen filesets is $req KBytes
     GetYN "Continue with Installation ?(y/n)"
     if [ $? -eq 0 ] 
     then
        return 1   
     else
       return 0 
     fi
  
    ) 

}

#
# script starts from here 
#

#
# if uid is 0, eg we are the super user, fine
# else exit with message
#

user_id=`id | awk -F'(' '{print $1}' | awk -F'=' '{print $2}'`

if [ "$user_id" != "0" ]
then
  echo "You must be super-user to run the installation script."
  exit 0
fi

PWD=`/bin/pwd`
TMP=${TMP:-/tmp}

#################################################
#
# SAVE OLD UMASK AND SET OWN
#
#################################################

OLDUMASK=`umask`
umask 022

###################################
#  What is the Architecture?
###################################

if [ -x /bin/sun ]
then
  if /bin/sun
  then
    OS=sun4
    if [ `uname -r | cut -c1` = "5" ]
    then
      OS=sun5
    fi
  fi
else
  OS=`uname -m`
  if [ $OS = "i386" -o $OS = "i486" ]
  then
    if [ `uname` = "Linux" ]
    then
       OS=Linux
    else
       OS=sco
    fi
  else
    OS=`uname`
  fi
fi

if [ "$OS" = "Linux"   ]
then
     NL=""
else 
    NL="\c"
fi

# 
#

SELECTED_FILESETS=""
INSTALLDIR=${INSTALLDIR:-/}
TAPEDEV=${TAPEDEVICE:-$PWD/generic.img}
QUIT="n"



#################################################
#
# Check usage and GET TAPE DEVICE
#
#################################################

# Check if tape device was provided as $1
# Check if device exists

case $# in
0)	
        set +u
        echo $0 | egrep '(^/)|([.]/)' > /dev/null
	if [ $? -eq 0 ]
	then
          TAPEDEV="`dirname $0`/generic.img"
          echo $TAPEDEV | egrep '[.]/' > /dev/null
	  if [ $? -eq 0 ]
	  then
            TAPEDEV=$PWD/$TAPEDEV
          fi
        fi

	if [ ! -f "$TAPEDEV" ]
	then
	  TAPEDEV=$PWD/generic.img
        fi

	if [ ! -f "$TAPEDEV" ]
	then
	  TAPEDEV=/cdrom/generic.img
        fi

        set -u
	;;
1|2|3|4)
        
        set -- `getopt s:d: $*`
        if [ $? -ne 0 ]
        then
           echo wrong USAGE
           exit 2
        fi
     
        while [ $# -gt 0 ]
        do
            case $1 in
             -s)
                if [ ! -f $2 ]
                then
                  echo "Usage: $0 -s <generic install image> -d <destination dir>"
                  exit
                fi
                TAPEDEV=$2
                shift 2
                ;;
             -d)
                INSTALLDIR=$2
                shift 2
                ;;
             --)
                break
                ;;
              *)
                echo "Usage: $0 -s <generic install image> -d <destination dir>"
                exit
                ;;

            esac
        done 

	;;
*)
        echo "Usage: $0 -s <generic install image> -d <destination dir>"
	exit 1 
        ;;
esac

echo $TAPEDEV | egrep '^/' > /dev/null
if [ $? -ne 0 ]
then
  TAPEDEV=$PWD/$TAPEDEV
fi

if [ ! -f $TAPEDEV ]
then
  if [ -f $PWD/$TAPEDEV ]
  then
    TAPEDEV=$PWD/$TAPEDEV
  else
    if [ -f $PWD/`dirname $0`/generic.img ]
    then
      TAPEDEV=$PWD/`dirname $0`/generic.img
    fi
  fi
fi

if [ ! -f "$TAPEDEV" ]
then
  if [ ! -c $TAPEDEV ]
  then
    echo cannot find install image 
    echo "Usage: $0 -s <generic install image> -d <destination dir>"
    exit 1;
  fi
else
  echo Using image in $TAPEDEV
  echo Destination directory is $INSTALLDIR
fi

if [ -c $TAPEDEV ]
then
  mt -f $TAPEDEV rew
  if [ $? -ne 0 ]
  then
    echo "FATAL ERROR: Couldn't rewind tape." 
    exit 1
  fi

  mt -f $TAPEDEV fsf 1
  if [ $? -ne 0 ]
  then
    echo "FATAL ERROR: Couldn't forward to 2nd file of tape." 
    exit 1
  fi
fi

#cd $TMP
#echo untaring installation files ... may take several minutes to complete
#tar xvf $TAPEDEV packages.hpxt > /dev/null 2>&1
#if [ $? -ne 0 ]
#then
#  echo "FATAL ERROR: tar failed." 
#  exit 1
#fi



ALL_BASE="HPXT-ENTRIA     \
          HPXT-ENVIZEX    \
          HPXT-SUPPL      \
          HPXT-TOKN       \
          HPXT-VG"               

ALL_BASE_EXPS="HPXT-ENTRIA|HPXT-ENVIZEX|HPXT-SUPPLY|HPXT-TOKN|HPXT-VG"
ALL_CLIENTS="HPXT-CDE      \
             HPXT-CLIENTS  \
             HPXT-R4CLNTS  \
             HPXT-DYNKEY   \
             HPXT-XLOCK    \
             HPXT-XTOUCH   \
             HPXT-VT320    \
             HPXT-MPEG    \
             HPXT-XCLOCK"

# This is  tobe used in egrep regular expression
ALL_CLIENTS_EXP="HPXT-CDE|HPXT-CLIENTS|HPXT-R4CLNTS|HPXT-DYNKEY|HPXT-XLOCK|HPXT-VT320|HPXT-XLOCK"

ALL_EXTS="HPXT-AUDIO      \
          HPXT-FLOPPY     \
          HPXT-FORMAT     \
          HPXT-SCANNER    \
          HPXT-NFS"

ALL_EXTS_EXP="HPXT-AUDIO|HPXT-FLOPPY|HPXT-FORMAT|HPXT-SCANNER|HPXT-NFS"

ALL_FONTS="HPXT-ASIAN    \
           HPXT-HP8FONTS      \
	   HPXT-ISOFONTS      \
           HPXT-MISCFONT"

ALL_FONTS_EXP="HPXT-ASIAN|HPXT-HP8FONTS|HPXT-ISOFONTS|HPXT-MISCFONT"

ALL_DOCS="HPXT-DOC"         
          

ALL_DOCS_EXP="HPXT-DOC"

ALL_MISC="HPXT-SOURCES" 
          


ALL_MISC_EXP="HPXT-SOURCES"

EVERYTHING="$ALL_BASE $ALL_CLIENTS $ALL_EXTS $ALL_FONTS $ALL_DOCS $ALL_MISC"
# Check the existence of INSTALLDIR

if [ ! -d $INSTALLDIR ]
then 
   GetYN "$INSTALLDIR doesn't exist. Create it(y/n)"
   if [ $? -eq 1 ] 
   then 
      mkdir -p $INSTALLDIR 2>&1 >/dev/null
      if [ $? -ne 0 ]
      then 
         echo "Unable to create directory \"$INSTALLDIR\"."
         echo "Cannot continue.. Exiting"
         exit 0
      fi
   else
      echo "Cannot continue.. Exiting"
      exit 0
    fi
fi
while [ 0 ]
do
	cat << EOF
    __________________________________________________
        HP ENWARE X Station Software Installation
    __________________________________________________
                      
    List of filesets  available for installation 
    
    Servers:                  Clients:
      1) HPXT-ENTRIA            14) HPXT-CDE 
      2) HPXT-ENVIZEX           15) HPXT-CLIENTS
      3) HPXT-SUPPL             16) HPXT-R4CLNTS
      4) HPXT-TOKN              17) HPXT-DYNKEY
      5) HPXT-100VG             18) HPXT-XLOCK
      6) All of above           19) HPXT-XTOUCH
                                20) HPXT-VT320
                                21) HPXT-XCLOCK
                                22) HPXT-MPEG
                                23) All of the above clients

    Extensions:                Fonts:
      7) HPXT-AUDIO             24) HPXT-ASIAN
      8) HPXT-FLOPPY            25) HPXT-HP8
      9) HPXT-FORMAT            26) HPXT-ISO
     10) HPXT-SCANNER           27) HPXT-MISC
     11) HPXT-NFS               28) All of the above fonts
     12) All Extensions

    Documentation:             Miscellaneous:
     13) HPXT-DOC               29) HPXT-SOURCES
                                
       
                       x|q) exit
EOF

    echo "Please enter fileset(s) to be installed (default=all)   :$NL"
    read LIST
    
    if [ "X$LIST" = "X" -o "$LIST" = "all" -o "$LIST" = "ALL"   ] 
    then
       LIST=99
    fi
    
    for SEL in $LIST
    do
       case $SEL in 
       1)
	   IsSelected "$SELECTED_FILESETS" HPXT-ENTRIA 
	   if [ $? -eq 1 ]
           then
               SELECTED_FILESETS="$SELECTED_FILESETS HPXT-ENTRIA"
           fi
           ;;
       2) 
           IsSelected "$SELECTED_FILESETS" HPXT-ENVIZEX 
	   if [ $? -eq 1 ]
           then
               SELECTED_FILESETS="$SELECTED_FILESETS HPXT-ENVIZEX"
           fi
           ;;
       3) 
           IsSelected "$SELECTED_FILESETS" HPXT-SUPPL 
	   if [ $? -eq 1 ]
           then
              SELECTED_FILESETS="$SELECTED_FILESETS HPXT-SUPPL"
           fi
           ;;
       4)  
           IsSelected "$SELECTED_FILESETS" HPXT-TOKN 
	   if [ $? -eq 1 ]
           then
               SELECTED_FILESETS="$SELECTED_FILESETS HPXT-TOKN"
	   fi
           ;;
       5)
	   IsSelected "$SELECTED_FILESETS" HPXT-VG 
	   if [ $? -eq 1 ]
           then
               SELECTED_FILESETS="$SELECTED_FILESETS HPXT-VG"
           fi
           ;;

       
       6) 
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )($ALL_BASE_EXPS)($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS $ALL_BASE"
           #####SELECTED_FILESETS="$SELECTED_FILESETS $ALL_BASE"
           ;;

       7)
           IsSelected "$SELECTED_FILESETS" HPXT-AUDIO
	   if [ $? -eq 1 ]
           then
               SELECTED_FILESETS="$SELECTED_FILESETS HPXT-AUDIO"
           fi
           ;;
       8)
	   IsSelected "$SELECTED_FILESETS" HPXT-FLOPPY 
	   if [ $? -eq 1 ]
           then
               SELECTED_FILESETS="$SELECTED_FILESETS HPXT-FLOPPY"
           fi
           ;;
       9)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-FORMAT($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-FORMAT"

	   SELECTED_FILESETS="$SELECTED_FILESETS HPXT-FORMAT"
           ;;
       10)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-SCANNER($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-SCANNER"

           SELECTED_FILESETS="$SELECTED_FILESETS HPXT-SCANNER"
           ;;
       11)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-NFS($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-NFS"
           ;; 
       12)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )($ALL_EXTS_EXP)($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS $ALL_EXTS"
           ;; 

       13)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-DOC($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-DOC"
           ;; 
       14)
	  
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPX-CDE($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-CDE"
           ;; 
       15)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-CLIENTS($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-CLIENTS"
           ;; 
       16)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-R4CLNTS($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-R4CLNTS"
           ;; 
       17)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-DYNKEY($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-DYNKEY"
           ;; 

       18)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-XLOCK($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-XLOCK"
           ;; 

       19)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-XTOUCH($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-XTOUCH"
           ;; 

       20)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-VT320($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-VT320"
           ;; 
       21)
	  
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-XLOCK($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-XLOCK"
           ;; 
       22)
	  
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-MPEG($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-MPEG"
           ;; 

       23)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )($ALL_CLIENTS_EXP)($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS $ALL_CLIENTS"
           ;; 

       24)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-ASIAN($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-ASIAN"
           ;; 

       25)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-HP8($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-HP8FONTS"
           ;; 

       26)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-ISO($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-ISOFONTS"
           ;; 

       27)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-MISC($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-MISCFONT"
           ;; 
       28)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )($ALL_FONTS_EXP)($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS $ALL_FONTS"
           ;; 

       29)
	   echo $SELECTED_FILESETS |  \
                 egrep -v "(^| )HPXT-SOURCES($| )" > /dev/null && \
                  SELECTED_FILESETS="$SELECTED_FILESETS HPXT-SOURCES"
           ;; 
       
       99) 
           SELECTED_FILESETS="$EVERYTHING"
           ;;
       x|X|q|Q)
           QUIT="y"
           break
           ;;
       *)
           echo 
           echo
           echo "Invalid Selection $SEL (Try again)"
           cat << EOF
_____________________________________________________________
   Please enter the index number(s) separated by space 
   corresponding to the fileset(s) you wish to install.
____________________________________________________________

EOF
           echo
           echo "Press any key to continue.."
           read jnk
           SELECTED_FILESETS=""
           continue
       ;;
       esac
    done

    if [ ! -z "$SELECTED_FILESETS" ]
    then
        echo "You chose the following filesets"
        for i in $SELECTED_FILESETS
        do
          echo "$i"
        done
   
        GetYN "Is this correct(y/n)"
        if [ $? -eq 1 ] 
        then 
            # 
            # untar   selected filesets
            # 
            #CheckSizeReq "$SELECTED_FILESETS" $INSTALLDIR
            #if [ $? -eq 0 ]
            #then
               for fileset in $SELECTED_FILESETS 
               do
                   echo "Installing Fileset $fileset .....$NL"
                   (cd $TMP ;  tar xf $TAPEDEV $fileset)

                   if [ -s $TMP/$fileset ]
                   then
                        (cd $INSTALLDIR ; tar xf $TMP/$fileset)
                         rm -rf $TMP/$fileset
                         echo "Done"
                   else
                        echo "Failed ( missing ?)"
                   fi
                done
             #fi
             QUIT="y"
          else
              SELECTED_FILESETS=""
          fi
      fi

    if [ "$QUIT" = "y" ]
    then 
      echo 
      echo 
      echo
      echo "Installation finished.    `date` "
      break
    fi
       
done
exit 0
