#!/bin/ash
#
# This file is part of PLD batch-installer
# Partial Copyright (C) 2001 CYBER Service
# Partial Copyright (C) 2001 Michal Moskal
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# Author: Rafa Kleger-Rudomin, Michal Moskal
# Contributors:


# this file presents menu that runs proper UIs

#set -e

test ! -f /etc/profile || . /etc/profile

PATH=$PATH:./ui

PATH=$PATH:. . installer-functions

for f in $(echo $PATH | sed -e 's/:/ /g') ; do
  test -x $f/dml && break
done

test -x $f/dml || {
# I guess there is no need to translate this.
  cat <<EOF
DML not found. As you probably did this disk yourself, I hope you know what 
you are doing...

EOF

  /bin/shell
  exit 0
}

wait_key() {
  nls "Press the [Enter] key.\n"
  read JUNK
}

run_fdisk() {
  prog=$1
  if test -x /bin/$prog -o -x /sbin/$prog ; then
    echo
    echo
    nls "Ok, spawning %s...\n" "$prog"
    nls "If you don't want it to work on /dev/hda, run it manually from different\n"
    nls "console.\n"
    echo
    $prog /dev/hda
  else
    dial <<EOF
<meta title="$(nls "Source OK?")">
$(nls "In order to run %s you need to setup source" "$prog")<br>
$(nls "of installation properly. If you aren't sure you")<br>
$(nls "did that, please go back and fix it (for example")<br>
$(nls "using Regular Setup UI menu item).")<br><br>
$(nls "Are you sure you did setup source of installation correctly?")<br><br>
<button res=1 caption='$(nls _Yes)'> <button res=2 caption='$(nls _No)'>
EOF

    if test "$res" != 1 ; then
      return
    fi
    
    # hmm, should load fdisk
    if installer-validate-source; then
	if installer-prep-source; then 
	    if load_package $prog ; then
	      echo
	      echo
	      nls "Ok, finally spawning %s...\n" "$prog"
	      nls "If you don't want it to work on /dev/hda, run it manually from different\n"
	      nls "console.\n"
	      echo
	      $prog /dev/hda
	    else
	      wait_key
  	      dial <<EOF
<meta title='$(nls "Oops...")'>
$(nls "Strange... cannot load %s." "$prog")<br>
$(nls "Check whatever you have selected correct source.")<br><br>
<button>
EOF
	    fi
	else
	    wait_key
	    dial <<EOF
<meta title='$(nls "Oops...")'>
$(nls "Running installer-prep-source failed.")<br>
$(nls "Check whatever you have selected correct source.")<br><br>
<button>
EOF
	fi
    else
	wait_key
        dial <<EOF
<meta title='$(nls "Oops...")'>
$(nls "Need to load some tools from source of distribution.")<br>
$(nls "You must properly configure source.")<br><br><button>
EOF
    fi
  fi
}

help () {
  dial <<EOF
<meta title='$(nls "Welcome to PLD Linux Installer.")'>
<text width=70 height=18>
$(nls "You can scroll this text using up and down arrow. Use the <Tab> key to move")
$(nls "between widgets.")
<br><br>
$(nls "Typical installation procedure involves running installation wizard.")
$(nls "If you are more familiar with Linux, you can also use '%s'" "$(nls "Regular setup UI")")
$(nls "or even vi to have more freedom about configuration. If you are going to do")
$(nls "so, you can still use interactive disk partitioning tool, just set source")
$(nls "of installation, dest_devices and select appropriate item from main menu.")
<br><br>
$(nls "For more information about PLD Linux, please visit our webpages at")
$(nls "<i>http://www.pld.org.pl</i>. If you have any comments about PLD, including")
$(nls "this installer, please email them to <i>&lt;feedback@pld.org.pl&gt;</i>")
<br><br>
$(nls "<i>Advanced</i>")
<br><br>
$(nls "Shell is available on 2nd through 5th console. Installation log is kept in")
$(nls "<i>/var/log/installer</i>. You might view it with <i>vi</i> if some messages")
$(nls "scrolled up the screen. You can also have a look at 6th console (logs are there).")
<br><br>
$(nls "You might also want to see installation howto, in order to do so select")
$(nls "appropriate item from next menu. Howto could be helpful especially in case")
$(nls "of non-standard installations. (especially if you are going to use RAID.")
<br><br>
$(nls 'And now, <b>"Follow the white rabbit"</b> ... :^)')
</text><br>
                                <button>
EOF
}


help

res_menu=ui-wizard

while : ; do

  case $arch in
  ppc )
    pdisk="<item id='run_fdisk pdisk'>$(nls "Load and run _pdisk")"
    ;;
  * )
    pdisk=
    ;;
  esac
  
dial <<EOF 
<meta title='$(nls "PLD Linux Installation")'>
<br>
         $(nls "Version: %s" "1.0.10")<br>
             $(nls "Main menu")<br>
    $(nls "(this menu is scrollable)")
<br>
<br>
<menu height=15 id=res_menu res=1 selected="$res_menu">
<item id="ui-wizard">$(nls "_Installation Wizard")
<item ghosted=1>--
<item id="help">$(nls "_Display intro help")
<item id="howto">$(nls "_Display HOWTO")
<item id="ui-bugreport">$(nls "_Report problem with installer")
<item ghosted=1>--
<item id="ui-main">$(nls "_Regular setup UI")
<item id="ui-parted2">$(nls "_Prepare partitions manually")
<item id="run_fdisk fdisk">$(nls "Load and run _fdisk")
$pdisk<item id="ui-pkgs">$(nls "_Select packages")
<item id="ui-conf">$(nls "_Initial config of installed system")
<item id="ui-boot">$(nls "_Bootmanager configuration")
<item id="installer">$(nls "_Run installation process")
<item id="reboot">$(nls "_Reboot system")
<item ghosted=1>--
<item id=save-config>$(nls "_Save config to diskette")
<item id=load-config>$(nls "_Load config from diskette")
<item id=bye>$(nls "E_scape to shell")
</menu>
EOF
# <item id="ui-parted">$(nls "_Old: _Prepare partitions manually")

echo "Running $res_menu..."

if [ "$res_menu" = bye ] ; then
  nls "You're dropped to shell right now. Type exit and press enter to\n"
  nls "get back to setup menu.\n"
  echo
  /bin/shell
elif $res_menu ; then
  if [ "$res_menu" = installer ] ; then
    nls "Installation seems successful.\n"
    wait_key
  fi
else
  nls "Something went bad.\n"
  wait_key
fi

done

# vim:ft=sh
