# Copyright (C) 2002 Ensim Corporation. All Rights Reserved.
#

trap_handler_INT()
{
    local _yorn=""
    local _done="n"
    while [ "$_done" = "n" ]
    do
        echo -n "Are you sure you want to abort? [n] "
        read _yorn
        if [ X"_yorn" = "" ] || [ "$_yorn" = "y" ] || [ "$_yorn" = "Y" ]
        then
            _done="y"
            exit 1
        fi
    done
}

F_read_email()
{
   local _q_mail="n"
   while [ "$_q_mail" = "n" ] 
   do 
   echo ""
   echo -n "> Please enter the email for receiving error logs: "
   read _q_email
   if [ X"$_q_email" = X"" ]
   then	
     	echo ""
       	echo "*** Kindly enter an email address ."
       	echo ""
   else
     	echo ""
        echo "You specified the following email : $_q_email"
        F_ckyorn "y" "Is the above correct?"
     	echo ""
        if [ $_q_yorn = "y" ]
        then
		_q_mail="y"
	fi
   fi	
   done
}
_q_yorn=""

F_ckyorn() # arg1 = default [y] or [n], arg2 = message
           # assumes _q_yorn as global external
{
    _q_yorn=$1
    local _q_done="n"
    while [ $_q_done = "n" ]
    do
        echo -n "$2 [$1] "
        read _q_yorn
        case X"$_q_yorn" in
            X"")
                _q_yorn=$1
                _q_done="y"
                break
            ;;
            X"Y"|X"y")
                _q_yorn="y"
                _q_done="y"
                break
            ;;
            X"N"|X"n")
                _q_yorn="n"
                _q_done="y"
                break
            ;;
            *)
                continue
            ;;
        esac
    done
}

F_checkfile() # arg1 = message
           # assumes _q_ckfile as global external
{
    local _q_done="n"
    while [ $_q_done = "n" ]
    do
        echo ""
        echo -n "$1"
        read _q_file
        if [ X"$_q_file" = X"" ]
        then
            echo "*** Invalid filename."
        else
            if [ -d "$_q_file" ]
            then
                echo "*** $_q_file: is a directory."
                continue
            fi
            if [ -f "$_q_file" ]
            then
		if [ -x "$_q_file" ]
                then
                    echo "*** $_q_file: is an executable."
                    continue
		else 
            		_q_md5=`tail --lines=1 $_q_file`
			_q_md5=`echo $_q_md5 | cut -f1 -d ' '`
			count=`wc -l $_q_file | sed -e 's/  *//' | cut -d " " -f1`; count=`expr $count \- 1`; 
			head -n $count $_q_file > $_q_file.$$
			_q_md5sum=`md5sum $_q_file.$$`
			_q_md5sum=`echo $_q_md5sum | cut -f1 -d ' '`
			if [ X"$_q_md5" = X"$_q_md5sum" ]
			then
				_q_ksdump="$_q_file.$$"
				_q_done="y"	
			else
				echo "> The filename entered by you is not the one prepared by OSI."
				echo "> Please enter the correct filename for the configuration data."
          		        F_ckyorn "y" "> Do you wish to continue ?"
				echo ""
                    		if [ $_q_yorn = "y" ]
                    		then
                        		continue
				else 
					_q_done="y"
					_q_config="y"
                   		fi
			fi
		 fi
            else
                if [ -e "$_q_file" ] # file exists, but is not a regular file
                then
                    echo "*** $_q_file: cannot save to file (not a regular file)."
                    break
                fi
			echo "> File does not exist"
          		F_ckyorn "y" "> Do you wish to continue ?"
			echo ""
                	if [ $_q_yorn = "y" ]
               		then
               			continue
			else 
				_q_done="y"
				_q_config="y"
       			fi
            fi
        fi
    done
}

F_ckfile() # arg1 = message
           # assumes _q_ckfile as global external
{
    local _q_done="n"
    while [ $_q_done = "n" ]
    do
        echo ""
        echo -n "$1"
        read _q_file
        if [ X"$_q_file" = X"" ]
        then
            echo "*** Invalid filename."
        else
            if [ -d "$_q_file" ]
            then
                echo "*** $_q_file: is a directory."
                continue
            fi
            if [ -f "$_q_file" ]
            then
                if [ ! -w "$_q_file" ]
                then
                    echo "*** $_q_file: file exists and is not writable."
                    continue
                else
                    F_ckyorn "n" "*** $_q_file already exists! Do you wish to overwrite it?"
                    if [ $_q_yorn = "n" ]
                    then
                        continue
                    fi
                    break
                fi
            else
                if [ -e "$_q_file" ] # file exists, but is not a regular file
                then
                    echo "*** $_q_file: cannot save to file (not a regular file)."
                fi
                break
            fi
        fi
    done
}

F_handle_ksdump()
{
    if [ X"$_q_ksdump" = X"" ] # there is no saved configuration
    then
        echo ""
        echo "  If you wish to be able to use this configuration information"
        echo "  again without having to answer the above questions, you should"
        echo "  save it to a file."
        echo ""
        F_ckyorn "y" "> Do you wish to save the configuration to a file?"
        if [ $_q_yorn = "y" ]
        then
	    if [ $LINUX = "linux" ]
	    then
		echo ""
            	echo "Please enter the file pathname to save the configuration."
		echo "Kindly specify absolute path for the file."
		echo "Example:/tmp/kickstart."
            	F_ckfile "File pathname : "
	    else			
		echo ""
            	echo "Please enter the filename to save the configuration."
		echo "Don't specify the path."
		echo "Example:kickstart."
            	F_ckfile "Filename: "
	    fi
            _q_ksdump="$_q_file"
            F_dumpks $_q_ksdump
	    _q_md5=`md5sum $_q_ksdump`
	    echo $_q_md5 >> $_q_ksdump	
        fi
    else
	_q_rm=$_q_ksdump
        . $_q_ksdump
	rm -rf $_q_rm
        if [ $? -ne 0 ]
        then
            echo "*** Failed to read saved configuration from $_q_ksdump"
            echo "*** Exiting."
            exit 1
        fi
    fi
}

# __EXIT__
