#!/bin/bash
#
# 	DESCRIPTION:	configure configuration file of bootloader
#
#	SCRIPT NAME:	configure_bootloader_cnf.sh
#
#	Input:	
#			argument:
#			<-r|--root>	partition mount by root
#			[-b|--boot]	partition mount by boot (optional,if the boot is not mount by stand alone)
#			[-k|--kparameter]	kernel parameter
#			[-o|--osversion]	OS version
#
#	Output:	
#			NULL
#	Return value:
#			1 argument error
#			2 bootloader type doesn't specify
#			3 bootloader no implement
#			4 bootloader configuration file doesn't exist
#
#
#	AUTHOR:		Qin Bo
#
#	EMAIL:		bqin@linx-info.com
#
#	DATE:		2010-09-07
#
#	HISTORY:
#	REVISOR		DATE			MODIFICATION
#	Qin Bo		2010-09-07		create
#
#
#

source ./functions

usage ()
{
	if [ "$1" == "-n" ];then
		cmd="info"
		ret=0
	else
		cmd="err"
		ret=1
	fi
	$cmd "
This script will install boot loader for system

Usage: $0 options

options:
	[--help]      show this message
	<-t|--type >      boot loader type:grub,etc.
	<-r|--root>	partition mount by root
	[-b|--boot]	partition mount by boot (optional,if the boot is not mount by stand alone)
	[-k|--kparameter]	kernel parameter
	[-o|--osversion]	OS version
   "
   return $ret
}


configure_elilo_cnf()
{
      local root_dev 
      by_uuid=$(get_disk_sym $ROOT_PARTITION "by-uuid")
      if [ -n "$by_uuid" ];then
          root_dev="UUID=$(echo $by_uuid|sed 's/.*\///')"
          sed -i -e "s@root=@append=\"$KPARAMETER root=$root_dev rootdelay=2500\"@"  $ELILO_CNF
      else
          root_dev="/dev/$ROOT_PARTITION"
          sed -i "s@root=@root=$root_dev@g" $ELILO_CNF
          sed -i -e "/initrd=.*/i\  append=\"$KPARAMETER rootdelay=2500\""  $ELILO_CNF
      fi
}

configure_grub_cnf ()
{

	local root_hd
	local part_num
	local root
	local hd_dev
	local devname
	local root_dev


	# get the boot directory in which hard disk
	if [ -n "$BOOT_PARTITION" ];then
		root_hd=${BOOT_PARTITION%%[0-9]*}
		part_num=${BOOT_PARTITION##*[a-z]}
		let $((part_num-=1))
	else
		root_hd=${ROOT_PARTITION%%[0-9]*}
		part_num=${ROOT_PARTITION##*[a-z]}
		let $((part_num-=1))
	fi

	info "generate $DEVICEMAP"
# generate device.map
grub --batch --no-floppy --device-map="$DEVICEMAP" << EOF 1>>"$DEV_LOG" 2>&1
quit
EOF

	hd_dev=$(grep "$root_hd" "$DEVICEMAP" | awk '{print $1}' | sed 's/(//' | sed 's/)//')		#e.g. hd_dev=hd0
	info "hd_dev: $hd_dev part_num: $part_num" 
	root="($hd_dev,$part_num)"
	info "root: $root"
	
	# use UUID in menu.lst except md,
	# because when I use UUID of raid device in menu.lst, 
	# initrd can't switch root .
	devname=$(basename $ROOT_PARTITION)
	if [[ "$devname" =~ ^md[0-9]*$ ]];then
		root_dev="/dev/$ROOT_PARTITION"
	else
		by_uuid=$(get_disk_sym $ROOT_PARTITION "by-uuid")
		if [ -n "$by_uuid" ];then
			root_dev="UUID=$(echo $by_uuid|sed 's/.*\///')"
		else
			root_dev="/dev/$ROOT_PARTITION"
		fi
	fi
	info "root_dev: $root_dev"

    local boot
	if [ -n "$BOOT_PARTITION" ];then
        boot=
    else
        boot="/boot"
	fi
cat >>"$GRUBMENU"<< EOF
## LINX splash image
splashimage $root/boot/grub/linx.xpm.gz

## define special fore-/background colors for splash screen image
foreground = FFFFFF
background = AAAAAA

## time to wait for user interaction
timeout 10

## default boot kernel 
default 0

## Default menu entries
title LINX Rocky Secure Operating System Version $OSVERSION with root 
kernel $root$boot/vmlinuz-root root=$root_dev $KPARAMETER linx-root
initrd $boot/initrd-full-cgroup.gz

title LINX Rocky Secure Operating System Version $OSVERSION without root 
kernel $root$boot/vmlinuz-root-n root=$root_dev $KPARAMETER linx-no-root
initrd $boot/initrd-full-cgroup.gz

title LINX Rocky Secure Operating System Version $OSVERSION with root but no cgroup 
kernel $root$boot/vmlinuz-root-nocgroup root=$root_dev $KPARAMETER linx-root-nocgroup
initrd $boot/initrd-full-nocgroup.gz

title LINX Rocky Secure Operating System Version $OSVERSION without root but no cgroup
kernel $root$boot/vmlinuz-root-n-nocgroup root=$root_dev $KPARAMETER linx-no-root-nocgroup
initrd $boot/initrd-full-nocgroup.gz
EOF
chown 96.96 $GRUBMENU
}

configure_def_grub (){
	sed -i "s/GRUB_CMDLINE_LINUX=\"/&$KPARAMETER/" "$DEFGRUB"
}

main ()
{
	if [ $# -eq 0 ];then
		usage; erv
	fi

	tmp=$(getopt -o t:r:b:k:o: --long type:,root:,boot:,kparameter:,osversion:,help -- "$@" 2>>$DEV_LOG)
	if [ $? -ne 0 ];then
		usage; erv
	fi

	# set all argument to arg variable
	eval set -- "$tmp"

	while true ;do
		case "$1" in
			-t|--type) TYPE="$2" 
				info "bootloader type: $TYPE"
				shift 2
			;;
			-r|--root) ROOT_PARTITION="$2" 
				info "root partition: $ROOT_PARTITION"
				shift 2
			;;
			-b|--boot) BOOT_PARTITION="$2" 
				info "boot partition: $BOOT_PARTITION"
				shift 2
			;;
			-k|--kparameter) KPARAMETER="$2" 
				info "kernel parameter: $KPARAMETER"
				shift 2
			;;
			-o|--osversion) OSVERSION="$2" 
				info "version of opterating system: $OSVERSION"
				shift 2
			;;
			--help) usage -n; exit 0 ;;
			# shift the last "--", this dash is define by getopt (more information see man getopt)
			--) shift; break;;
			*) usage; erv ;;
		esac
	done

	if [ -n "$TYPE" ];then
		case "$TYPE" in
			grub)
				if [ -z "$ROOT_PARTITION" ]; then
					err "configure grub configuration file root partition can't be empty"
					exit 1
				fi
				configure_grub_cnf
				erv
				;;
			elilo)
				if [ -z "$ROOT_PARTITION" ]; then
					err "configure elilo configuration file root partition can't be empty"
					exit 1
				fi
				configure_elilo_cnf
				erv
				;;
			grub2)
				configure_def_grub
				erv
				;;
			*)
				err "$TYPE bootloader no implement yet !"
				exit 3
				;;
		esac
	else
		err "bootloader type doesn't specify"
		exit 2
	fi

}


TYPE=""
ROOT_PARTITION=""
BOOT_PARTITION=""
KPARAMETER=""
OSVERSION=""
GRUB_DIR="$TARGET/boot/grub"
GRUBMENU="$GRUB_DIR/menu.lst"
DEFGRUB="$TARGET/etc/default/grub"
DEVICEMAP="$GRUB_DIR/device.map"
ELILO_CNF="/tmp/tmppoint/efi/boot/elilo.conf"

# The quotes around '$@' are essential!
# in this way we can accept whitespace as parameter.
main "$@"
	

