#!/bin/bash
#
# KNOPPIX on User-mode Linux boot script
#
# GPL
#
# SUZAKI Kuniyasu <k.suzaki@aist.go.jp>
# TAN Hideyuki <tanh@alpha.co.jp>
#
UMLKNXROOT=/opt/umlknoppix
export PATH=${UMLKNXROOT}/bin:$PATH
program=`echo $0 | sed 's#.*/##g'`
linux=linux
#linux=echo
usage="\
Usage: ${program} [OPTIONS] [options to uml binary]

Boot up KNOPPIX on User Mode Linux.

Options:
 -c|--cloopfile CLOOPFILE
      Knoppix cloop absolute filename to use
      (e.g., -c /cdrom/KNOPPIX/KNOPPIX)
 -d|--desktop (icewm|fluxbox|kde|wmaker|twm|xfce)
      Desktop manager to use (default: kde)
 -g|--geometry WIDTHxHEIGHT
      uml root window size (default: 800x600)
 -l|--lang (ja|us|da|de|es|fr|ns|it|pl|ru|sk ...)
      Choose language to use (default: $LANGUAGE)
 -m|--memsize MEMSIZE
      Memory size to use (default: 128M)
      see section 'mem=' below
 -h|--help
      This message
      And the following is the message of uml binary.
"

while [ $# -gt 0 ]; do
  case "$1" in
    -c|--cloopfile) shift;
      cloop="$1";
      if [ ! -e "${cloop}" ]; then
        echo "--cloopfile ${cloop} : No such file or directory."
        exit 1
      fi
      shift;;
    -d|--desktop) shift;
      desktop="$1"; shift;;
    -g|-geom|-geometry|--geometry) shift;
      geometry="$(echo $1 | egrep '(^([0-9]+)x([0-9]+)$)')"; RC="$?"
      if [ "$RC" != "0" ]; then
        echo "--geometry WIDTHxHEIGHT (where WIDTH and HEIGHT are numbers)"
        exit 1
      fi
      shift;;
    -l|--lang) shift;
      lang="$1"; shift;;
    -m|--memsize) shift;
      memsize="$1"; shift;;
    -h|-help|--help) shift;
      echo "${usage}";
      ${linux} --help
      exit 0;;
    *) cmdline="${cmdline} $1"; shift;;
  esac
done

if [ -z "${desktop}" ]; then
  desktop=kde
fi

if [ -z "$LANGUAGE" ];then
  LANGUAGE=ja
fi

if [ -z "${lang}" ]; then
  lang="$LANGUAGE"
fi

if [ -z "${memsize}" ]; then
#  memsize="$(awk 'BEGIN{m=0};/MemFree|Cached/{m+=$2};END{print m/5*4"k"}' /proc/meminfo)"
  memsize="128M"
fi

if [ -z "${geometry}" ]; then
#  widhtheigth="$(xwininfo -root | egrep "(Width:|Height:)" | sed -e "s/Width: /WIDTH=/" -e "s/Height: /HEIGHT=/" )"
#  eval "${widhtheigth}"
#  geometry="${WIDTH}x${HEIGHT}"
  geometry="800x600"
fi

if [ -z "${cloop}" ]; then
  cloop="/cdrom/KNOPPIX/KNOPPIX"
#  cloop="/sfs/foobar@hogehoge/KNOPPIX-DVD"
#  cloop="/KNOPPIX"
fi

# initrd
initrd="${UMLKNXROOT}/lib/miniroot-uml.gz"
if [ -f ./miniroot-uml.gz ]; then
  initrd="./miniroot-uml.gz"
fi
if [ -f ./miniroot-uml ]; then
  initrd="./miniroot-uml"
fi

macaddr="54:61:4E"
for i in 1 2 3 ; do
macaddr="${macaddr}:$(printf %x `expr $RANDOM / 128 `)"
done
echo $macaddr

if [ -e "${cloop}" ]; then
  cloop="/mnt/hostfs$cloop"
  $linux mem="${memsize}" initrd="${initrd}" \
    eth0=daemon,${macaddr},unix,/tmp/.uml/umlhub \
    lang="${lang}" desktop="${desktop}" \
    CLOOP="${cloop}" XNESTGEOM="${geometry}" "${cmdline}"

else
  $linux mem="${memsize}" initrd="${initrd}" \
    eth0=daemon,${macaddr},unix,/tmp/.uml/umlhub \
    lang="${lang}"  desktop="${desktop}" \
    ubd0=/dev/cdrom \
    XNESTGEOM="${geometry}" "${cmdline}" \
    
fi

echo uml exit: $?

exit 0;
