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

F_kscreat()
{
    echo "# Copyright (C) 2002 Ensim Corporation. All Rights Reserved."
    echo "#"
    echo "# This file is automatically created by the Ensim installer. Do not edit."
    echo "# @ v $EOSI_VERSION"
    echo "#"
    echo ""
    echo "# language"
    echo "lang $_q_lang"
    echo ""
    echo "langsupport --default en_US en_US"
    echo ""
    echo "# networking"
    echo "network --bootproto static --ip $_q_ipaddr --netmask $_q_netmask --gateway $_q_gateway --nameserver $_q_nameserver --hostname=$_q_hostname"
    echo ""
    echo "# installsource"
    case X"$_q_installsource" in
            X1)
                echo "cdrom"
                ;;
            X2)
                echo "nfs --server $_q_nfsserver --dir $_q_nfsexport"
                ;;
            X3)
                echo "url --url $_q_ftpurl"
                ;;
            X4)
                echo "url --url $_q_httpurl"
                ;;
            *)
                # Totally messed up.
                ;;
   esac
   echo ""
  
   echo "# keyboard"
   echo "keyboard $_q_kb"
   echo ""
   echo "# mouse"
   echo "mouse $_q_mouse"
   echo ""
   echo "# disk"
   echo "zerombr yes"
   if [ $_q_clear = "n" ]
   then	
   	echo "clearpart --linux --initlabel"
   else
   	echo "clearpart --all --initlabel"
   fi	
   echo "part /boot --fstype ext3 --size=$_q_bootsize"
   echo "part swap --size=$_q_swapsize --grow --maxsize=$_q_maxswapsize"
   echo "part /    --fstype ext3 --size=$_q_rootsize --grow"
   echo ""

   echo "## Optional PLEASE SEE RED HAT REFERENCE GUIDE"
   echo "#part /home --size 500"
   echo ""
   echo "## This should fill up the first drive in a multi drive system"
   echo "#part /var --size 1024 --grow"
   echo ""

   echo ""
   echo "# install or upgrade"
   echo "install"
   echo ""
   echo "# timezone"
   echo "timezone $_q_tz"
   echo ""
   echo "# root password"						
   echo "rootpw $_q_passwd"							
   echo ""								
   echo "# skip X"
   echo "skipx"
   echo ""
   echo "#text mode "
   echo "text"
   echo ""
   echo "# authentication"
   echo "auth --useshadow --enablemd5"
   echo ""
   echo "# LILO"
	
   if [ X"$_q_boot" == X"y" ]
   then	
		if [ X"$_q_grubpasswd" != X"" ] 
		then
   			echo "bootloader  --location mbr --password $_q_grubpasswd"
		else
   			echo "bootloader  --location mbr"
		fi
   else	
   		echo "bootloader  --location mbr --useLilo"
   fi
   echo ""
   echo "# Packages"
   echo "%packages"
    $CAT $_q_packages
   echo ""

    # Post-installation
    #
    echo "# Post-installation"
    echo "#"
    echo "%post"

    # Fix shadow just in case
    #
    echo "/usr/sbin/authconfig --enablemd5 --useshadow --kickstart"

    # Add user
    #
    echo "# Add user"
    echo "#"
    echo "/usr/sbin/useradd -c \"Ensim Administrator\" -m admin"
    echo "echo \"$_q_adminpasswd\" | /usr/bin/passwd --stdin admin"
    # Populate /boot/message
    #
    echo "# Populate /boot/message"
    echo "#"
    echo "echo \"Ensim $_q_choice Copyright (C) 2002 Ensim Corporation. All Rights Reserved.\" > /boot/message"

    # Reconstruct /etc/resolv.conf
    #
    echo "# Reconstruct /etc/resolv.conf"
    echo "#"
    echo "/bin/grep search /etc/resolv.conf > /tmp/xresolv.conf"
    echo "/bin/grep nameserver /etc/resolv.conf >> /tmp/xresolv.conf"
    if [ ! X"$_q_nameserver2" = X"none" ]
    then
        echo "echo \"nameserver $_q_nameserver2\" >> /tmp/xresolv.conf"
    fi
    if [ ! X"$_q_nameserver3" = X"none" ]
    then
        echo "echo \"nameserver $_q_nameserver3\" >> /tmp/xresolv.conf"
    fi
    if [ ! X"$_q_nameserver4" = X"none" ]
    then
        echo "   echo \"nameserver $_q_nameserver4\" >> /tmp/xresolv.conf"
    fi
    echo "/bin/mv -f /tmp/xresolv.conf /etc/resolv.conf"
	
#   ## Set hostname
#   echo ""	
#   echo "## Set hostname"
#   echo "#"
#   echo "grep -v HOSTNAME /etc/sysconfig/network | grep -v DOMAINNAME > /tmp/network.\\\$$"
#   echo "echo DOMAINNAME=$_q_domainname >> /tmp/network.\\\$$"
#   echo "echo HOSTNAME=$_q_hostname >> /tmp/network.\\\$$"
#   echo "/bin/hostname $_q_hostname.$_q_domainname"
#   echo "/bin/domainname $_q_domainname"
#   echo "cp /tmp/network.\\\$$ /etc/sysconfig/network"
#   echo "rm -f /tmp/network.\\\$$"

    # "Fix" services
    #
    echo "# Fix services"
    echo "#"
    echo "/sbin/chkconfig --level 2345 apmd off"
    echo "/sbin/chkconfig --level 2345 atd off"
    echo "/sbin/chkconfig --level 2345 gpm off"
    echo "/sbin/chkconfig --level 2345 keytable off"
    echo "/sbin/chkconfig --level 2345 netfs off"
    echo "/sbin/chkconfig --level 2345 portmap off"
    echo "/sbin/chkconfig --level 2345 rpc.statd off"
    echo ""


    # Reinstall LILO
    #
    echo "# Reinstall LILO"
    echo "#"
   	if [ X"$_q_boot" == X"n" ]
    then
		 echo "/sbin/lilo"
    	 echo ""
	fi
    
    # Setup post-boot installation
    #
    echo "# Setup post-boot installation"
    echo "#"
    echo "mkdir -p /var/lib/ensim"
    echo "mkdir -p /var/log/ensim"
	echo "mkdir -p /tmp/oem/apt/ensim"
    echo "touch /usr/local/bin/ensim-update"
    echo "chmod 755 /usr/local/bin/ensim-update"
    echo "touch /var/lib/ensim/aptcd"
    echo "chmod 755 /var/lib/ensim/aptcd"
    echo "touch /var/lib/ensim/cdsetup.sh"
    echo "chmod 755 /var/lib/ensim/cdsetup.sh"
    
	#/usr/local/bin/ensim-update generation
	#
awk '{print}' << WPSETUPGEN
awk '{print}' > /usr/local/bin/ensim-update<< WPSETUPGEN_0
#! /bin/sh
# Copyright (C) 2002 Ensim Corporation. All Rights Reserved.
#
get_file()
{
    ncftpget=/usr/bin/ncftpget
	copy=/bin/cp
   	local_dir=\\\$1
    remote_file=\\\$2
	result=0
    status=0
	cdrom=\\\$3

	if [ X"\\\$cdrom" != X"-c" ] 
	then
		if [ -f \\\$local_dir/\\\$remote_file ]; then rm -f \\\$local_dir/\\\$remote_file 
		fi

		\\\$ncftpget -u \\\$FTP_LOGIN -p \\\$FTP_PASSWD -r 3 -t 60 -VZF \\\$FTP_SERVER \\\$local_dir \\\$OSI_DATA_DIR/\\\$remote_file
		result=\\\$?
	elif [ ! -f \\\$local_dir/\\\$remote_file ]; then
		status=1	
	fi

		if [ \\\$result -ne 0 ]; then
		status=1
		fi
		return \\\$status
}
fail()
{
	echo "*** Failure obtaining the required data ." >> /tmp/updateserr
	echo "###`uname -a`###" >> /tmp/updateserr
	mail -s "\\\$HOSTNAME[Error]:Initializing Setup ."  \\\$2 < /tmp/updateserr
}

		export FTP_SERVER=$FTP_SERVER
		export OSI_DIR=$OSI_DIR
		export OSI_version=$OSI_version
		export OSI_release=$OSI_release
		export OSI_DATA_DIR=$OSI_DATA_DIR
		export OSI_RPMS_DIR=$OSI_RPMS_DIR
		export OSI_PUC_DIR=$OSI_PUC_DIR
		export FTP_LOGIN=$FTP_LOGIN
		export FTP_PASSWD=$FTP_PASSWD
		_q_downloaddata="/var/lib/ensim"
	
		if [ X"\\\$4" != X"-c" ]
		then
			echo "Downloading required data from  ftp server ....."
			if ! get_file \\\$_q_downloaddata ensim-installer.sh \\\$4 ; then
				fail
				exit 1
			fi
		else 
			sh /var/lib/ensim/cdsetup.sh $OSI_version $OSI_release
		fi
			
		if [ ! -f \\\$_q_downloaddata/ensim-installer.sh ]; then
				fail
				exit 1
		fi

	echo "$_q_email" > /var/lib/ensim/email
	if [ -f /var/lib/ensim/ensim-installer.sh ]
	then
		sh /var/lib/ensim/ensim-installer.sh \\\$1 \\\$2 \\\$3 \\\$4 \\\$5 \\\$6
		if [ \\\$? == 0 ]
		then
			echo "Your system is updated successfully..."
			exit 0
		else
			echo ""
   			echo "Kindly refer to the error logs mailed to you for details."
			echo ""
			exit 1
		fi
	else
    	echo "Error connecting/downloading from Ensim's ftp server"
		exit 1
	fi

# __END__
WPSETUPGEN_0
WPSETUPGEN
	
	#/var/lib/ensim/aptcd generation
	#
awk '{print}' << WPSETUPGEN
awk '{print}' > /var/lib/ensim/aptcd << WPSETUPGEN_0
#! /bin/sh
# Copyright (C) 2002 Ensim Corporation. All Rights Reserved.
#
	apt-cdrom --no-act add >apt.out.$$ 2>&1 << EOF

Linux WEBppliance Pro
EOF
	ret=\\\$?
	if [ "\\\$ret" -eq 0 ]; then
		exit 0
	else 
		exit 1
	fi

# __END__
WPSETUPGEN_0
WPSETUPGEN
	#/var/lib/ensim/cdsetup.sh generation
	#
awk '{print}' << WPSETUPGEN
awk '{print}' > /var/lib/ensim/cdsetup.sh << WPSETUPGEN_0
#! /bin/sh
# Copyright (C) 2002 Ensim Corporation. All Rights Reserved.
#
_q_updateslog=/var/log/ensim/updates.log
_q_updateserr=/var/log/ensim/updates.err
touch \\\$_q_updateslog
touch \\\$_q_updateserr
OSI_version=\\\$1
OSI_release=\\\$2

F_read_CD()
{
	echo "copying the rpms from the disc ..."
	/bin/mount /mnt/cdrom 2>>/dev/null
	if [ \\\$? == 0 ]
	then
		if [ -d /mnt/cdrom/OSI/\\\$OSI_version/\\\$OSI_release ]
		then
			cp -rf /mnt/cdrom/OSI /tmp/oem/apt/ensim/
			cp -rf /mnt/cdrom/OSI/\\\$OSI_version/\\\$OSI_release/data/ensim-installer.sh /var/lib/ensim/ensim-installer.sh
			if [ -d /tmp/oem/apt/ensim/LWP ] ; then rm -rf /tmp/oem/apt/ensim/LWP ; fi
			/bin/mv -f /tmp/oem/apt/ensim/OSI/\\\$OSI_version/\\\$OSI_release/LWP /tmp/oem/apt/ensim
			echo "$SUCCESS copying the rpms from the disc." >> \\\$_q_updateslog
			/bin/umount /mnt/cdrom 2>>/dev/null
			return 0
		else
			echo "FAILURE copying the rpms from the disc." >> \\\$_q_updateserr
			echo "Please ensure you have the correct CD." >> \\\$_q_updateserr
			echo "FAILURE copying the rpms from the disc." >> \\\$_q_updateslog
			echo "Please ensure you have the correct CD."
			/bin/umount /mnt/cdrom 2>>/dev/null
			return 1
		fi
	else
			echo "FAILURE mounting the CD-ROM." >> \\\$_q_updateserr
			echo "FAILURE mounting the CD-ROM." >> \\\$_q_updateslog
			return 1	
	fi
}
err_cnt=0
while [ \\\$err_cnt -lt 2 ];do 
	echo ""
	echo "Please insert the Ensim WEBppliance Pro CD in the CD-ROM drive"
	eject
	echo -n "Press <ENTER> to continue, or ^C to abort."
	read _q_anykey
	echo ""
	F_read_CD
		if [ \\\$? = 0 ] 
		then
			echo "Successfully added the Disc to the Repository" 
			err_cnt=2
		else
			echo "Error reading from disc."
			let err_cnt=err_cnt+1
			echo ""
			if [ \\\$err_cnt == 2 ] 
			then
					echo "Please ensure you have the correct CD."
					exit 1
			fi
		fi
done
# __END__
WPSETUPGEN_0
WPSETUPGEN
	echo "echo \"/usr/local/bin/ensim-update -e "$_q_email" -k $_q_cdrom -r -u 2>>/var/log/ensim/updates.log\" >> /etc/rc.d/rc.local "
echo "/bin/dd if=/dev/zero of=/dev/fd0 count=1 bs=1024"
echo "/sbin/reboot -f"
}
