#!/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 server login"
	while [ -z $hn ];do read hn;done
	echo "Enter your video server's name"
	while [ -z $serv ];do read serv;done
	echo "Enter remote ssh port to configure this box"
	while [ -z $rp ]; do read rp;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
		serv1="localhost"
	    rsync_args=" -az -e ssh -p $lport $hn@$serv1:~/store/ /store/tmp/ "
	else
		lport="22"
		serv1=$serv
	    rsync_args="-az -e ssh $hn@$serv:~/store/ /store/tmp/ "
	fi
clear

    mount -t iso9660 /dev/cdrom /mnt/cdrom >/dev/null
	
	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 2>/dev/null


	mkfs.ext3 /dev/$disk"1" >/dev/null 2>/dev/null
	d1=$disk"1"
	mkswap /dev/$disk"2" >/dev/null 2>/dev/null
	d2=$disk"2"
	mkfs.ext3 /dev/$disk"3" >/dev/null 2>/dev/null
	d3=$disk"3"
	
	mount -t ext3 -o rw /dev/$d1 /rootfs >/dev/null 2>/dev/null
    	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 /image/rpms/i586/RPMS.classic/* /image/rpms/noarch/RPMS.classic/*        
	echo "Installation complete"
	
        echo "committing configuration"

echo '#!/bin/bash
rm -rf /var/run/tunnel.pid 
'>>/rootfs/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
	    echo '
dhcp="y"
	    '>>/rootfs/etc/sysconfig/givc-server.conf
	    chroot /rootfs/ chkconfig network on
	else
	    mkdir -p /rootfs/etc/net/ifaces/eth0/
	    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	
		echo "
		dir=\"$dir\"
		prip=\"$prip\"
		prport=\"$prport\"
		upname=\"$upname\"
		ppasswd=\"$ppasswd\"
		serv=\"$serv\"
		lport=\"$lport\"
		rport=\"$rport\"
		anon=\"$anon\"
		">>/rootfs/etc/sysconfig/givc-server.conf
#	    	if [ $anon = "n" ]; then
#		    echo "htc -P $prip:$prport -A $upname:$ppasswd -F $lport $serv:$rport">>/rootfs/etc/rc.d/rc.local
#		else
#		    echo "htc -P $prip:$prport -F $lport   $serv:$rport">>/rootfs/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"
	clear
	echo "
	prompt    
	boot=/dev/$disk
	disk=/dev/$disk 
	vga=788
	timeout=5
	root=/dev/$d1
	read-only
		
		image=/boot/vmlinuz
		label=linux-default
		initrd=/boot/initrd
		root=/dev/$d1
		append=\" splash=silent root=/dev/$d1 ro  \"
		read-only
		
		image=/boot/vmlinuz
		label=linux-root-console
		root=/dev/$d1
		read-only
		append=\" root=/dev/$d1 ro init=/bin/bash \"
	">/rootfs/etc/lilo.conf
	echo "LILO autoconfigured"

	chroot /rootfs /sbin/mkinitrd /boot/initrd 2.6.18-std-smp-alt8
	cp -a /dev/* /rootfs/dev/
#	chroot /rootfs /sbin/service udevd start
	mount --bind /dev/ /rootfs/dev/
	chroot /rootfs /sbin/lilo
	sleep 150 s
	umount /rootfs/dev
#	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 off --level 2345
#	cp -af /image/rpms/misc/tunnel /rootfs/etc/init.d/tunnel
#	chmod a+x /rootfs/etc/init.d/tunnel
#	chroot /rootfs chkconfig dm off --level 2345
#	cp -af /image/rpms/misc/showreklama /rootfs/usr/bin/
#	chmod a+x /rootfs/usr/bin/showreklama
#	cp -af /image/rpms/misc/startshow /rootfs/usr/bin/
#	chmod a+x /mnt/cdrom/rpms/misc/startshow
#	cp -af /image/rpms/misc/blank_cursor /rootfs/usr/share/blank_cursor

	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 
date=`date`
echo "$date update started at $date" >>/store/log
rsync_pid=`pidof rsync|cut -f1`
if [ ! -z $rsync_pid ] ; then
echo $rsync_pid
else
'>>/rootfs/usr/bin/updatevideo
echo "
	rsync -avz --delete-after $rsync_args 
">>/rootfs/usr/bin/updatevideo

echo '    if ! [ -z `diff -r /store/tmp /store/out| sed = |head -n1` ] ; then
	service dm stop
	sleep 5s
	rm -rf /store/out/*
	rsync -avP --delete-after /store/tmp/ /store/out/ >>/store/log
	echo "$date starting video show script"
	service dm start
	else
	echo "$date there are no files to update" >>/store/log

    fi
fi
'>>/rootfs/usr/bin/updatevideo
	chmod a+x /rootfs/usr/bin/updatevideo
	echo "adding video update to cron"
	echo  "*	*	*	*	*	 su - -c /usr/bin/startshow
*	*	*	*	*	su - -c /etc/init.d/tunnel user
	" >/rootfs/tmp/crontab.stored
	echo "service crond start
	    crontab /tmp/crontab.stored
	    service crond stop
	    "> /rootfs/tmp/add2cron
	echo "
LANG=ru_RU.UTF-8
SUPPORTED=ru_RU
	">/rootfs/etc/sysconfig/i18n
	echo "
KEYTABLE=ruwin_ct_sh-UTF-8
">/rootfs/etc/sysconfig/keyboard 
echo "
SYSFONT=UniCyrExt_8x16

">/rootfs/etc/sysconfig/consolefont
	chroot /rootfs /bin/bash /tmp/add2cron
	chroot /rootfs /bin/ps auxwww
	chroot /rootfs /sbin/chkconfig hotplug on
	chroot /rootfs /sbin/chkconfig acpid on
	chroot /rootfs /usr/bin/x11createconfig > /rootfs/etc/X11/xorg.conf
	chroot /rootfs /usr/sbin/x11presetdrv
	chroot /rootfs /usr/bin/x11setupdrv
	chroot /rootfs /sbin/chkconfig x11presetdrv on 

	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 /image/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 /rootfs/home/user
	cp -af /rootfs/etc/skel/* /rootfs/home/user/
#mkdir -p /rootfs/home/user/.ssh/
mkdir -p /rootfs/root/.ssh
echo " Host updateserver
       Hostname      $serv1
       Port $lport
       RemoteForward $rp localhost:22
	User $hn
	StrictHostKeyChecking no
	ServerAliveInterval 60s
">>/rootfs/root/.ssh/config
mkdir -p /rootfs/home/user/.ssh/

echo "  Host updateserver
       Hostname      $serv1
       Port $lport
       RemoteForward $rp localhost:22
	User $hn
	StrictHostKeyChecking no
	ServerAliveInterval 60s
">>/rootfs/home/user/.ssh/config
echo "button 
">>/rootfs/etc/modules
	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
	cp /mnt/cdrom/rpms/misc/id_rsa /rootfs/home/user/.ssh/
	for f in /rootfs/etc/bootsplash/themes/current/images/*.jpg ; do
	resolution=`echo $f |cut -f8 -d "/" |sed s/bootsplash-//|sed s/silent-//|sed s/.jpg//`
	echo "resizing bootsplash for resolution $resolution"
	width=`echo $resolution|cut -f1 -d"x"`
	heigth=`echo $resolution|cut -f2 -d"x"`
f1=`echo $f|sed s/rootfs//`
echo "
version=3
state=1
fgcolor=0
bgcolor=0
tx=0
ty=0
th=$heigth
tw=$width
silentjpeg=$f1
jpeg=$f1
overpaintok=1
">/rootfs/etc/bootsplash/themes/current/config/bootsplash-$resolution.cfg
	convert -resize $resolution /image/rpms/misc/givc.jpg $f
	done
	chown -R 500:500 /rootfs/home/user/
	chmod 600 /rootfs/home/user/.ssh/id_rsa	
#	chroot /rootfs /rootfs/usr/bin/updatevideo 
	chroot /rootfs chkconfig udev on
	chroot /rootfs chkconfig hotplug on
	chroot /rootfs chkconfig dm on
	chroot /rootfs chkconfig sshd on
	chroot /rootfs chkconifg tunnel off
echo '
rm -rf /var/run/startshow.pid
if ! [ -f /boot/initrd-bootsplash ] ; then
/sbin/mkinitrd /boot/initrd-bootsplash `uname -r` 
rm /boot/initrd
ln -s /boot/initrd-bootsplash /boot/initrd
/sbin/lilo
fi
clear
'>>/rootfs/etc/rc.d/rc.local
	chmod a+x /rootfs/etc/rc.d/rc.local
echo "configuring apt for updates"
echo "
rpm ftp://ftp.altlinux.org/pub/distributions/ALTLinux/GIVC/pkgs i586 classic
rpm ftp://ftp.altlinux.org/pub/distributions/ALTLinux/GIVC/pkgs noarch classic
">>/rootfs/etc/apt/sources.list
echo "adding requested modules"
echo "
nvidia
button
">>/rootfs/etc/modules
	mkdir -p /rootfs/store/
	sync
#sleep 35s
	umount /rootfs/
	umount /rootfs/dev
	umount /rootfs/proc
	umount /rootfs/sys
	umount /rootfs
	umount -a
	sync
	mount -o remount,ro /rootfs
	mount -o remount,ro /
#	mount /dev/$d3 /rootfs/store
	reboot -h -f -i 
	reboot
    else
	echo "Aborting istallation"
    fi

