# Copyright (C) 2002 Ensim Corporation. All Rights Reserved.
#
F_installer()
{
	echo ""
	F_ckyorn "y" "> Is this the target server for Ensim Webppliance?"
 	if [ $_q_yorn = "y" ]
	then
########	Remove the old configuration first ########
	if [ -f /var/lib/ensim/local.xml ]
	then
		rm -rf /var/lib/ensim/local.xml
	fi
	if [ -f /var/lib/ensim/updateslocal.xml ]
	then
		rm -rf /var/lib/ensim/updateslocal.xml
	fi
	if [ -f /var/lib/ensim/updates.sh ]
	then
		rm -rf /var/lib/ensim/updates.sh
	fi
	if [ -f /var/lib/ensim/installer.sh ]
	then
		rm -rf /var/lib/ensim/installer.sh
	fi
	F_Get_wpinstall
	F_Get_updates
	F_read_email
    	. $KSGENDIR/lib/updatesetup.sh
	cd $KSGENDIR/lib/preinstall
	./oem_installer.sh
		if [ $? = 0 ]
		then
			cd $KSGENDIR
			. /var/lib/ensim/updates.sh
			if [ $? = 0 ]
			then
				exit 0	
			else 
				sed -e "s/^\/var\/lib\/ensim\/wpsetup.sh.*//g" /etc/rc.d/rc.local > /tmp/S99local.\\\$$
				mv /tmp/S99local.\\\$$ /etc/rc.d/rc.local
				chmod 755 /etc/rc.d/rc.local
				exit 1
			fi

		else
			sed -e "s/^\/var\/lib\/ensim\/wpsetup.sh.*//g" /etc/rc.d/rc.local > /tmp/S99local.\\\$$
			mv /tmp/S99local.\\\$$ /etc/rc.d/rc.local
			chmod 755 /etc/rc.d/rc.local
			cd $KSGENDIR
			echo ""
		#	F_ckyorn "y"  "> Do you want to create an OSInstallation floppy for installing webppliance on the target server ?"
 		#	if [ $_q_yorn = "y" ]
		#	then
		#		_q_installer="y"
		#	else 
		#		echo ""
				echo "Please correct the above errors first before retrying."	
				echo "Refer to the documentation for more details"
				exit 1
		#	fi
		fi
	else 
		_q_installer="y"
   	fi	
}
F_Get_wpinstall()
{
	
	echo ""	
	echo "> Select the location to download the $_q_choice installer from ."
        F_ckyorn "y" "> Do you wish to download from Ensim's ftp server [ftp://ftp.ensim.com] ?"
        
	if [ $_q_yorn = "y" ]
        
	then
	       
		return
 
	else
			_q_local=1
    		
			local _q_done="n"
			F_read_ftpserver $_q_choice
        		_q_done="y"
        		local  _q_ftpdone="n"
#This method is diff from F_name_passwd : it doen not encode @ to %40 for ncftp
			F_namepasswd
        		_q_ftpdone="y"
			_q_localhost="$_q_ftpserver"
			_q_localdir="$_q_ftpdir"
			_q_locallogin="$_q_ftpuser"
			_q_localpasswd="$_q_ftppasswd"
			echo ""
	  		echo "> The name of $_q_choice installer : $_q_installfile"		
        		F_ckyorn "y" "Is this correct?"
        		
		if [ $_q_yorn = "y" ]
		then	
			_q_name="$_q_installfile"	
		else
	  		echo "> Specify The name of $_q_choice installer : "		
			read _q_name
		fi
			
			_q_installfile="$_q_name" 
        
	fi
}
F_Get_updates()
{
	
	echo ""	
	echo ""	
	echo "> OSI by default downloads the updates from the various redhat ftp sites ."
        F_ckyorn "n" "> Do you have the updates downloaded already on a local ftp server?"
        
	if [ $_q_yorn = "n" ]
        
	then
	       
		return
 
	else
		_q_local_updates=1
		local _q_done="n"
		F_read_ftpserver  "RedHat7.3 Updates"
       		_q_done="y"
       		local  _q_ftpdone="n"
#This method is diff from F_name_passwd : it doen not encode @ to %40 for ncftp
		F_namepasswd
       		_q_ftpdone="y"
		_q_updates_host="$_q_ftpserver"
		_q_updates_dir="$_q_ftpdir"
		_q_updates_login="$_q_ftpuser"
		_q_updates_passwd="$_q_ftppasswd"
        
	fi
}

F_read_nfsserver()
{
	
    local _q_dir="n"
    while [ $_q_done = "n" ]
    do
        echo ""
        echo "> Please specify the NFS server (name or IP address) and the directory"
        echo "  exported by the server that contains the RedHat $_q_rhl Installation tree."
        echo ""
        echo -n "    NFS Server        : "
        read _q_nfsserver
        _q_dir="n"
 	while [ $_q_dir = "n" ]
	do
        echo -n "    Exported Directory: "
        read _q_nfsexport
        echo ""
	if [ ! `echo $_q_nfsexport | grep ^\/` ] || [ `echo $_q_nfsexport | grep \/$` ] && [ `echo $_q_nfsexport | wc --chars` -gt 2 ]
	then
	echo "The directory is invalid.It must start with '/' and  not end with '/'"
	echo ""
	else _q_dir="y"
	fi
	done
        echo "You specified" "$_q_nfsserver":"$_q_nfsexport"
        F_ckyorn "y" "Is this correct?"
        if [ $_q_yorn = "y" ]
        then
            _q_done="y"
        fi
    done
}
F_read_ftpserver()
{
	
    local _q_dir="n"
    while [ $_q_done = "n" ]
    do
        echo ""
        echo "> Please specify the FTP server (name or IP address) and the directory"
        echo "  that contains $1."
        echo ""
        echo -n "    FTP Server        : "
        read _q_ftpserver
        _q_dir="n"
 	while [ $_q_dir = "n" ]
	do
        echo -n "    Directory[Kindly Specify the absolute paths]: "
        read _q_ftpdir
        echo ""
	if [ ! `echo $_q_ftpdir | grep ^\/` ] || [ `echo $_q_ftpdir | grep \/$` ] && [ `echo $_q_ftpdir | wc --chars` -gt 2 ]
	then
	echo "The directory is invalid.It must start with '/' and  not end with '/'"
	echo ""
	else _q_dir="y"
	fi
	done
        echo "You specified" "$_q_ftpserver""/""$_q_ftpdir"
        F_ckyorn "y" "Is this correct?"
        if [ $_q_yorn = "y" ]
        then
            _q_done="y"
        fi
    done
}
F_read_httpserver()
{
    local _q_dir="n"
    while [ $_q_done = "n" ]
    do
        echo ""
        echo "> Please specify the HTTP server (name or IP address) and the directory"
        echo "  that contains the RedHat7.3 installation Tree."
        echo ""
        echo -n "    HTTP Server        : "
        read _q_httpserver
        _q_dir="n"
 	while [ $_q_dir = "n" ]
	do
	echo -n "    Directory		: "
        read _q_httpdir
        echo ""
	if [ ! `echo $_q_httpdir | grep ^\/` ] || [ `echo $_q_httpdir | grep \/$` ] && [ `echo $_q_httpdir | wc --chars` -gt 2 ]
	then
	echo "The directory is invalid . Directory  must start with '/' and should not end with '/'"
	echo ""
	else _q_dir="y"
	fi
	done
        echo "You specified" "$_q_httpserver""$_q_httpdir"
        F_ckyorn "y" "Is this correct?"
        if [ $_q_yorn = "y" ]
        then
            _q_done="y"
        fi
    done
}

#This method is diff from F_name_passwd : it doen not encode @ to %40 for ncftp

F_namepasswd()
{
	while [ $_q_ftpdone = "n" ]
    	do
       		echo ""
       		echo "> Please enter a username and password to access the above FTP url."
		echo ""
		echo -n "    FTP username [Enter \"ftp\" for anonymous FTP]       : "
        	read _q_ftpuser
		if [ X"$_q_ftpuser" = X"" ] || [ X"$_q_ftpuser" = X"ftp" ]
        	then
			_q_ftpuser="ftp"
        		echo ""
        		echo "You specified the following:"
        		echo "    Username: $_q_ftpuser"
        	else
			echo -n "    FTP password : "
        		read _q_ftppasswd
        		echo ""
        		echo "You specified the following:"
        		echo "    Username: $_q_ftpuser"
        		echo "    Password: $_q_ftppasswd"
		fi
               	F_ckyorn "y" "Is the above correct?"
        	if [ $_q_yorn = "y" ]
        	then
           		 _q_ftpdone="y"
        	fi
 	done		
}

F_name_passwd()
{
	while [ $_q_ftpdone = "n" ]
    	do
       		echo ""
       		echo "> Please enter a username and password to access the above FTP url."
		echo ""
		echo -n "    FTP username [Enter \"ftp\" for anonymous FTP]       : "
        	read _q_ftpuser
		if [ X"$_q_ftpuser" = X"" ] || [ X"$_q_ftpuser" = X"ftp" ]
        	then
			_q_ftpuser="ftp"
        		echo ""
        		echo "You specified the following:"
        		echo "    Username: $_q_ftpuser"
        	else
			echo -n "    FTP password : "
        		read _q_ftppasswd
        		echo ""
        		echo "You specified the following:"
        		echo "    Username: $_q_ftpuser"
        		echo "    Password: $_q_ftppasswd"
        	fi
		# If '@' in the password give a warning message
        	#
        	local _tmp=`echo $_q_ftppasswd | $AWK 'gsub("@", "%40", $1);'`
        	if [ X"$_tmp" = X"" ]
        	then
            		_q_tmp=""
        	else
		echo ""	
         	echo "*** WARNING: RedHat Installation will fail if there is @ in the passwd" 

        	fi
               	F_ckyorn "y" "Is the above correct?"
        	if [ $_q_yorn = "y" ]
        	then
           		 _q_ftpdone="y"
        	fi
		# Encode any '@' in the password
        	# RedHat does'nt support @ or %40 in the url 
        	#local _tmp=`echo $_q_ftppasswd | $AWK 'gsub("@", "%40", $1);'`
        	#if [ X"$_tmp" = X"" ]
        	#then
            	#	_q_tmp=""
        	#else
           	#		_q_ftppasswd="$_tmp"
        	#fi
 	done		
}


F_display_ftpurl()
{
    $CAT $KSGENDIR/lib/data/ftp.dat
}

F_read_ftpurl()
{
    echo ""
    echo "> Please enter the number corresponding to the FTP server you"
    echo "  wish to install RedHat from:"
    _q_ftpurl=""
    local _N_ftpurl=`wc -l $KSGENDIR/lib/data/ftp.dat | $AWK '{print $1}'`
    while [ X"$_q_ftpurl" = X"" ] || [ $_q_ftpurl -lt 1 ] || [ $_q_ftpurl -gt $_N_ftpurl ]
    do
        echo ""
        F_display_ftpurl
        echo ""
        echo -n "FTP location [Default is 1]: "
        read _q_ftpurl
        if [ X"$_q_ftpurl" = X"" ]
        then
            _q_ftpurl=1
            break
        fi
        _q_ftpurl=`echo $_q_ftpurl | $AWK '/^[0-9]+$/ {print}'`
    done
	
	if [ $_q_ftpurl == 2 ]
	then 
                local _q_done="n"
		F_read_ftpserver "RedHat7.3 installation Tree"
                _q_done="y"
    		local _q_ftpdone="n"
		F_name_passwd
    		_q_ftpdone="y"
		_q_ftp="$_q_ftpserver/$_q_ftpdir"
        	_q_ftpurl="$_q_ftpserver"
	else
    		local _q_ftpdone="n"
#		F_name_passwd
    		_q_ftpdone="y"
    		_q_ftp=`head -$_q_ftpurl $KSGENDIR/lib/data/ftp.dat | tail -1 | $AWK '{print $3 $4}'`
    		_q_ftpurl=`head -$_q_ftpurl $KSGENDIR/lib/data/ftp.dat | tail -1 | $AWK '{print $3 }'`

	fi
    if [ X"$_q_ftpuser" = X"" ] || [ X"$_q_ftpuser" = X"ftp" ]
    then	
    	 _q_ftpurl="ftp://$_q_ftp/"
    else
    	 _q_ftpurl="ftp://$_q_ftpuser:$_q_ftppasswd@$_q_ftp/"
    fi			
    return
}

F_display_httpurl()
{
    $CAT $KSGENDIR/lib/data/http.dat
}

F_read_httpurl()
{
    echo ""
    echo "> Please enter the number corresponding to the HTTP server you"
    echo "  wish to install RedHat from:"
    _q_httpurl=""
    local _N_httpurl=`wc -l $KSGENDIR/lib/data/http.dat | $AWK '{print $1}'`
    while [ X"$_q_httpurl" = X"" ] || [ $_q_httpurl -lt 1 ] || [ $_q_httpurl -gt $_N_httpurl ]
    do
        echo ""
        F_display_httpurl
        echo ""
        echo -n "HTTP location [Default is 1]: "
        read _q_httpurl
        if [ X"$_q_httpurl" = X"" ]
        then
            _q_httpurl=1
            break
        fi
        _q_httpurl=`echo $_q_httpurl | $AWK '/^[0-9]+$/ {print}'`
    done
	if [ $_q_httpurl = 1 ]
	then 
                local _q_done="n"
		F_read_httpserver
                _q_done="y"
		_q_http="$_q_httpserver/$_q_httpdir"
        	_q_httpurl="$_q_httpserver"
	else
    _q_http=`head -$_q_httpurl $KSGENDIR/lib/data/http.dat | tail -1 | $AWK '{print $2 $3 }'`
    _q_httpurl=`head -$_q_httpurl $KSGENDIR/lib/data/http.dat | tail -1 | $AWK '{print $2 }'`

	fi

    _q_httpurl="http://$_q_http/"
    return
}
F_read_image()
{
    local _q_done="n"
    echo "" 
    echo "> Please select the RedHat Linux 7.3 boot floppy :"
    echo "" 
    echo "  1 Standard Red Hat Linux boot floppy ."
    echo "  2 Custom   Red Hat Linux boot floppy ."
    echo ""
    while [ "$_q_done" = "n" ]
    do
        echo -n "Please enter the number corresponding to your choice: "
        read _q_image
	
        _q_image=`echo $_q_image | $AWK '/^[0-9]+$/ {print}'`
        case X"$_q_image" in
            X1)
                echo ""
                _q_done="y"
                break	
                ;;
            X2)
                _q_done="y"
				break
                ;;
            X*)
                continue
                ;;
        esac
    done	
}
F_read_installsource()
{   
    F_read_image
    _q_installsource=""
    local _q_done="n"
    echo "" 
    echo "> Please select the method for installing RedHat Linux 7.3:"
    echo "> You have the following installation options:"
    echo "" 
    echo "  1 CD-ROM (a Red Hat $RH_VERSION CD-ROM is required)"
    echo "  2 NFS    (an NFS server exporting the Red Hat $RH_VERSION installation tree)"
    echo "  3 FTP    (an FTP server serving the Red Hat $RH_VERSION installation tree)"
    echo "  4 HTTP   (a  Web server serving the Red Hat $RH_VERSION installation tree)"
    echo ""
    while [ "$_q_done" = "n" ]
    do
        echo -n "Please enter the number corresponding to your choice: "
        read _q_installsource
	
        local _q_yorn=""
        _q_installsource=`echo $_q_installsource | $AWK '/^[0-9]+$/ {print}'`
        case X"$_q_installsource" in
            X1)
                echo ""
                echo "    NOTE: You must insert the RedHat 7.3 CD (disc 1) in the CD-ROM drive"
                echo "          BEFORE you boot the system using this diskette!"
		echo "	  Also ensure that the system is set to boot from floppy drive."
		
                if [ $_q_image = 1 ] 
				then 
					_q_bootimage="$KSGENDIR/lib/image/boot.img"
                	_q_bootimage_W="$KSGENDIR\\lib\\image\\boot.img"
				else
					_q_bootimage="$KSGENDIR/lib/image/customboot.img"
                	_q_bootimage_W="$KSGENDIR\\lib\\image\\customboot.img"
				fi

                _q_done="y"
                break	
                ;;
            X2)
                if [ $_q_image = 1 ] 
				then 
                	_q_bootimage="$KSGENDIR/lib/image/bootnet.img"
                	_q_bootimage_W="$KSGENDIR\\lib\\image\\bootnet.img"
				else
					_q_bootimage="$KSGENDIR/lib/image/custombootnet.img"
                	_q_bootimage_W="$KSGENDIR\\lib\\image\\custombootnet.img"
				fi
                F_read_nfsserver 
                _q_done="y"
		break
                ;;
            X3)
                if [ $_q_image = 1 ] 
		then 
                	_q_bootimage="$KSGENDIR/lib/image/bootnet.img"
                	_q_bootimage_W="$KSGENDIR\\lib\\image\\bootnet.img"
		else
			_q_bootimage="$KSGENDIR/lib/image/custombootnet.img"
                	_q_bootimage_W="$KSGENDIR\\lib\\image\\custombootnet.img"
		fi
                F_read_ftpurl
                _q_done="y"
		break
                ;;
           X4)
                if [ $_q_image = 1 ] 
		then 
                	_q_bootimage="$KSGENDIR/lib/image/bootnet.img"
                	_q_bootimage_W="$KSGENDIR\\lib\\image\\bootnet.img"
		else
					_q_bootimage="$KSGENDIR/lib/image/custombootnet.img"
                	_q_bootimage_W="$KSGENDIR\\lib\\image\\custombootnet.img"
		fi
               F_read_httpurl
		break
               ;;
            X*)
                continue
                ;;
        esac
    done
}
F_read_lwpsource()
{   
    _q_lwpsource=""
    local _q_done="n"
    echo "" 
    echo "> Please select the method for installing WEBppliance for Linux:"
    echo "> You have the following installation options:"
    echo "" 
    echo "  1 CD-ROM (Ensim's WEBppliance CD-ROM is required)"
    echo "  2 FTP    (Ensim's FTP server )"
    echo ""
    while [ "$_q_done" = "n" ]
    do
        echo -n "Please enter the number corresponding to your choice: "
        read _q_lwpsource
	
        local _q_yorn=""
        _q_lwpsource=`echo $_q_lwpsource | $AWK '/^[0-9]+$/ {print}'`
        case X"$_q_lwpsource" in
            X1)
                echo ""
                _q_done="y"
				_q_cdrom="-c"
                break	
                ;;
            X2)
                _q_done="y"
				break
                ;;
            X*)
                continue
                ;;
        esac
    done
}
# __END__
