# start: $Id: rfpcommit.app,v 1.10.6.3 2004/08/16 06:32:00 jeremyc Exp $

# start: $Id: rfpadminroutines.app,v 1.1.6.2 2004/08/12 15:26:59 jeremyc Exp $
test `uname -s` = Linux && echocmd="echo -e" || echocmd="echo"
trap '' 2
INDENT="   "  
yorn ()
{
   YN=b
   while [ "$YN" != "n" -a "$YN" != "no" -a \
           "$YN" != "N" -a "$YN" != "NO" -a \
           "$YN" != "y" -a "$YN" != "yes" -a \
           "$YN" != "Y" -a "$YN" != "YES" -a \
           "$YN" != "Yes" -a "$YN" != "No" ]; do
      $echocmd $1
      read YN
      if [ "$YN" = "y" -o "$YN" = "yes" -o "$YN" = "Yes" -o \
           "$YN" = "Y" -o "$YN" = "YES" ]; then
         return 1
      fi
      if [ "$YN" = "n" -o "$YN" = "no" -o "$YN" = "No" -o \
           "$YN" = "N" -o "$YN" = "NO" ]; then
         return 0
      fi
      $echocmd "Please enter y or n"
   done
}
log()
{
   DATE=`date`
   $echocmd "${DATE}: $1" >>$COBDIR/fixpacks/install.log
}
getallfpdetails()
{
   _FPS=`grep '^PTI=.*pack' $COBDIR/etc/cobver | sed -e 's/^PTI=//' -e 's/ committed$//' | tr '\n' ' '`
   COUNT=0
   COMMITTED_COUNT=0
   FPS_DETAILS=""
   for _FP in $_FPS ; do
      COUNT=`expr $COUNT + 1`
      grep "^PTI=$_FP committed" $COBDIR/etc/cobver > /dev/null
      if [ $? -eq 0 ]; then
         FPS_DETAILS="${FPS_DETAILS}:${COUNT}-${_FP}-committed"
         COMMITTED_COUNT=`expr $COMMITTED_COUNT + 1`
      else
	 FPS_DETAILS="${FPS_DETAILS}:${COUNT}-${_FP}-no"
      fi
   done
}
searchfpdetails()
{
   _ID=$1
   for _FP in `echo $FPS_DETAILS | sed 's/:/ /g'` ; do
      ID=`echo $_FP | cut -d'-' -f1`
      NAME=`echo $_FP | cut -d'-' -f2`
      COMMITTED=`echo $_FP | cut -d'-' -f3`
      [ "$ID" = "$_ID" ] && return 0 ;# success
   done
   return 1 ;# failure
}
MFADMIN=$1
exitmenu()
{
   if [ "x$MFADMIN" != "x" ]; then
      $echocmd "Press Return to continue."
      read _TMP
   else
      $echocmd "exiting ..."
   fi
   exit 0
}
contmenu()
{
   $echocmd "Press Return to continue."
   read _TMP
}
if [ ! -w /etc/passwd ]; then
   $echocmd "*** You must be root to run this script."
   $echocmd "exiting ..."
   exit 1
fi
if [ ".$COBDIR" = "." ] || [ ! -f $COBDIR/etc/cobver ]; then
   $echocmd "*** COBDIR must be set to point to a valid product."
   $echocmd "exiting ..."
   exit 1
fi
# end: $Id: rfpadminroutines.app,v 1.1.6.2 2004/08/12 15:26:59 jeremyc Exp $

indicatefpcommitted()
{
   _FP=$1
   cd $COBDIR/etc
   cp cobver /tmp/cobver.$$
   sed "s/^PTI=${_FP}\$/PTI=${_FP} committed/" /tmp/cobver.$$ >cobver
   rm /tmp/cobver.$$
}

getcommitselstart()
{
   SEL_LIST=
   SEL_START=1
   SEL_END=1
   _ID=1
   searchfpdetails $_ID
   while [ $? -eq 0 ]
   do
      SEL_END=$_ID
      if [ "x$SEL_LIST" = "x" ]; then
         SEL_LIST=$SEL_END
      else
         SEL_LIST="$SEL_LIST,$SEL_END"
      fi
      if [ $COMMITTED = "committed" ]; then
         SEL_START=`expr $_ID + 1`
         SEL_LIST=
      fi
      _ID=`expr $_ID + 1`
      searchfpdetails $_ID
   done
}

commitfixpacks()
{
   _SEL=$1
   _ID=1
   _FPS_TO_COMMIT=""
   _FPS_TO_COMMIT2=""
   searchfpdetails $_ID
   while [ $? -eq 0 ]
   do
      if [ $_ID -le $_SEL ] && [ "$COMMITTED" = "no" ]; then
         if [ "x$_FPS_TO_COMMIT" != "x" ]; then
	    _FPS_TO_COMMIT="$_FPS_TO_COMMIT\n$NAME"
	    _FPS_TO_COMMIT2="$_FPS_TO_COMMIT2 $NAME"
	 else
	    _FPS_TO_COMMIT=$NAME
	    _FPS_TO_COMMIT2=$NAME
	 fi
      fi
      _ID=`expr $_ID + 1`
      searchfpdetails $_ID
   done
   clear
   $echocmd "The following Packs will be committed:"
   $echocmd $_FPS_TO_COMMIT
   yorn "Do you want to continue? (y/n): \c"
   if [ $? -eq 1 ]; then
      for _NAME in $_FPS_TO_COMMIT2
      do
         if [ "x$_NAME" != "x" ]; then
	    $echocmd "\n${INDENT}Committing $_NAME. Please wait..."
	    rm -fr $COBDIR/fixpacks/$_NAME
	    indicatefpcommitted $_NAME
	    log "Committed $_NAME"
            $echocmd "${INDENT}$_NAME has been committed." 
            sleep 2
	 fi
      done
      exitmenu
   fi
}

displaycobdir()
{
   cat <<END

[COBDIR=$COBDIR]
END
}

while [ 1 ]; do
   clear
   cat <<END

            Commit a Pack

END
   getallfpdetails
   if [ $COUNT -eq 0 ]; then
      $echocmd "\n*** No Packs are currently installed."
      exitmenu
   fi
   $echocmd "The following Packs are currently installed:"
   I=0
   while [ $I -lt $COUNT ]; do
      I=`expr $I + 1`
      searchfpdetails $I
      if [ $COUNT -eq $COMMITTED_COUNT ]; then
         cat <<END
            ${NAME} ${COMMITTED}
END
      else
         [ "$COMMITTED" = "no" ] && COMMITTED=""
         cat <<END
            ${ID}) ${NAME} ${COMMITTED}
END
      fi
   done
   if [ $COUNT -eq $COMMITTED_COUNT ]; then
      $echocmd "\n*** All Packs have been committed."
      exitmenu
   fi
   getcommitselstart
   $echocmd "\nWARNING: When a Pack is committed, all earlier"
   $echocmd "installed Packs are also automatically committed."
   if [ "$SEL_START" = "$SEL_END" ]; then
      $echocmd "Select a Pack to commit (${SEL_START}, or q to quit): \c"
   else
      $echocmd "Select a Pack to commit (${SEL_LIST}, or q to quit): \c"
   fi
   read SEL
   if [ "$SEL" = "q" ]; then
      exit 0
   fi
   echo "$SEL" | grep '^[0-9][0-9]*$' > /dev/null
   if [ $? -ne 0 ] || [ $SEL -lt "1" ] || [ $SEL -gt $COUNT ]; then
      $echocmd "\n*** Invalid selection."
      contmenu
   else
      searchfpdetails $SEL
      if [ $COMMITTED = "committed" ]; then
         $echocmd "\n*** Invalid selection."
         contmenu
      else
         commitfixpacks $SEL
      fi
   fi
done
# end: $Id: rfpcommit.app,v 1.10.6.3 2004/08/16 06:32:00 jeremyc Exp $
