#!/bin/bash
#set -x

MSG1="\n\nPlease select one of the following options:\n\n"
MSG2="An error has occured processing the media.\n"
MSG2="${MSG2}The followings conditions may have occured:\n\n"
MSG2="${MSG2}-The media in the drive is not valid\n"
MSG2="${MSG2}-The certificate on the media is not valid\n"
MSG2="${MSG2}-An I/O error occured while processing the media\n"
MSG2="${MSG2}-Media validation error due to incorrect date and time in BIOS\n\n"
MSG2="${MSG2}Choose one of the option below:\n\n"
MORE_SOFTWARE="1 -- Install additional software from media\n"
DVD_RESTORE="1 -- Restore critical console data from DVD-RAM media (Install only)\n"
DVD_RESTORE2="2 -- Restore Upgrade data from DVD-RAM media (Upgrade from older Version/Release)\n"
RESTORE1="1 -- Restore Critical Console data (Install only)\n"
RESTORE2="2 -- Restore Upgrade data  (Upgrade from older Version/Release)\n"
RETRY="1 -- Retry the operation\n"
ABORT="2 -- Abort the operation\n"
FINISH_1="3 -- Finish the installation without restoring Upgrade data \n"
FINISH_2="2 -- Finish the installation without Restore Upgrade data from DVD-RAM \n"
FINISH_3="3 -- Finish the installation\n"
INCOMPLETE_FINISH="2 -- Finish the installation at a later time\n"
YOUR_SELECTION="\nPlease enter your selection : "
INSERT_CDROM="\n\n\n     Please insert the media into the drive,"
INSERT_CDROM="$INSERT_CDROM and press Enter to continue.\n"
INSERT_DVD="\n\n\n     Please insert the DVD media into the drive,"
INSERT_DVD="$INSERT_DVD and press Enter to continue.\n"
INSERT_KEY="\n\n\n     Please insert the USB flash memory device,\n "
INSERT_KEY="$INSERT_KEY     and press Enter to continue.\n"
INSERT_MEDIA="\n\n\n   Please insert the DVD-RAM or USB flash memory device.\n"
INSERT_MEDIA="$INSERT_MEDIA  The DVD-RAM media will be used if both devices are inserted.\n\n"
INSERT_MEDIA="$INSERT_MEDIA Press Enter to continue.\n"
RST_FAIL_1="\n\n The restore operation failed\n"
RST_FAIL_2="1 -- Retry the Restore Upgrade data  \n"
USB_FAIL="\n ERROR : Unable to detect a USB flash memory device. Remove the device and re-insert\n"
USB_FAIL="$USB_FAIL           or retry with a different media\n"
FAILURE_1="\nERROR : Unable to read the media. Please retry with different media\n\n"
FAILURE_2="\nERROR : Unable mount the media. Please retry\n\n"
FAILURE_3="\nERROR : No upgrade data on the media. Please retry with different media\n\n"
DETECTING="\n Detecting USB flash memory device\n"
UPGRADE1="1 -- Use DVD-RAM media\n"
UPGRADE2="2 -- Use USB flash memory\n"

UPGR_FILE="/mnt/upgrade/SaveHSCSystemUpgradeData.tar"
RSTR_FILES="/etc/localtime /etc/sysconfig/clock"

function DisplayChoice {
    echo -en $1
    echo -en $2
    echo -en $3
    if [ "$4" != "" ]; then echo -en $4; fi
    echo -en $YOUR_SELECTION
}

function getUSB {
    typeset -i loopcount=0
    MOUNTPOINT=""
    while [ $loopcount -lt 10 ]; do

       x=`/opt/hsc/bin/discover_devices --all usbflashkey`
       if [ "$x" != "" ]; then
       # now find mount point
          device=`echo $x | cut -d';' -f1`
          l=`cat /etc/fstab | grep ^"$device"`
          for i in $l
          do
            d=`/usr/bin/dirname $i 2>/dev/null`
            if [ "$d" = "/media" ]; then
                MOUNTPOINT="$i"
                return 0
            fi
          done
       else
          loopcount=$loopcount+1
          if [ $loopcount  -eq 1 ]; then
               echo -en $DETECTING
          fi
          sleep 5
       fi
    done
    if [ "$MOUNTPOINT" == "" ] ; then
          echo -en $USB_FAIL
    fi 
  
}

case $1 in
    1 ) DisplayChoice "$MSG2" "$RETRY" "$ABORT"
        read response
        if [ "$response" != "2" ]; then
            exit 1
        fi ;;
    2 ) DisplayChoice "$MSG1" "$MORE_SOFTWARE" "$INCOMPLETE_FINISH"
        read response
        if [ "$response" == "1" ]; then
            echo -en $INSERT_CDROM
            read response
            exit 1
        fi ;;
    3 ) DisplayChoice "$MSG1" "$RESTORE1" "$RESTORE2" "$FINISH_3"
        read response
        RETURN_VAL=2
        case "$response" in 
            1 ) echo -en $INSERT_MEDIA
                read dvd_insert
                exit $RETURN_VAL ;;
            2 )  while [ "$RETURN_VAL" == "2" ]; do
                   DisplayChoice "$MSG1" "$UPGRADE1" "$UPGRADE2" "$FINISH_1" 
                   read upgresponse
                   case "$upgresponse" in
                      1 ) MOUNTPOINT="/media/cdrom"
                          echo -en $INSERT_DVD 
                          read dvd_insert;;
                      2 ) echo -en $INSERT_KEY 
                          read dvd_insert
                          getUSB ;;
                      * ) RETURN_VAL=3 ;;
                   esac
                   if [ "$MOUNTPOINT" == "" ]; then
                       continue;
                   fi
                   mount $MOUNTPOINT 2>/dev/null
                   /opt/hsc/sbin/restoreupgrdata $MOUNTPOINT
                   RTN_CODE=$?
                   if [ "$MOUNTPOINT" == "/media/cdrom" ]; then
                      eject $MOUNTPOINT
                   fi
                   if [ $RTN_CODE -eq 0 ]; then
                        mount /mnt/upgrade 2>/dev/null
                        if [ -f $UPGR_FILE ]; then
                            tar -xBf $UPGR_FILE $RSTR_FILES 2>/dev/null
                        fi 
                        umount /mnt/upgrade 2>/dev/null
                        /etc/init.d/boot.clock restart
                        RETURN_VAL=3
                    else
                        case $RTN_CODE in
                            1 ) echo -en $FAILURE_1 ;;
                            2 ) echo -en $FAILURE_2 ;;
                            3 ) echo -en $FAILURE_3 ;;
                        esac
                        echo -en $RST_FAIL_1
                        DisplayChoice "$MSG1" "$RST_FAIL_2" "$FINISH_2"
                        read response
                        if [ "$response" == "2" ]; then
                            RETURN_VAL=3
                        fi
                    fi 
                done
                eject $MOUNTPOINT 2>/dev/null
                exit $RETURN_VAL ;;
	    * )
	      # Clear bit so no restore occurs
              mount /mnt/upgrade 2>/dev/null
	      rm -f /mnt/upgrade/iqybcrit.dat
	      umount /mnt/upgrade
	      ;;
        esac
        ;;
esac
exit 0
