#!/bin/sh
#This script create first rescue disk for double rescue disks.
#Next run mkrd.sh.
version=0.2.4-rc2
PATH="$PATH:/sbin:/usr/sbin"
bd_pwd=`pwd`

bd_exit() { echo "Unexpected error occured. The operation was aborted." >&2
	    cd ${2:-..}

	    if [ "$1" ];then

		echo "In debug mode, not umouted fd_root" >&2
		exit 1
	    else

		umount fd_root
		rm -fr fd_root
		exit 1
	    fi

	     }

#permission

if [ `id -u` != 0 ];then

    echo "You need the privilege of root to execute `basename $0`." >&2
    echo "aborted" >&2
    exit 1

fi


#Options start.

while [ "$1" ];do

    case $1 in

	"-k" ) shift

            if [ ! $1 ];then
		
                echo "A kernel image is not specified." >&2
                echo "An example," >&2
                echo "#`basename $0` -k /boot/bzImage" >&2
                exit 1

            fi

	    if [ ! `echo $1|grep ^/ ` ];then
		
		echo "$1 must be absoute path." >&2
		exit 1

	    fi

            if [ -f $1 ];then

                kernel_image=$1
		
	    elif [ -d $1 ];then

		echo "$1 is directory." >&2
		exit 1

	    else

		echo "$1 is not found." >&2
		exit 1

            fi

	    shift

	    ;;

	"-i" ) shift
	    
            if [ ! $1 ];then

                echo "-i option need an absolute path of an initrd.image or 0."  >&2
                echo "( ex:`basename $0` -i /boot/INITRD or `basename $0` -i 0 )" >&2
		echo "aborted." >&2
		exit 1
	    else

		if [ ! -f $1 ]&&[ $1 != 0 ];then
		    echo "$1,such file is not found." >&2
		    exit 1
		fi

            fi

            case $1 in

                "0" ) initrd_val=0

                ;;

                /* ) if [ -f $1 ];then
                        initrd_val=$1
                    fi         

                ;;

                *)  echo "-i option need an absolute path of an initrd.image or 0."  >&2
		    echo "( ex:`basename $0` -i /boot/INITRD or `basename $0` -i 0 )" >&2
		    echo "aborted." >&2
 		    exit 1

		;;

	    esac

    	    shift

	    ;;

	"-m" ) shift
	    lilo_msg=$1
	    shift
	    ;;

	"-d" ) shift
	    debug=1
	    ;;

	"-R" ) shift
	    root_dev=$1
	    shift
	    ;;

	"-e" ) shift
	    fd288=1
	    fd144=0
	    fd_size=288
	    r_sz=$((64*1024))
	    ;;
	
	"-H" ) shift
	    fd288=0
	    fd144=1
	    fd_size=144
	    fd_sec=18
	    r_sz=$((64*1024))
	    ;;

	"-l" ) shift

	    if [ "$1" ];then
		label=$1
	    else
		echo "label is not defined." >&2
		exit 1
	    fi
	    shift 
	    ;;    
	
	"-v" )
 	    echo "mkbd.sh: $version"
	    exit 0
	    ;;

	*)
	    echo "$1, Unexpected value was spcified, aborted." >&2
	    exit 1
	  
	    ;;
	
    esac
    
done

if [ ! "$label" ];then
    label=rescue
fi

if [ ! "$kernel_image" ];then

    if [ ! -f /boot/vmlinuz ];then

	echo "Default kernel image /boot/vmliuz is not found." >&2
	echo "You need use -i option to specify absolute path of an kernel image." >&2
	echo "aborted." >&2
	exit 1

    else
	kernel_image=/boot/vmlinuz
    fi

fi
    
if [ ! "$initrd_val" ];then
    initrd_val=0
fi

for t in "`mount |grep ' / '`" ;do
    org_root=`echo $t|cut -d' ' -f1`
    break  
done

if [ "$debug" ];then

    echo "kernel_image is $kernel_image."
    echo "initrd_val is $initrd_val." 
    echo "Stopped for debug."
    echo "Simply press Enter, and restart."
    echo "Any character entered, and abort."

    read dummy

    if [ "$dummy" ];then

	exit 0

    fi

fi
            
#options end.

echo "Set floppy disk on /dev/fd0, and simply press Enter."
echo "If any values set, this operation is aborted."

read dummy

if [ "$dummy" ];then
    
    echo "aborted."
    exit 0
    
fi

 
if [ `grep -c -e /dev/fd0 /etc/mtab` = 1 ];then

    umount /dev/fd0 || { echo "umount failure occured, the operation couldn't start." >&2
			    exit 1
			    }
fi

#    fdformat /dev/fd0 || { echo "fdformat failed,the operation couldn't start."
#				exit 1 
#				}
#			
#    mke2fs -m 0 /dev/fd0H1440 ||   { echo "mke2fs failed,the operation couldn't start."
#				     exit 1
#				     }
#
bd=bd.$$.img
rm -fr $bd
if [ "$fd288" = 1 ];then
    count=$((2880*2))
elif [ "$fd144" = 1 ];then
    count=2880
else
    count=2880
fi

dd if=/dev/zero of=$bd bs=512 count=$count
mke2fs -m0 -F $bd

dvn=`umount fd_root 2>&1 1>/dev/null`
rm -fr fd_root
if [ -d fd_root ];then
    echo fd_root is busy.
    exit 1
fi
mkdir fd_root

mount $bd fd_root -o loop || { echo "mount failure occured, the operation was aborted." >&2
				rm -fr fd_root
				exit 1
				}

cd fd_root
rmdir lost+found
mkdir boot
mkdir dev
mkdir etc

#/boot/boot.b is unneeded for lilo-22.x

lilo_v=`/sbin/lilo -V|cut -d" " -f3|cut -d"." -f1`
if [ $lilo_v -lt 22 ];then

    for bl in /boot/boot.b /boot/boot-text.b /boot/boot-menu.b
    do
	if [ -f $bl ];then
	    cp $bl boot/boot.b || bd_exit "$debug" "$bd_pwd"
	    break
	fi
    done

fi
cp $kernel_image boot/vmlinuz

if [ "$fd288" = 1 ]||[ "$fd144" = 1 ];then

    echo "initrd_val is ${initrd_val:-none} for fd${fd_size}."
    if [ ${initrd_val:-0} = 0 ];then
	ird_v=""
    else
	if [ -f $initrd_val ];then
	    ird_v="        initrd=/boot/initrd.img"
	    cp $initrd_val boot/initrd.img || bd_exit "$debug" "$bd_pwd"
	fi
    fi

elif [ "$initrd_val" != 0 ];then
    echo initrd is $initrd_val.
    ird_v="        initrd=/boot/initrd.img"
    cp $initrd_val boot/initrd.img || bd_exit "$debug" "$bd_pwd"

fi
#create lilo.conf
if [ "$fd288" = 1 ]||[ "$fd144" = 1 ];then

    loop=`mount|grep -e $bd|cut -d'=' -f2`
    loop=${loop%)}
echo "boot=$loop
disk=$loop 
bios=0x00
sectors=${fd_sec:-36} 
heads=2 	 
cylinders=80
timeout=1000
message=/boot/message
append=\"ramdisk_size=$r_sz\"
prompt
image=/boot/vmlinuz
        label=$label
        root=/dev/ram3
        vga=0x311
$ird_v
image=/boot/vmlinuz
        label=${root_dev:-$org_root}
        root=${root_dev:-$org_root}
        vga=0x311
$ird_v
image=/boot/vmlinuz
        label=rescue2
        append=\"load_ramdisk=2 prompt_ramdisk=1\"
        root=/dev/fd0
        vga=0x311
$ird_v
image=/boot/vmlinuz
        label=/dev/hda
        root=/dev/hda
        vga=0x311
$ird_v
" >etc/lilo.conf

else

echo "boot=/dev/fd0
timeout=1000
message=/boot/message
prompt
image=/boot/vmlinuz
        label=$label
        append=\"load_ramdisk=2 prompt_ramdisk=1\"
        root=/dev/fd0
$ird_v
" >etc/lilo.conf

fi

#create message
if [ -f ${lilo_msg:-0} ];then
    cp -f ${lilo_msg} boot/message
else
    echo "This boots rescue system. But correct root option make it boot normally.
An example, 
boot:rescue root=/dev/hda3 (It means that the root is on /dev/hda3.)   
" >boot/message
fi

#devices list
d_list="/dev/fd?
/dev/hd[ab]
/dev/hd[ab][1-9]
/dev/hd[ab]1[0-5]
/dev/scd[01]
/dev/sd[ab]
/dev/sd[ab][1-9]
/dev/sd[ab]1[0-5]
/dev/loop[0-7]
/dev/ram
/dev/ram[0-7]"

for cp_dev in $d_list ;do

    if [ ! -e $cp_dev ];then
	continue
    fi
    cp -a $cp_dev dev || bd_exit "$debug" "$bd_pwd"
done

cd ..
if [ "$fd288" = 1 ]||[ "$fd144" = 1 ];then
    /sbin/lilo -r fd_root;lilo_v=$?
fi

echo
if [ "$lilo_v" != 0 ];then
    echo "lilo faild!"
fi
echo "Write a floppy disk. Are you O.K.? If you are O.K. simply press ENTER."
echo "If any values set, this operation is aborted."

read dummy

if [ "$dummy" ];then
    
    echo "boot disk image is bd.$$.img."
    echo "aborted."
    umount fd_root
    exit 0
    
fi


dd if=$bd of=/dev/fd0 || bd_exit "$debug" "$bd_pwd" 

mount /dev/fd0 fd_root || bd_exit "$debug" "$bd_pwd"

cd fd_root

lilo -v -r `pwd` || bd_exit "$debug" "$bd_pwd"

if [ "$debug" ];then

    echo "kernel_image is $kernel_image."
    echo "initrd_val is $initrd_val." 

    echo "The operation has been stopped. To restart, press Enter."
    echo "If any values are entered, this operation are finished." 
    read dummy

    if [ "$dummy" ];then

	echo "The creation of first disk was finised."
	echo "But a little remains."
	exit 0

    fi
else

    rm -rf ../$bd

fi

cd ..

umount fd_root || { echo "umount failure occured, the operation was aborted." >&2
		    echo "$fd_root is NOT umounted, you should check the directory." >&2
		    exit 1
		   }


rm -fr fd_root

echo "The operation was completed."

exit 0

#Sat Jun 12 2004 yoshinobu syouzi e-mail:yoshino@rita.karing.jp
#                device file changed.
#2003/6/11  yoshinobu syouzi e-mail:yoshino@rita.karing.jp
