#!/bin/bash

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"
RETRY="1 -- Retry the operation\n"
ABORT="2 -- Abort the operation\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"
RST_FAIL_1="\n\n Restore from DVD failed\n"
RST_FAIL_2="1 -- Retry the Restore Upgrade data from DVD-RAM \n"
FAILURE_1="\nERROR : Unable to read the DVD-RAM. Please retry with different DVD-RAM media\n\n"
FAILURE_2="\nERROR : Unable mount the DVD-RAM. Please retry\n\n"
FAILURE_3="\nERROR : No upgrade date in DVD-RAM. Please retry with different DVD-RAM media\n\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
}

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" "$DVD_RESTORE" "$DVD_RESTORE2" "$FINISH_3"
        read response
        RETURN_VAL=2
        case "$response" in 
            1 ) echo -en $INSERT_DVD
                read dvd_insert
                exit $RETURN_VAL ;;
            2 ) while [ "$RETURN_VAL" == "2" ]; do
                    echo -en $INSERT_DVD
                    read dvd_insert
                    mount /media/cdrom
                    /opt/hsc/sbin/restoreupgrdata
                    RTN_CODE=$?
                    eject /media/cdrom
                    if [ $RTN_CODE -eq 0 ]; then
                        mount /mnt/upgrade 2>/dev/null
                        if [ -f $UPGR_FILE ]; then
                            tar -xBf $UPGR_FILE $RSTR_FILES
                        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
                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
