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


if [ "$XTHOME" = "" ]
then
  PATH=$PATH:/projects/tapes/xtutils
  XTHOME=/tmp/xthome
  ECHO="echo"
  NL="\c"
fi

case $OS in
  hp*)  ;;
  *)  XTHOME=$TOPDIR/usr/lib/X11/700X ;;
esac

set -u

while [ 0 ] 
do
	cat <<EOF





	_________________________________________________
		HP X STATION ADMINISTRATIVE TASKS
			VERSIONING MENU


        *NOTE*: this host must be the file server for the X station(s)


	1) show installed versions

	2) change default version sitewide

	3) show/change active version for an X station

	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
	     DIRLIST=`ls -d $XTHOME*`
	     VERLIST=""
	     NEWDIRLIST=""
	     echo  
	     echo  "Installed versions:"
	     echo  "No    ACTIVE_VERSION      VERSION"
	     echo  "============================================"
	     for DIR in $DIRLIST
	     do
	       if [ -d $DIR -a ! -h $DIR ]
	       then
	         if [ "`basename $DIR`" = "`basename $XTHOME`" ]
	         then
		   ACTIVE="yes"
	         else
		   ACTIVE="no "
	         fi
		 VER=`xtutil_GetVersion $DIR/bin 2>&1`
	         if [ $? -ne 0 ]
	         then
		   continue
	         fi
		 NUMBER=`expr $NUMBER + 1`
	         echo "$NUMBER)    $ACTIVE                 $VER"
		 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 X station
                   $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:' | 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/ //'`
                     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 X station file server for $XTNAME seems to be $BH."
                       echo "  Only X stations 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" -lt 1 -o $NUM -gt "$NUMBER" ]
		 then
		   echo
		   echo "Invalid version number -- (not in range 1-$NUMBER)."
		   echo
		   continue
		 fi


                 NEWDIR=`echo "$NEWDIRLIST" | cut -d' ' -f $NUM`
                 if [ "$SEL" != "3" ]
                 then
		   if [ "`basename $NEWDIR`" = "`basename $XTHOME`" ]
		   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


                 OVER=`xtutil_GetVersion $XTHOME/bin`
		 NVER=`xtutil_GetVersion $NEWDIR/bin`

                 if [ "$SEL" = "4" ]
                 then
                   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 current XTHOME/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 [ -f $XTHOME/config/$XTNAME.cfg ]
                   then
                     CFGTHERE="true"
                     echo creating new config file $XTHOME/config/$XTNAME.cfg
                   else
                     CFGTHERE="false"
                   fi

                   OCFGDIR=`echo ${OP}/config | sed 's!//!/!'`
                   if [ ! -d ${OCFGDIR} ]
                   then
                     mkdir -p ${OCFGDIR}
                   fi
                   cp /tmp/$XTNAME.cfg.new ${OCFGDIR}/$XTNAME.cfg

                   if [ ! -d $XTHOME/config ]
                   then
                     mkdir -p $XTHOME/config
                   fi
                   cp /tmp/$XTNAME.cfg.new $XTHOME/config/$XTNAME.cfg

                   # 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:!p" \
			$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
                   (
                   sleep 5
                   if [ $CFGTHERE = "false" ] 
                   then
                     rm $XTHOME/config/$XTNAME.cfg
                   fi
                   ) &

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


                   case $ANS in
                     y*|Y*|"")
                     :
                     ;;
                     *) 
                       echo
                       echo 'The X station 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 X station with new version
                   $REMSH $XTNAME reboot
                   $ECHO "Type <Enter> to continue $NL"
		   REPLY=`line`
		   echo
		   continue
                 fi

                 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 "X terminals booting off this host should be rebooted."
		     echo
		   fi
		 else
		   echo "could not do a \"mv $XTHOME $XTHOME.$OVER\"" >&2
		 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 X stations.

3) show/change active version for an X station 
	Will allow you to specify the active version on a per terminal basis.
        The X station has to be up and running; also it most be running
        version B.05.10 or later of ENWARE.
        The X station 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
