#!/bin/sh
# versions	Perform various version related administrative tasks 
#

XTHOME=/usr/lib/X11/700X
ENVVARS700X=/usr/bin/X11/700X/admin/env.vars
if [ -f $ENVVARS700X ]
then
  case "$OS" in
    sun*) XTHOME="`egrep '^TOPDIR=' $ENVVARS700X | cut -f 2 -d'='`$XTHOME" ;;
    *)    XTHOME=`egrep '^XTHOME=' $ENVVARS700X | cut -f 2 -d'='` ;;
  esac
fi
XTHOME2=/opt/hpxt/enware2/xthome
ENWARE_THERE="false"
set -u

while [ 0 ] 
do
  REALXTHOME2=`(cd $XTHOME2;pwd)`
  BASEXTHOME2=`basename $REALXTHOME2`
  ACTIVE7=0
  ACTIVE8=0
	cat <<EOF





	_________________________________________________
		HP NETSTATION ADMINISTRATIVE TASKS
			VERSIONING MENU

        *NOTE*: this host must be the file server for the netstation(s)
        *IMPORTANT NOTE*: Do not change or delete versions that are
         currently in use by a netstation. This can lead to unexpected
         results.

	1) show installed versions

	2) change default version sitewide

	3) show/change active version for a netstation

	4) remove a version

		?) Help		p) previous menu

EOF
  	$ECHO "Please enter selection (default=1): $NL"
	read SEL 
	case $SEL in
	   1|2|3|4|"")
             if [ "$SEL" = "3" ]
             then
               getxtname "change"

               if [ $? -ne 0 ]
               then
                  continue
               fi
               XTNAME=`cat $TMP/XTNAME | awk -F'.' '{print $1}'`
               rm  $TMP/XTNAME
             fi

	     NUMBER=0
             if [ -d $XTHOME/bin ]
             then
               ENWARE_THERE="true"
               DIRLIST=`ls -d $XTHOME*`
             else
               DIRLIST=""
             fi

             if [ -d $XTHOME2 ]
             then
               DIRLIST2=`ls -d $XTHOME2*`
             else
               DIRLIST2=""
             fi
	     VERLIST=""
	     NEWDIRLIST=""
	     BANNER="no"
	     for DIR in $DIRLIST
	     do
	       if [ -d $DIR -a ! -h $DIR ]
	       then
		 VER=`xtutil_GetVersion $DIR/bin 2>&1`
	         if [ $? -ne 0 ]
	         then
                   # could not find valid version .. just ignore directory
		   continue
	         fi
	         if [ "`basename $DIR`" = "`basename $XTHOME`" ]
	         then
		   ACTIVE7=`expr $NUMBER + 1`
		   CHOME7=$DIR
		   ACTIVE="yes"
	         else
		   ACTIVE="no "
	         fi
                 SW_CLASS="ENWARE SW"
		 if [ "$BANNER" = "no" ]
		 then
		   BANNER="yes"
		   echo  
		   echo  "Installed versions:"
		   echo  "No    ACTIVE_VERSION      VERSION #"
		   echo  "===================================================="
		 fi
		 NUMBER=`expr $NUMBER + 1`
	         echo "$NUMBER)    $ACTIVE                 $VER   ($SW_CLASS)"
		 if [ "$NEWDIRLIST" = "" ]
		 then
		   NEWDIRLIST="$DIR"
		   VERLIST="$VER"
		 else
		   NEWDIRLIST="$NEWDIRLIST $DIR"
		   VERLIST="$VERLIST $VER"
		 fi
	       fi
	     done
	     MIDNUMBER=$NUMBER

	     for DIR in $DIRLIST2
	     do
	       if [ -d $DIR -a ! -h $DIR ]
	       then
		 VER=`xtutil_GetVersion $DIR 2>&1`
	         if [ $? -ne 0 ]
	         then
                   # could not find valid version .. just ignore directory
		   continue
	         fi
	         if [ "$BASEXTHOME2" = "`basename $DIR`" ]
	         then
		   ACTIVE8=`expr $NUMBER + 1`
		   CHOME8=$DIR
		   ACTIVE="yes"
	         else
		   ACTIVE="no "
	         fi
                 SW_CLASS="Netstation SW"
		 if [ "$BANNER" = "no" ]
		 then
		   BANNER="yes"
		   echo
		   echo  "No    ACTIVE_VERSION      VERSION #"
		   echo  "===================================================="
		 fi
		 NUMBER=`expr $NUMBER + 1`
	         echo "$NUMBER)    $ACTIVE                 $VER   ($SW_CLASS)"
		 if [ "$NEWDIRLIST" = "" ]
		 then
		   NEWDIRLIST="$DIR"
		   VERLIST="$VER"
		 else
		   NEWDIRLIST="$NEWDIRLIST $DIR"
		   VERLIST="$VERLIST $VER"
		 fi
	       fi
	     done
	

	     echo  "===================================================="
	     echo ""
	     case $SEL in
	       1|"")
                 $ECHO "Please type <Enter> to continue $NL"
		 REPLY=`line`
		 echo
		 echo
		 continue
		 ;;
	       2|3|4)
	         case $SEL in
	           2|3) ACTION=change; ALREADY=already
                      TEXT="different version to become the active version";;
	           4) ACTION=remove; ALREADY=""
                      TEXT="version to remove";;
                 esac
                 if [ $SEL != "3" ]
                 then
		   if [ "$NUMBER" = "0" -o "$NUMBER" = "1" ]
		   then
		     echo "Cannot $ACTION last version, only $NUMBER version installed."
                     $ECHO "Please type <Enter> to continue $NL"
		     REPLY=`line`
		     echo
		     echo
		     continue
		   fi
		 fi


                 if [ $SEL = "3" ]
                 then
                   rm -f /tmp/$XTNAME.cfg
                   FILETHERE="false"
                   echo attempting to $REMSH to netstation
                   $REMSH $XTNAME get config  > /tmp/$XTNAME.cfg
                   if [ -f /tmp/$XTNAME.cfg -a -s /tmp/$XTNAME.cfg ]
                   then
                     grep -i BasePath /tmp/$XTNAME.cfg > /dev/null
                     if [ $? -eq 0 ]
                     then
                        FILETHERE="true"
                     fi
                   fi
                     
                   if [ $FILETHERE = "true" ]
                   then
                     THISVER=`$REMSH $XTNAME get installed  \
			| egrep -i '(^Server version:)|(Xserver version)' \
			| head -1 | awk '{print $NF}'`
                     echo $XTNAME seems to be running $THISVER

                     BOOTHOST=`egrep -i '^File Server.*=' /tmp/$XTNAME.cfg \
                       | sed -e 's!/nfs/!!' -e 's!/tftp/!!' \
		       | awk -F'=' '{print $2}' | sed 's/ //' \
			| awk -F',' '{print $1}'`
                     echo $BOOTHOST | egrep '([A-Z])|(a-z)' > /dev/null
                     if [ $? -ne 0 ]
                     then
                       isipaddrinh $BOOTHOST
                       if [ -f $TMP/HOSTNAME ]
                       then
                         BOOTHOST=`cat $TMP/HOSTNAME`
                         rm $TMP/HOSTNAME
                       fi
                     fi

                     echo $BOOTHOST | egrep '([A-Z])|(a-z)' > /dev/null
                     if [ $? -eq 0 ]
                     then
                       BH=`echo "$BOOTHOST" | awk -F'.' '{print $1}'`
                     else
                       BH=$BOOTHOST
                     fi
                     HN=`echo "$HOSTNAME" | awk -F'.' '{print $1}'`

                     if [ "$BH" != "$HN" ]
                     then
                       echo ""
                       echo "  Current netstation file server for $XTNAME seems to be $BH."
                       echo "  Only netstations currently booting off $HN can be changed from this menu."
                       echo "  As an alternative, edit the remote configuration file or modify the"
                       echo "  \"BasePath\" field on the Server configuration screen."
                       echo ""
                       $ECHO "type <Enter> to continue $NL"
		       REPLY=`line`
                       echo 
                       continue
                         echo 
                     fi
                   else
                     echo 
                     echo Could not determine version of $XTNAME.
                     echo Terminal needs to be connected to net and booted
                     echo for this option to work.
                     echo Also it must be possible to do a \"$REMSH $XTNAME get config\"
                     $ECHO "type <Enter> to continue $NL"
		     REPLY=`line`
                     echo 
                     continue
                   fi
                 fi

		 echo
		 echo "$NUMBER versions installed."
		 echo "Each version is associated with a number in leftmost column of table above." 
		 echo "You may select a $TEXT"
		 echo "by entering the number associated with version."
		 echo ""
		 $ECHO "Please enter a number between 1 and $NUMBER or q to quit (q) $NL" 
		 NUM=`line`
                 echo
		 export NUM
		 case "$NUM" in
		   q|Q|quit|QUIT|"") exit 100 ;;
		   \?|\.|\*)
		    echo
		    echo "no help at this prompt"
		    echo
		    sleep 2
		    exit 100
		   ;;
		   [1-9]) : ;;
		   1[0-9]) : ;;
		   *)
		   echo
		   echo "Invalid version number -- (not in range 1-$NUMBER)."
                   $ECHO "type <Enter> to continue $NL"
		   REPLY=`line`
		   echo
		   continue
		   ;;
		 esac 

		 if [ "$NUM" -lt 1 -o $NUM -gt "$NUMBER" ]
		 then
		   echo
		   echo "Invalid version number -- (not in range 1-$NUMBER)."
                   $ECHO "type <Enter> to continue $NL"
		   REPLY=`line`
		   echo
		   continue
		 fi

		 if [ "$NUM" -gt "$MIDNUMBER" ]
		 then
		   ENWARE2="true"
		 else
		   ENWARE2="false"
		 fi

                 NEWDIR=`echo "$NEWDIRLIST" | cut -d' ' -f $NUM`
                 if [ "$SEL" != "3" ]
                 then
		   if [ "$ACTIVE7" -eq "$NUM"  -o "$ACTIVE8" -eq "$NUM" ]
		   then
		     echo "" 
		     echo "selected version is $ALREADY the active version, no changes made."
                     $ECHO "type <Enter> to continue $NL"
		     REPLY=`line`
		     echo ""
		     continue
		   fi
		 fi


                 if [ "$ENWARE2" = "false" ]
                 then
                   OVER=`xtutil_GetVersion $XTHOME/bin`
                 fi

		 NVER=`xtutil_GetVersion $NEWDIR/bin`


                 if [ "$SEL" = "4" ]
                 then
                   echo .. working
                   rm -rf $NEWDIR
                   LINK=`echo $NEWDIR | sed -e 's!/usr/tftpdir!!' -e 's!/tftpboot!!'`
          
                   rm -f $LINK
                   echo removed version $NVER in $NEWDIR
                   $ECHO "type <Enter> to continue $NL"
		   REPLY=`line`
                   continue
                 fi

                 if [ "$SEL" = "3" ]
                 then
                   #
                   # get the requested directory for new version
                   #
                   NP=`echo $NEWDIR | sed -e 's!/usr/tftpdir!!' -e 's!/tftpboot!!'`
                   #
                   # create a config file with new path
                   # in ./config
                   #
                   NP=`echo "$NP/" | sed 's!//!/!'`
                   OP=`egrep '^BasePath' /tmp/$XTNAME.cfg | awk '{print $NF}'`
                   sed -e "/BasePath/s!$OP!$NP!" /tmp/$XTNAME.cfg  \
			> /tmp/$XTNAME.cfg.new
                   cmp -s /tmp/$XTNAME.cfg /tmp/$XTNAME.cfg.new
                   if [ $? -eq 0 ]
                   then
                     sed -n '1,/^BasePath/p' /tmp/$XTNAME.cfg | sed '$d' \
						> /tmp/$XTNAME.cfg.new 
                     echo "BasePath $NP"       >> /tmp/$XTNAME.cfg.new
                     sed -n '/^BasePath/,$p' /tmp/$XTNAME.cfg | sed '1d' \
					       >> /tmp/$XTNAME.cfg.new 
                   fi

                   #
                   # 700/RX roms don't all understand BasePath
                   # and must have "X Server File" set. 
                   #
                   MODEL=`$REMSH $XTNAME get installed  \
			| egrep -i '^Base Unit:' | awk '{print $NF}'`
                   if [ "$MODEL" = "C2703A" -o "$MODEL" = "C2707A" -o "$MODEL" = "C2708A" -o "$MODEL" = "C2700A" -o "$MODEL" = "C3225" -o "$MODEL" = "700/RX" ]
                   then
                     # 
                     #  "700/RX" is a class of models
                     #  get real model from different field
                     # 
                     if [ "$MODEL" = "700/RX" ]
                     then
                        MODEL=`$REMSH $XTNAME get installed  \
                          | egrep -i '^Base Unit:' | awk '{print $3}'`
                     fi
                     ICFG=/tmp/$XTNAME.cfg.new
                     TMPCFG=/tmp/$XTNAME.cfg.new2
                     sed -n '1,/^X Server file/p' $ICFG | sed '$d'  > $TMPCFG
                     echo "X Server file = \"$NP/bin/$MODEL\""     >> $TMPCFG
                     sed -n '/^X Server file/,$p' $ICFG | sed '1d' >> $TMPCFG
                     cp $TMPCFG /tmp/$XTNAME.cfg.new
                   fi

                   if [ "$ENWARE2" = "false" ]
                   then
                     # ENWARE
                     if [ -f $XTHOME/config/$XTNAME.cfg ]
                     then
                       CFGTHERE="true"
                       echo creating new config file $XTHOME/config/$XTNAME.cfg
                     else
                       CFGTHERE="false"
                     fi
                   else
                     # Netstation
                     if [ -f $XTHOME2/config/$XTNAME.cfg ]
                     then
                       CFGTHERE2="true"
                       echo creating new config file $XTHOME2/config/$XTNAME.cfg
                     else
                       CFGTHERE2="false"
                     fi
                   fi

                   #------------------------------------------------#
                   # Make sure terminal has a cfg file to read
                   # using its currently active path before we reboot it.
                   #------------------------------------------------#
                   #
                   OCFGDIR=`echo ${OP}/config | sed 's!//!/!'`
                   if [ ! -d ${OCFGDIR} ]
                   then
                     mkdir -p ${OCFGDIR}
                   fi
                   cp /tmp/$XTNAME.cfg.new ${OCFGDIR}/$XTNAME.cfg

                   #------------------------------------------------#
                   # Create a cfg file in the path that will
                   # be active after we reboot it.
                   #------------------------------------------------#
                   #
                   if [ "$ENWARE2" = "false" ]
                   then
                     if [ ! -d $XTHOME/config ]
                     then
                       mkdir -p $XTHOME/config
                     fi
                     cp /tmp/$XTNAME.cfg.new $XTHOME/config/$XTNAME.cfg
                   else
                     if [ ! -d $XTHOME2/config ]
                     then
                       mkdir -p $XTHOME2/config
                     fi
                     cp /tmp/$XTNAME.cfg.new $XTHOME2/config/$XTNAME.cfg
                   fi

                   #------------------------------------------------------#
                   # if there's a bootptab entry and it mentions the "bf" 
                   # make it the directory with new version 
                   #------------------------------------------------------#
                   #
                   sed "/^$XTNAME[:.]/,/bf=/s!bf=/.*:!bf=$NP:!" \
			$BOOTPTABLE > /tmp/bp.$$
                   cmp -s $BOOTPTABLE /tmp/bp.$$
                   if [ $? -ne 0 -a -s /tmp/bp.$$ ]
                   then
                     cp /tmp/bp.$$ $BOOTPTABLE
                     echo modified \"bf\" tag for $XTNAME in $BOOTPTABLE
                   fi
                   rm -f /tmp/bp.$$ 

                   $REMSH $XTNAME reconfig
                   sleep 3

                   #------------------------------------------------------#
                   # Get rid of the config file we created
                   # if there wasn't one before 
                   #------------------------------------------------------#
                   #
                   (
                   sleep 5
                   if [ $ENWARE2 = "false" ]
                   then
                     if [ $CFGTHERE = "false" ] 
                     then
                       rm $XTHOME/config/$XTNAME.cfg
                     fi
                   fi
                   ) &
                   (
                   sleep 5
                   if [ $ENWARE2 = "true" ]
                   then
                     if [ $CFGTHERE2 = "false" ] 
                     then
                       rm $XTHOME2/config/$XTNAME.cfg
                     fi
                   fi
                   ) &

                   echo
                   $ECHO "OK to reboot netstation now? [y|n] (y): $NL"
                   read ANS


                   case $ANS in
                     y*|Y*|"")
                     :
                     ;;
                     *) 
                       echo
                       echo 'The netstation should be rebooted (power cycled) to make it re-read'
                       echo "its new remote config file and then load the new X Server version."
                       echo
                       $ECHO "Please type <Enter> to continue $NL"
                       read SEL
                       echo
                       continue ;;
                   esac 
                   echo attempting to reboot netstation with new version
                   $REMSH $XTNAME reboot
                   $ECHO "Type <Enter> to continue $NL"
		   REPLY=`line`
		   echo
		   continue
                 fi

                 #------------------------------------------------------#
                 # Change active version code
                 #------------------------------------------------------#
                 #
                 if [ "$SEL" = "2" ]
                 then
                   if [ "$ENWARE2" = "true" ]
		   then
		     rm -f $XTHOME2
		     if [ $? -ne 0 ]
		     then
		       echo "couldn't remove link $XTHOME2"
		     fi
		     ln -s $NEWDIR $XTHOME2
		     echo
		     echo "$NVER is now the active version"
		     echo "Netstations booting off this host should be rebooted."
		     echo
		   else
		     if [ -h $XTHOME.$OVER ]
                     then
                       rm $XTHOME.$OVER
                     fi

	 	     #
                     # the $XTHOME directories below are not versioned
                     EXCLUDED="fonts config etc"
  
                     for EXDIR in $EXCLUDED
                     do
                       rm -f $NEWDIR/$EXDIR
                       mv  $XTHOME/$EXDIR $NEWDIR/$EXDIR
                       case $OS in
                         hp10) ln -s $XTHOME/$EXDIR $XTHOME/$EXDIR ;;
                         *)    ln -s /usr/lib/X11/700X/$EXDIR $XTHOME/$EXDIR ;;
                       esac
                     done
  
		     mv $XTHOME $XTHOME.$OVER
		     if [ $? -eq 0 ]
		     then
		       mv $NEWDIR $XTHOME
		       if [ $? -ne 0 ]
		       then
		         echo "could not do a \"mv $NEWDIR  $XTHOME\"">&2
		       else
                         case $OS in
                           hp10) ln -s $XTHOME $NEWDIR ;;
                           *)    ln -s /usr/lib/X11/700X $NEWDIR ;;
                         esac
		         echo
		         echo "$NVER is now the active version"
		         echo "Netstations booting off this host should be rebooted."
		         echo
		       fi
		     else
		       echo "could not do a \"mv $XTHOME $XTHOME.$OVER\"" >&2
		     fi
		   fi
		 fi
		 ;;
	       *)
		 ;;
             esac
	     ;; 
	  p*|P*)
		exit 100
		;; 
	     \?)
		cat <<EOF
Selecting:

1) show installed versions
	Will allow you to see the versions currently installed.

2) change default version
	Will allow you to change the default active version for all netstations.

3) show/change active version for a netstation 
	Will allow you to specify the active version on a per terminal basis.
        The netstation has to be up and running; also it most be running
        version B.05.10 or later of ENWARE.
        The netstation should also be set to read its remote config file
        and it must have permissions set to allow "$REMSH" in order to do a
        "get config", "get installed", and "reboot".
        If the terminal is using its defaults, this will normally be the case.

        The version can also be changed manually on the terminal by editing
        the Base path field in the Terminal Configuration Server screen.
        (press F12 for 2 sec to access the configuration screen)
        
4) remove a version
	Will allow you to remove a non-active version.
        It is not possible to remove the last version.

EOF
		;;
	  *)
	    echo
	    continue
	    ;; 
	esac

  $ECHO "Please type <Enter> to continue $NL"
  read SEL
done

exit 0
