#!/bin/ash
#
# Interactive bootmanager configuration tool for PLD Linux installer.
#
# (C) 2002 Michal Moskal <malekith@pld.org.pl>
#
# This file can be modified and/or redistributed under
# the terms of GPL version 2 (without any options :^).
#
# WARNING: Voodoo magic inside :)
#

PATH=.:..:$PATH 
add_title="_Bootmanager config"
. installer-functions
. ui-functions

find_config_paths "$@"
load_or_create_config

clr ()
{
  img_label=
  img_root=
  img_kernel=
  img_initrd=
  img_os=
}

load_img () 
{
  clr
  img_label=$1

  if test -f /tmp/img.$img_label ; then
    . /tmp/img.$img_label
  fi
}

save_img () 
{
  cat > /tmp/img.$img_label <<EOF
img_label="$img_label"
img_root="$img_root"
img_kernel="$img_kernel"
img_initrd="$img_initrd"
img_os="$img_os"
EOF
}

get_os ()
{
  dial <<EOF
$(nls "Please select OS")
<menu res=1 id=os selected="$1" >
<item id=linux>$(nls "_Linux: other linux installation")
<item id=bsd>$(nls "_BSD: {Free,Open,Net,*}BSD")
<item id=dos>$(nls "_DOS: MS-DOS/Windows and such")
</menu>
$ok_cancel
EOF

  case $res in
  1 )
    echo "$os"
    ;;
  esac
}

print_devices () 
{
  for f in $dest_devices ; do
    parted -s $f print | grep '^[1-9]' | while read line ; do
      no=$(echo $line | (read x y; echo $x))
      echo "<item id=$f$no>$line"
    done
  done
}

get_root () 
{
  def=$1
  dial <<EOF
$(nls "Please select device.")
<menu id=device selected="$1" res=1>
<item id=custom>$(nls "custom")
`print_devices`
</menu>
$ok_cancel
EOF

  case $res in
  1 )
    case $device in
    custom )
      get_input device "$def" "$(nls "Please enter device name:")"
      ;;
    esac
    echo "$device"
    ;;
  * )
    ;;
  esac
}

edit_img ()
{
  if [ "$1" != "" ] ; then
    load_img "$1"
  fi
  
  org_label="$img_label"
  
  dial <<EOF
$(nls "Label          :") <input id=img_label caption="$img_label"><br>
$(nls "OS             :") <input id=img_os caption="$img_os"><br>
$(nls "Root directory :") <input id=img_root caption="$img_root"><br>
$(nls "Kernel         :") <input id=img_kernel caption="$img_kernel"><br>
$(nls "Initrd         :") <input id=img_initrd caption="$img_initrd"><br>
$ok_cancel <button caption="$(nls "_Remove")" res=3>
EOF

  case $res in
  1 )
    if [ "$img_label" = "" ] ; then
      info $(nls "Label cannot be empty.")
      edit_img
    else
      save_img
    fi
    ;;
  3 )
    rm -f /tmp/img.$org_label
    ;;
  esac
}

make_new ()
{
  clr
  img_os=`get_os`
  if [ "$img_os" = "" ] ; then
    return
  fi
  img_root=`get_root`
  if [ "$img_root" = "" ] ; then
    return
  fi
  edit_img ''
}

change_os ()
{
  load_img $1
  img_os=`get_os $img_os`
  if [ "$img_os" != "" ] ; then
    save_img 
  fi
}

change_root ()
{
  load_img $1
  img_root=`get_root $img_root`
  if [ "$img_root" != "" ] ; then
    save_img 
  fi
}

load_it ()
{
  for img in $boot_loader_other ; do
    eval $(parse_boot_loader_entry $img)
    save_img $img_label
  done
}

save_it ()
{
  load_config

  boot_loader_other=
  for f in /tmp/img.* ; do
    if test -f $f ; then
      . $f
      case "$img_os" in
      linux )
        case "$img_initrd" in
	"" )
	  i="linux:$img_label:$img_kernel:$img_root"
	  ;;
	* )
	  i="linux:$img_label:$img_kernel:$img_initrd:$img_root"
	  ;;
	esac
        ;;
      * )
	i="$img_os:$img_label:$img_root"
	;;
      esac
      boot_loader_other="$boot_loader_other $i"
    fi
  done

  save_config
}

img_items () 
{
  load_img $1
  
  echo "<item id='edit_img $img_label'>image = $img_label"
  echo "<item id='change_os $img_label'>      os     = $img_os"
  echo "<item id='change_root $img_label'>      root   = $img_root"
  case $img_os in
    linux )
      echo "<item id='edit_img $img_label'>      kernel = $img_kernel initrd = $img_initrd"
      ;;
  esac
}

rm -f /tmp/img.*

load_parted || exit 1

# parse $boot_loader_other
load_it

while : ; do
  dial <<EOF
<meta title="$(nls "Bootmanager configurator")">
<br>
$(nls "Press enter on item to change or remove it.")<br><br>
<menu res=1 height=13 width=70 id=action selected=$action>
$(
  for f in /tmp/img.* ; do
    if test -f $f ; then
      img_items $( . $f ; echo $img_label )
    fi
  done
)
</menu>
<br>
<button res=1 caption="$(nls "_Change")">
 <button res=2 caption="$(nls "_Make new entry")">
 <button res=4 caption="$(nls "_Leave bootloader configurator")">
EOF
  case $res in
  1)
    $action
    ;;
  2)
    make_new
    ;;
  4)
    save_it
    exit 0
    ;;
  esac
done

# vim:ft=sh
