#!/bin/bash
#
#
#
#
#
    clear
    echo "ARE YOU READY TO CONTINUE?"
    echo "!!!!WARING!!! ALL DATA ON YOUR sda1 disk will be lost!!!!"
    echo "enter 'y' and press ENTER to continue"
    while [ -z $cont ] ; do read cont ; done
    if [ $cont = "y" ] ; then
	echo "enter a disk name you want to install the system on"
	echo "i.e. sda for a first SCSI or SATA disk or hda for first IDE drive"
	read disk
	echo "Entering configuration stage"
	echo "You should enter the following data to complete the installation"
	echo "Do you use DHCP to configure your network? Enter y or n"
	while [ -z $dhcp ];do read dhcp;done
	if [ $dhcp != "y" ]; then
	    echo "Please enter your IP in the following format ip/netmask length"	
	    while [ -z $ip ]; do read ip;done
	    echo "Please enter your default gateway"
	    while [ -z $gw ]; do read gw;done
	    echo "Enter your dns server IP"
	    while [ -z $ns ]; do read ns;done
	fi

	echo "Enter your hostname. WARNING!!! This should be the same hostname, as specified in your download area!"
	while [ -z $hn ];do read hn;done
	echo "Enter your video server's name"
	while [ -z $serv ];do read serv;done

	echo "Select the way you connect to internet"
	echo "Enter 'proxy' if you use a HTTP proxy server"
	echo "or enter n if you don't use any proxy" 

	while [ -z $dir ];do read dir;done
	if [ $dir = "proxy" ] ; then
	    echo "Enter proxy ip"
	    while [ -z $prip ];do  read prip;done
	    echo "Enter proxy port"
	    while [ -z $prport ];do read prport;done
	    echo "Enter remote port"
	    while [ -z $rport ];do read rport;done
	    echo "Enter local port"
	    while [ -z $lport ];do read lport;done
	    echo "Do you use an anonymous proxy? y or n"
	    while [ -z $anon ];do read anon;done
	    	if [ $anon = "n" ]; then
		    echo "Enter proxy username"
		    while [ -z $upname ];do read upname;done
		    echo "Enter proxy password"
		    while [ -z $ppasswd ];do read ppasswd;done
		fi
	    rsync_args=" -az -e ssh -p $lport $hn@localhost:~/store/ /store/tmp/ "
	else
	    rsync_args="-az -e ssh $hn@$serv:~/store/ /store/tmp/ "
	fi
clear

    mount -t iso9660 /dev/cdrom /mnt/cdrom
	
	echo "o
	n 
	p
	1
	
	+1000M
	
	t
	83
		
	n 
	p
	2
	
	+2000M
	
	t
	2
	82
	
	n 
	p
	3
	
	t
	3
	82
	
	w
	"|fdisk /dev/$disk -- >/dev/null


	mkfs.ext3 /dev/$disk"1" >/dev/null
	d1=$disk"1"
	mkswap /dev/$disk"2" >/dev/null
	d2=$disk"2"
	mkfs.ext3 /dev/$disk"3" >/dev/null
	d3=$disk"3"
	
	mount -t ext3 -o rw /dev/$d1 /rootfs
    	mkdir -p /rootfs/var/lib/rpm
        mkdir -p /rootfs/tmp/.private 
        echo "rootfs prepared for rpm database"
	rpmdb --initdb -r /rootfs/
        mkdir -p /rootfs/proc
        mkdir -p /rootfs/sys

	mount -t proc proc /rootfs/proc
	mount -t sysfs sysfs /rootfs/sys
        echo "rootfs prepared for installation"
        echo "installing"
	mkdir -p /rootfs/dev
#	rpm -ivh -r /rootfs /
	cpio -idv -pvd /rootfs < /mnt/cdrom/rpms/misc/devfs.cpio

        rpm -Uvh  -r /rootfs /mnt/cdrom/rpms/i586/RPMS.classic/* /mnt/cdrom/rpms/noarch/RPMS.classic/*        
	echo "Installation complete"
	
        echo "committing configuration"

echo "#!/bin/bash
">/etc/rc.d/rc.local

	if [ $dhcp = "y" ]; then
	    mkdir -p /rootfs/etc/rc.d
	    echo "dhcpcd eth0" >>/rootfs/etc/rc.d/rc.local
	    chmod a+x /rootfs/etc/rc.d/rc.local
	else
	    echo $ip >/rootfs/etc/net/ifaces/eth0/ipv4address
	    echo "default via $gw" >/rootfs/etc/net/ifaces/eth0/ipv4route
	    echo "nameserver $ns" >/rootfs/etc/resolv.conf
	    echo "TYPE=eth" > /rootfs/etc/net/ifaces/eth0/options
	    chroot /rootfs/ chkconfig network on
	fi

	if [ $dir = "proxy" ] ; then	
	    	if [ $anon = "n" ]; then	    
		    echo "htc -P $prip:$prport -A $upname:$ppasswd -F $lport $serv:$rport">>/etc/rc.d/rc.local
		else
		    echo "htc -P $prip:$prport -F $lport   $serv:$rport">>/etc/rc.d/rc.local
		fi
	fi
    echo "Preparing root filesystem configuration"
echo "proc	/proc	proc	gid=19	0	0
devpts	/dev/pts	devpts	gid=5,mode=620	0	0
/dev/$d1	/	ext3	defaults	1	1
/dev/$d2	swap	swap	defaults	0	0
/dev/$d3	/store	ext3	defaults	0	0
">/rootfs/etc/fstab
	
	echo "FSTAB is auto-created"
	
	echo "
	prompt    
	boot=/dev/$disk
	disk=/dev/$disk 
#	    0x80
	timeout=100
	root=/dev/$d1
	read-only
		
		image=/boot/vmlinuz
		label=linux-default
		initrd=/boot/initrd
		root=/dev/$d1

		image=/boot/vmlinuz
		label=linux-root-console
		root=/dev/$d1
		append=\" init=/bin/bash\"
	">/rootfs/etc/lilo.conf
	chroot /rootfs /sbin/mkinitrd /boot/initrd `uname -r`
	cp -a /dev/* /rootfs/dev/
	echo "LILO autoconfigured"
#	chroot /rootfs /sbin/service udevd start
	chroot /rootfs /sbin/lilo
#	chroot /rootfs /sbin/service udevd stop
	echo "Installing LILO on your hard drive"
	chattr -a /rootfs/tmp/.private
        echo "Installing patched scripts"
        echo "Installing fake display manager script"
        cp -af /mnt/cdrom/rpms/misc/dm /rootfs/etc/init.d/dm
        chmod a+x /rootfs/etc/init.d/dm
        chroot /rootfs chkconfig dm on --level 2345
	cp -af /mnt/cdrom/rpms/misc/showreklama /rootfs/usr/bin/
	chmod a+x /rootfs/usr/bin/showreklama
	chroot /rootfs /usr/sbin/useradd user -G wheel 
	chroot /rootfs passwd user <<EOF
admin
admin
EOF



        echo "DONE"
	
        echo "creating video update crond script"
	echo "#!/bin/bash 
ssh-keyscan -t rsa $serv >/root/.ssh/known_hosts
rsync_pid=`pidof rsync`
if [ \$rsync_pid -gt 1 ] then
echo \"rsync is already running\"
else
rsync -avz --delete-after $rsync_args 
service dm stop
rm -rf /store/out/*
rsync -avP --delete-after /store/tmp/ /store/out/
service dm start
rm -rf /store/tmp/*
fi

">/rootfs/usr/bin/updatevideo
	chmod a+x /rootfs/usr/bin/updatevideo
	echo "adding video update to cron"
	echo  "0,10,20,30,40,50,59	*	*	*	*	 /usr/bin/updatevideo
	" >/rootfs/tmp/crontab.stored
	echo "service crond start

	    crontab /tmp/crontab.stored
	    service crond stop
	    "> /rootfs/tmp/add2cron
	chroot /rootfs /bin/bash /tmp/add2cron
	chroot /rootfs /bin/ps auxwww
	

	echo "
%wheel ALL=(ALL) NOPASSWD: ALL
">>/rootfs/etc/sudoers
	mkdir -p /rootfs/store
	rm -rf /rootfs/dev/null
	mknod /rootfs/dev/null c 1 3
	mkdir -p /rootfs/root/.ssh
	cp -af /mnt/cdrom/rpms/misc/id_rsa /mnt/cdrom/rpms/misc/id_rsa.pub /rootfs/root/.ssh
	chmod 700 /rootfs/root/.ssh
	chmod 600 /rootfs/root/.ssh/id_rsa
	mkdir -p /roots/home/user
	rsync -avP --delete-after /etc/skel /rootfs/home/user
#mkdir -p /rootfs/home/user/.ssh/
	cp /mnt/cdrom/rpms/misc/id_rsa.pub /rootfs/home/user/.ssh/authorized_keys
	cp /mnt/cdrom/rpms/misc/id_rsa.pub /rootfs/home/user/.ssh/authorized_keys2
#	chroot /rootfs /rootfs/usr/bin/updatevideo 
	chroot /rootfs chkconfig dm on
	
	chroot /rootfs chkconfig sshd on
#	chroot /rootfs updatevideo
	umount /rootfs/proc
	umount /rootfs/sys

#	/bin/bash

	echo "
/usr/bin/updatevideo
" >>/rootfs/etc/rc.d/rc.local
mkdir -p /store/tmp
#	echo "nvidia
#	">/etc/modules
	umount /rootfs/proc
	umount /rootfs/sys
	umount /rootfs
	umount -a
	mount -o remount,ro /
#	mount /dev/$d3 /rootfs/store
	poweroff -i -p
	reboot
    else
	echo "Aborting istallation"
    fi
