#!/bin/sh -e

PROG="${0##*/}"

if [ "${0:0:2}" = "./" ]; then
    . ../data/functions
    SCRIPTDIR=.
else
    . /usr/share/separator/functions
fi


print_usage()
{
    [ "$1" = 0 ] || exec >&2
    cat <<EOF
Usage: $PROG [options] <workdir>

$PROG generates isolinux-related things under <workdir>.
<workdir> should exist and contain results of 'mkliveroot' script.

Valid options are:
  -h, --help	display help screen
  -v, --version	display version information
		     				     
Report bugs to <rider@altlinux.org>
EOF
    [ -n "$1" ] && exit "$1" || exit
}

#parse command line options
TEMP=`getopt -n $PROG -o v,h -l help,version -- "$@"` || exit 1
eval set -- "$TEMP"

while :; do
    case "$1" in
	-h|--help) print_usage 0
	    ;;
	-v|--version) print_version; exit 0
	    ;;
	--) shift; break
	    ;;
	*) "unrecognized option: $1"
	    exit 1
	    ;;
    esac
    shift
done

[ -z "$@" ] && print_usage 0
WORKDIR=`realpath $@`

OUTDIR=$WORKDIR/out
PROFILE=$WORKDIR/profile
TMPDIR=$WORKDIR/tmp
ROOT=$TMPDIR/root

[ -d $OUTDIR -a -d $PROFILE -a -d $ROOT -a -d $TMPDIR ] || print_usage 1

. $PROFILE/config

if [ -z "$BOXES" ];then
    Info "BOXES not found, proccesing default box..."    
    BOXES="__FALSEGHRBDNSK"
fi

for BOX in $BOXES;do
    SYSLINUXCFG=
    ISOLINUXCFG=
    # use config for boxes, if exists
    if [ -f $PROFILE/$BOX ];then 
	Info "Making box bootable with $BOX configuration file"
	. $PROFILE/$BOX
	OUTDIR="$WORKDIR/out/$BOXID"
    else
	if [ "$BOX" != "__FALSEGHRBDNSK" ];then
	   Info "configuration file $BOX not found"
	   exit 1
	fi
    fi
    
    rm -rf -- $OUTDIR/isolinux
    mkdir -p $OUTDIR/isolinux/alt0

# syslinux stuff
    [ -z "$SYSLINUXCFG" ] && SYSLINUXCFG=syslinux.cfg.in
    [ -z "$ISOLINUXCFG" ] && ISOLINUXCFG=isolinux.cfg.in
    cp $PROFILE/$ISOLINUXCFG $OUTDIR/isolinux/isolinux.cfg
    [ -f $PROFILE/$SYSLINUXCFG ] && cp $PROFILE/$SYSLINUXCFG $OUTDIR/isolinux/syslinux.cfg
    if [ -f $ROOT/usr/lib/syslinux/isolinux.bin ];then
	 cp $ROOT/usr/lib/syslinux/isolinux.bin $OUTDIR/isolinux/
    else
	 Info "Warning! Use isolinux.bin from host system. Please, add syslinux package to boot component into profile !"
	 cp /usr/lib/syslinux/isolinux.bin $OUTDIR/isolinux/
    fi

# kernel image
    cp $ROOT/boot/vmlinuz-* $OUTDIR/isolinux/alt0/vmlinuz

# copy bootlogo
    if [ -f "$ROOT/usr/share/gfxboot/$THEME/bootlogo" ];then 
	cp -f $ROOT/usr/share/gfxboot/$THEME/bootlogo $OUTDIR/isolinux/
    fi

#create bootsplash

    createsplash()
    {
	local name n X Y filename
	if [ -d "$ROOT/etc/bootsplash/themes/current/config" ];then
	    for n in $ROOT/etc/bootsplash/themes/current/config/bootsplash-*.cfg;
	      do
	      name=`basename $n`
	      X=`echo $name|sed -e 's/.*-\(.*\)x\(.*\)\.cfg/\1/'`
	      Y=`echo $name|sed -e 's/.*-\(.*\)x\(.*\)\.cfg/\2/'`
	      [ ${#X} -eq 3 ] && X="0$X"
	      [ ${#Y} -eq 3 ] && Y="0$Y"
	      filename="$X$Y.spl"
	      tmpspl=`mktemp -d /tmp/spl.XXXXXXXXXX`
	      /sbin/splash -r $ROOT -f -s $n >$tmpspl/bootsplash
	      echo "file /bootsplash $tmpspl/bootsplash 0777 0 0"|gencpio ->$OUTDIR/isolinux/$filename
	      rm -Rf $tmpspl
	    done
	fi
    }

    createsplash


# kernel modules
    rm -rf -- $TMPDIR/modules
    mkdir -p $TMPDIR/modules

# get kernel version
    kver=$(ls -d $ROOT/lib/modules/*)
    kver=${kver##*/}

# generate marfile & co
    /usr/bin/mkmar -r $ROOT -k $kver \
	-p $PROFILE/modules \
	-o $TMPDIR/modules/modules

# generate .VERSION
    echo "$ORIGIN $VERSION $SUITE ($CODENAME)" > $TMPDIR/.VERSION

    MEMTEST=`ls -1 $ROOT/boot/memtest* 2>/dev/null|head -1`
    [ -z "$MEMTEST" ] || cp -f $ROOT/boot/`basename $MEMTEST` $OUTDIR/isolinux/memtest

# and create initramfs image
    sed -e "s|@MODDIR@|$TMPDIR/modules|g" \
	-e "s|@DOTVERSION@|$TMPDIR/.VERSION|" \
	< $PROFILE/initfs |gencpio -|gzip -c \
	> $OUTDIR/isolinux/alt0/full.cz
done
