#!/bin/sh

make_install=
make_rescue=
make_live=
make_memtest=
make_mbr=
boot_only=
home_size=
image=
force=
verbose=
quiet=
iso_path=/media/cdrom



exit_handler() {
	        local rc=$?
		trap - EXIT
		if [ "$mpoint" ] ; then 
			chmod -R +w $mpoint
			say "syncing..."
			sync
			rm -f "$mpoint"/ldlinux.sys
			umount $mpoint >/dev/null 2>&1
			rmdir -- "$mpoint"
			syslinux $device
			umount $iso_path
		fi
		exit 
	}

trap exit_handler HUP PIPE INT QUIT TERM EXIT

die(){
	echo $1
	exit
}

say(){
	if [ "$verbose" ] ; then echo $1; fi
}

show_help() {
        cat <<EOF
mkbootflash - make FAT formatted USB storage suitable to initiate 
              network installation or be usable as rescue or live system.

Usage: mkbootflash [options] <path-to-device>
    -i, --install            make install image
    -r, --rescue             make rescue image
    -l, --live               make live image
    -m, --memtest            make memtest image
    -M, --MBR                write mbr
    -s, --size               size of home partition image in 512 bytes blocks
    -f, --force              force home image creation
    -b, --boot-only          don't copy images, do boot stuff only
    -p, --path		     path to a directory with rescue and/or live 
                             squashfs images
    -v, --verbose            print a message from each action
    -V, --version            print program version and exit
    -h, --help               show this text and exit

Report bugs to http://bugzilla.altlinux.org/

EOF
exit
}

TEMP=`getopt -n mkbootflash -o M,i,v,r,q,V,h,l,p:,s:,f,m,b -l install,rescue,live,verbose,version,quiet,help,path:,size:,force,memtest,MBR,boot-only -- "$@"` || show_help


copy_cmd="rsync -a --no-o --inplace $verbose "

eval set -- "$TEMP"

while :; do
	case "$1" in
		-i|--install) make_install=1
		;;
		-r|--rescue)  make_rescue=1 
		;;
		-l|--live)  make_live=1 
		;;
		-m|--memtest)  make_memtest=1
		;;
		-M|--MBR)  make_mbr=1
		                 ;;
		-p|--path)  shift
		            iso_path=$1
		;;
		-s|--size)  shift
                            home_size=$1
                            ;;
                -f|--force) force=-v
                ;;
		-b|--boot-only) boot_only=1
                ;;
	        -v|--verbose) verbose=-v
		;;
		-q|--quiet) quiet=1
		;;
		-h|--help) show_help
		;;
		--) shift; break
		;;

	esac
	shift
done

device=$1


if [ "$make_rescue" -o "$make_live" ] ; then
	image=rescue
	if [ "$make_live" ] ; then
		image=live
	fi
fi

# Exactly one argument, please.
[ "$#" -ge 1 ] || show_help 'Insufficient arguments.'
[ "$#" -le 1 ] || show_help 'Too many arguments.'

mpoint=`mktemp -d -t mkbootflash-mount.XXXXXXXXXX`

mount_options="-o quiet"
[ -b $device ] || mount_options="$mount_options,loop"
mount $mount_options $device $mpoint || die "can't mount"
if [ -d "$iso_path/isolinux" ] ; then
     $copy_cmd $verbose "$iso_path/isolinux/" $mpoint/syslinux || die "can't copy $iso_path/isolinux to $mpoint"
else
     $copy_cmd $verbose "$iso_path/syslinux/" $mpoint/syslinux || die "can't copy $iso_path/syslinux to $mpoint"
fi
subst 's/automatic=method:cdrom/                      /g' $mpoint/syslinux/bootlogo



cat > $mpoint/syslinux/syslinux.cfg <<-EOF
TIMEOUT 100
GFXBOOT bootlogo
PROMPT 1
IMPLICIT 1
DEFAULT linux 
EOF

UUID=`/sbin/blkid | grep $device | sed -e 's/.*UUID=\"\([^"]*\)*\".*/\1/'`
if [ $make_install ] ; then
	echo "making install syslinux.cnf"
	if [ ! $boot_only ] ; then
		mkdir -p $mpoint/ALTLinux
		$copy_cmd $iso_path/ALTLinux/RPMS.main $mpoint/ALTLinux/
		$copy_cmd $iso_path/ALTLinux/base $mpoint/ALTLinux/
		$copy_cmd $iso_path/altinst $mpoint/
		mkdir -p $mpoint/Metadata/
		$copy_cmd $iso_path/Metadata/pkg-groups.tar $mpoint/Metadata/
	fi
	cat >> $mpoint/syslinux/syslinux.cfg <<-EOF
LABEL linux
KERNEL alt0/vmlinuz
APPEND initrd=alt0/full.cz lang=ru_RU automatic=method:disk,uuid:$UUID ramdisk_size=128000 showopts
EOF
fi

if [ $make_rescue ] ; then
	    echo "making rescue syslinux.cnf"
	    if [ ! $boot_only ] ; then
	         $copy_cmd $iso_path/rescue $mpoint
	    fi
	    cat >> $mpoint/syslinux/syslinux.cfg <<-EOF

LABEL rescue
KERNEL alt0/vmlinuz
APPEND initrd=alt0/full.cz lowmem lang=ru_RU automatic=method:disk,uuid:$UUID fastboot stagename=rescue showopts
EOF

subst 's/DEFAULT.*/DEFAULT rescue/'  $mpoint/syslinux/syslinux.cfg 
fi



if [ $make_live ] ; then
		say "copying live image"
		if [ ! $boot_only ] ; then
		      $copy_cmd $iso_path/live $mpoint
	        fi
		say "making live syslinux.cnf"
		cat >> $mpoint/syslinux/syslinux.cfg <<-EOF

LABEL live
KERNEL alt0/vmlinuz
APPEND initrd=alt0/full.cz lowmem lang=ru_RU automatic=method:disk,uuid:$UUID fastboot stagename=live showopts
EOF

subst 's/DEFAULT.*/DEFAULT live/'  $mpoint/syslinux/syslinux.cfg
fi

if [ $make_memtest ] ; then
	say "making memtest syslinux.cnf"
	cat >> $mpoint/syslinux/syslinux.cfg <<-EOF

LABEL memtest
KERNEL memtest 
EOF
fi

if [ "$make_mbr" ] ; then
boot_device=`echo $device | sed 's/[0-9]*$//'` 
part_number=`echo $device | sed 's,[/a-z]*,,'`


dd if=/usr/lib/syslinux/mbr.bin of=$boot_device
sfdisk "$boot_device" -A $part_number
fi 

