#!/bin/sh

drv_basename=FastTrak
base_ver="2.4.18-14 2.4.20-8"
drvname="${drv_basename}.o"

display_usage()
{
	echo "If you install ${drv_basename} driver on existed OS, please run"
	echo "     sh ./install "
}
determine_lilo_or_grub()
{
   LILO_GRUB=0
   dd if=$1 of=/tmp/mbr_sect bs=512 count=1 >/dev/null 2>&1
   if [ $? != 0 ]; then
      return;
   fi
   GREP_RESULT=`grep GRUB /tmp/mbr_sect |awk '{print $4}'`
   if [ x"$GREP_RESULT" = "xmatches" ]; then
        LILO_GRUB=2        
   else
        LILO_GRUB=1
   fi
   rm -rf /tmp/mbr_sect >/dev/null 2>&1
}
if [ $# -ge 1 ];then
	echo "wrong parameter"
	display_usage
	exit 0
fi
os=`uname -r| awk '{
		               sub("smp","",$0)
		               sub("bigmem","",$0)
					   print $0
                   }'`
match_ver=0
for v in $base_ver ; do
	if [ x$os = x$v ];then
		match_ver=1
		break
	fi
done
if [ $match_ver = 0 ]; then
	echo "This disk does not contain any drivers of kernel version : `uname -r` "
	exit 1
fi
if [ ! -e modules.cgz ] ; then
	echo "This is not a correct promise install disk"
	exit 1
else
	cp modules.cgz /tmp/modules.gz
	cd /tmp;mkdir tmp;cd tmp
	gunzip ../modules.gz > /dev/null 2>&1
	cpio -idumv < ../modules > /dev/null 2>&1
	
    if [ x"$os" != x ]; then   
	if [ ! -d /lib/modules/${os}/kernel/drivers/scsi ];then
		mkdir -p /lib/modules/${os}/kernel/drivers/scsi
		cp ${os}/${drvname} /lib/modules/${os}/kernel/drivers/scsi
	else
		cp  -f ${os}/${drvname} /lib/modules/${os}/kernel/drivers/scsi/
	fi
	if [ ! -d /lib/modules/${os}smp/kernel/drivers/scsi ];then
		mkdir -p /lib/modules/${os}smp/kernel/drivers/scsi
		cp ${os}smp/${drvname} /lib/modules/${os}smp/kernel/drivers/scsi
	else
		cp -f ${os}smp/${drvname} /lib/modules/${os}smp/kernel/drivers/scsi/
	fi
    fi	

        cd ..;rm -f modules;rm -rf tmp
fi
	
if [ -d /mnt/sysimage ];then
pre=/mnt/sysimage
else
pre=
fi
output=/tmp/promise_log
output1=/tmp/promise1_log
initrd_name=initrd
grub_install=$pre/sbin/grub-install
grub_exec=$pre/sbin/grub
mkinitrd_name=$pre/sbin/mkinitrd
echo>$output; echo>$output1

	if [ -e /etc/modules.conf ];then
		cp -f /etc/modules.conf /etc/modules.conf.old
		if [ ! "$?"="0" ]; then
			echo "can not create file in /etc, install abort."
			exit 1
		fi
		cat /etc/modules.conf | grep -v ${drv_basename} > /etc/ftsx.tmp
		echo "alias scsi_hostadapter ${drv_basename}" >> /etc/ftsx.tmp
		mv -f /etc/ftsx.tmp /etc/modules.conf
	else
		echo "alias scsi_hostadapter ${drv_basename}" > /etc/modules.conf
	fi

if [ -d $pre/boot/grub ];then
	cd $pre/boot/grub
	if [ -e grub.conf ]; then

		cat grub.conf>grub.conf.old
		cat grub.conf|grep -v "^#" > t1
		cat t1|grep -v initrd > tmp
		count=`cat t1|grep kernel|wc -l`
		cx=1
		while [ $count -gt 0 ] ;do
			boot_pre=`cat grub.conf|grep -v '#'|grep kernel|cut -d " " -f 2|cut -d / -f 2|sed --silent ${cx}p`
			if [ $boot_pre = "boot" ]; then
			initrd_name=`cat grub.conf|grep -v '#'|grep kernel|cut -d " " -f 2|cut -d / -f 3|sed --silent ${cx}p|sed s/vmlinuz/initrd/`
			insert_initrd_name=/boot/$initrd_name
			rootdev=`cat grub.conf|grep -v '#'|grep kernel|cut -d " " -f 4|cut -d = -f 2|sed --silent ${cx}p|sed s/[0-9]$//`
			kernel_ver=`cat grub.conf|grep -v '#'|grep kernel|cut -d " " -f 2|cut -d / -f 3|sed --silent ${cx}p|sed s/vmlinuz-//`
			kernel_str=`cat grub.conf|grep -v '#'|grep kernel|cut -d " " -f 2|cut -d / -f 3|sed --silent ${cx}p`
			else
			initrd_name=`cat grub.conf|grep -v '#'|grep kernel|cut -d " " -f 2|cut -d / -f 2|sed --silent ${cx}p|sed s/vmlinuz/initrd/`
			insert_initrd_name=/$initrd_name
			rootdev=`cat grub.conf|grep -v '#'|grep kernel|cut -d " " -f 4|cut -d = -f 2|sed --silent ${cx}p|sed s/[0-9]$//`
			kernel_ver=`cat grub.conf|grep -v '#'|grep kernel|cut -d " " -f 2|cut -d / -f 2|sed --silent ${cx}p|sed s/vmlinuz-//`
			kernel_str=`cat grub.conf|grep -v '#'|grep kernel|cut -d " " -f 2|cut -d / -f 2|sed --silent ${cx}p`
			fi
			cx=`expr $cx + 1`
			count=`expr $count - 1`
		echo "initrd:$initrd_name">>$output
		echo "insert initrd:$insert_initrd_name">>$output
		echo "ver:$kernel_ver">>$output
		echo "rootdev:$rootdev">>$output
		echo "kernel:$kernel_str">>$output
if [ -e $pre/boot/$initrd_name ]; then
	#echo "initrd file exist, save as old"
	echo "mv -f $pre/boot/$initrd_name $pre/boot/$initrd_name.old">>$output
	rm -f $pre/boot/initrd_name.old
	mv -f $pre/boot/$initrd_name $pre/boot/$initrd_name.old
fi
			echo "$mkinitrd_name $pre/boot/$initrd_name $kernel_ver">>$output
			$mkinitrd_name $pre/boot/$initrd_name $kernel_ver
			
			if [ -e $pre/boot/$initrd_name ];then
ed tmp <<EOF >> $output 2>&1
g/$kernel_str /
a
	initrd $insert_initrd_name
.
w
q
EOF
			else 
				echo "no $initrd_name , error, exit"|tee -a $output 2>&1
				#exit 1
			fi

		done

	else
		echo "grub.conf not exist,exit"|tee -a $output 2>&1
		exit
	fi
else
	echo "directory $pre/boot/grub not exist , exit"|tee -a $output 2>&1
	exit 0
fi

mv -f tmp grub.conf
cat grub.conf>>$output 2>&1
################################

if [ -d $pre/etc ];then
	cd $pre/etc
	if [ -e lilo.conf ]; then
		cat lilo.conf>lilo.conf.old
		cat lilo.conf|grep -v "^#" > t1
		cat t1|grep -v initrd > tmp 
		count=`cat t1|grep image|wc -l`
		cx=1
		while [ $count -gt 0 ] ;do
			kernel_str=`cat lilo.conf|grep -v '#'|grep image|cut -d "=" -f 2|cut -d / -f 3`
			boot_pre=`cat lilo.conf|grep -v '#'|grep image|cut -d "=" -f 2|cut -d / -f 2|sed --silent ${cx}p`
			if [ $boot_pre = "boot" ]; then
			initrd_name=`cat lilo.conf|grep -v '#'|grep image|cut -d "=" -f 2|cut -d / -f 3|sed --silent ${cx}p|sed s/vmlinuz/initrd/`
			insert_initrd_name=/boot/$initrd_name
			rootdev=`cat lilo.conf|grep -v '#'|grep root|cut -d "=" -f 2|sed --silent ${cx}p|sed s/[0-9]$//`
			kernel_ver=`cat lilo.conf|grep -v '#'|grep image|cut -d "=" -f 2|cut -d / -f 3|sed --silent ${cx}p|sed s/vmlinuz-//`
			kernel_str=`cat lilo.conf|grep -v '#'|grep image|cut -d "=" -f 2|cut -d / -f 3|sed --silent ${cx}p`
			else
			initrd_name=`cat lilo.conf|grep -v '#'|grep image|cut -d "=" -f 2|cut -d / -f 2|sed --silent ${cx}p|sed s/vmlinuz/initrd/`
			insert_initrd_name=/$initrd_name
			rootdev=`cat lilo.conf|grep -v '#'|grep root|cut -d "=" -f 2|sed --silent ${cx}p|sed s/[0-9]$//`
			kernel_ver=`cat lilo.conf|grep -v '#'|grep image|cut -d "=" -f 2|cut -d / -f 2|sed --silent ${cx}p|sed s/vmlinuz-//`
			kernel_str=`cat lilo.conf|grep -v '#'|grep image|cut -d "=" -f 2|cut -d / -f 2|sed --silent ${cx}p`
			fi
			cx=`expr $cx + 1`
			count=`expr $count - 1`
		echo "initrd:$initrd_name">>$output
		echo "boot_pre:$boot_pre">>$output
		echo "insert initrd:$insert_initrd_name">>$output
		echo "ver:$kernel_ver">>$output
		echo "rootdev:$rootdev">>$output
		echo "kernel:$kernel_str">>$output
		if [ ! `echo "$initrd_name"|grep initrd` ];then
			echo "initrd name strange:$initrd_name, continue">>$output
			continue
		fi
		if [ 0 -a -e $pre/boot/$initrd_name ]; then
			#echo "initrd file exist, save as old"
			echo "maybe not mv -f $pre/boot/$initrd_name $pre/boot/$initrd_name.old">>$output
			#rm -f $pre/boot/initrd_name.old
			#mv -f $pre/boot/$initrd_name $pre/boot/$initrd_name.old
		fi
			#echo "$mkinitrd_name $pre/boot/$initrd_name $kernel_ver"
			#echo "$mkinitrd_name $pre/boot/$initrd_name $kernel_ver">>$output
			#$mkinitrd_name $pre/boot/$initrd_name $kernel_ver
			
			if [  1 -o  -e $pre/boot/$initrd_name ];then
ed tmp <<EOF >> $output 2>&1
g/$kernel_str$/
a
	initrd=$insert_initrd_name
.
w
q
EOF
			#else 
				#echo "no file $pre/boot/$initrd_name , error, exit"|tee -a $output 2>&1
				#exit 1
			fi

		done

	#else
		#echo "lilo.conf not exist,exit"|tee -a $output 2>&1
		#exit

		#new lilo.conf
		#cat tmp
		#echo "save lilo.conf as lilo.conf.old"
		mv -f tmp /etc/lilo.conf
	fi
else
	echo "directory $pre/etc not exist "|tee -a $output 2>&1
	#exit 0
fi
#####################################
cx_mod=0
cx_mod=`cat /proc/modules|grep ${drv_basename}|wc -l`
install_e=0
if [ $install_e -eq 0 ]; then
if [ $cx_mod -lt 1 ];then
	t_f=x
	echo "Detected: You are installing driver on "
	echo "an exist Os. Is it true(y/n)?"
	read t_f
	while [ "x$t_f" != "xy" -a "x$t_f" != "xn" ];do
		echo "Is it true(y/n)?"
		read t_f
	done
	if [ "x$t_f" = "xy" ];then
		install_e=1
	fi
fi
fi
if [ $install_e -eq 1 ]; then
	cx_mod=`cat /proc/modules|grep ${drv_basename}|wc -l`
	if [ $cx_mod -lt 1 ];then
		cx_scsimod=`lsmod|grep scsi_mod|wc -l`
		if [ $cx_scsimod -lt 1 ];then
			insmod scsi_mod>>$output 2>&1
		fi
		insmod ${drvname}>>$output 2>&1
	fi
	echo "install driver on a exist os finished"
	if [ -e /etc/lilo.conf ];then
        boot_device=`grep '^[^#]*boot=' /etc/lilo.conf | sed 's/boot=//'`
        determine_lilo_or_grub $boot_device
#	echo "if you use lilo as your os loader, please run "
#	echo "lilo"
#	echo "after install"
            if [ $LILO_GRUB = 1 ];then
               echo "System Use lilo as boot loader"
               lilo
            fi   
	fi
else
	exit 0
fi

#modify /boot/grub/device.map
if [ ! -e /boot/grub/stage1 -a ! -e /boot/stage1 ];then
	echo "Perhapse you are not use the default os loader"
	echo "grub, if you use lilo or else os loader"
	echo "please run \"lilo\" or other command "
	echo "according to the related document"
fi
cat $pre/boot/grub/device.map|tee -a $output 2>&1
echo "If the device map of above is not right, "|tee -a $output 2>&1
echo "please correct it and run install.sh again"|tee -a $output 2>&1
$grub_exec << EOF >> $output1 2>&1
root (hd0,0)
setup (hd0,0)
quit
EOF
echo "configuration finished."
