#!/bin/bash
#
#
#
dir=" "
#rpm="/usr/bin/rpm"
    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"
#    mount -t tmpfs tmpfs /tmp 
#    service udevd restart
    mount -t iso9660 /dev/cdrom /mnt/cdrom
    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 "o
	n 
	p
	1
	
	+500M
	
	t
	1
	83
		
	n 
	p
	2
	
	+200M
	
	t
	2
	82
	
	n 
	p
	3
	
	t
	3
	82
	
	w
	"|fdisk /dev/$disk -- 
	#parted /dev/sda mkpartfs primary ext2 0 2000
	#parted /dev/sda mkpartfs primary linux-swap 2000 3000
	#parted /dev/sda mkpartfs primary ext2 0 2000
	mkfs.ext3 /dev/$disk"1"
	d1=$disk"1"
	mkswap /dev/$disk"2"
	d2=$disk"2"
	mkfs.ext3 /dev/$disk"3"
	d3=$disk"3"
	
	mount -t ext3 -o rw /dev/$d1 /rootfs

	mount
	
	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
	    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 "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
		    while [ -z $ppasswd ];do read $ppasswd;done
		    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
	    rsync_args=" -az -e ssh -p $lport $hn@localhost:~/ /store "
	else
	    rsync_args="-az -e ssh $hn@$serv:~/ /store "
	fi
	
	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 "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=5
	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"

	if [ $dhcp != "y" ]; then
	    echo "configuring network"
    	    mkdir -p /rootfs/etc/net/ifaces/eth0
	    echo $ip >/rootfs/etc/net/ifaces/eth0/ipv4addr
    	    echo "default via $gw" >/rootfs/etc/net/ifaces/eth0/ipv4route
    	    echo "TYPE=eth" > /rootfs/etc/net/ifaces/eth0/options	
    	    echo "nameserver $ns" >/rootfs/etc/resolv.conf
	fi
	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
        echo "DONE"
	
        echo "creating video update crond script"
	echo "killall -s9 rsync
        rsync $rsync_args ">/rootfs/usr/bin/updatevideo
	echo "adding video update to cron"
	echo  "0	*	*	*	*	 /usr/bin/updatevideo
	30	*	*	*	*	 /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
	mkdir -p /rootfs/store
	rm -rf /rootfs/dev/null
	mknod /rootfs/dev/null c 1 3
	umount /rootfs/proc
	umount /rootfs/sys

	/bin/bash

	umount /rootfs/proc
	umount /rootfs/sys
	umount /rootfs
	umount -a
	mount -o remount,ro /
reboot
else
    echo "Aborting istallation"
fi

#mount -o remount,ro /

cpio --null -pvd new-dir
