#!/bin/ash
#
# Functions for PLD installer UI.
#
# (C) 2001, 2002 Michal Moskal <malekith@pld-linux.org>
#
# This file can be modified and/or redistributed under
# the terms of GPL version 2 (without any options :^).
#

# helpers
ok_cancel="<button id=ok> <button id=cancel caption='$(nls "_Cancel")' res=2>"

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

yes_no () {
  local act
  act=$1
  shift
  case $act in
    yes | no )
      ;;
    * )
      die "yes_no(): 1st arg isn't yes or no"
      ;;
  esac
  dial <<EOF
<meta title="$(nls "Question")">
<meta active=$act>
<br>
$@
<br>
                   <button id=yes res=1 caption="$(nls "_Yes")">
 <button id=no res=2 caption="$(nls "_No")">
EOF

  case "$res" in
    1 )
      return 0
      ;;
    2 )
      return 1
      ;;
    * )
      case "$act" in
      yes )
        return 0
	;;
      no ) 
        return 1
	;;
      esac
      ;;
  esac
}

info () {
  ( dial <<EOF
<meta title="$(nls "Information")">
<meta active=$act>
<br>
$@
<br>
                   <button>
EOF
)
}

dial() {
  local title
  title="                       _P _L _D   _L i n u x   1 . 0   /  $add_title"
  
  eval $(dml -t "$title"  "$@" || true)
}

# usage: get_input var_name default text...
get_input () {
  local var def
  var=$1
  shift
  def=$1
  shift
  dial <<EOF
<meta title="$(nls "Text input")">
<meta active=$var>
<br>
$@
<br>
<input caption="$def" id=$var><br>
               $ok_cancel
EOF

  case "$res" in
    1 )
      return 0
      ;;
    * )
      eval "$var=\"\$def\""
      return 1
      ;;
  esac
}

disk_name () {
  case "$1" in
    /dev/hda ) nls "IDE Primary Master" ;;
    /dev/hdb ) nls "IDE Primary Slave" ;;
    /dev/hdc ) nls "IDE Secondary Master" ;;
    /dev/hdd ) nls "IDE Secondary Slave" ;;
    /dev/sd[a-i] ) nls "SCSI Disk #%d" $(echo $f | sed -e 's|/dev/sd||' | tr 'a-i' '1-9') ;;
    /dev/ida/* ) nls "%s hardware RAID device" "cpqarray" ;;
    /dev/rd/* ) nls "%s hardware RAID device" "DAC960" ;;  
    /dev/cciss/* ) nls "%s hardware RAID device" "cciss" ;;
    /dev/ataraid/* ) nls "%s hardware RAID device" "ATA" ;;
    /dev/i2o/* ) nls "%s I2O disk device" "i2o" ;;
  esac
}


# "print" config file
# TODO: improve comments for current state...
output_config () {
boot_loader_other="$(echo $boot_loader_other)"
cat <<EOF
## 
## Generated automatically by ui at `date`
##

# source is one of:
#  - net - ftp, http. source_filesystem has to be net
#  - nfs - nfs server. source_filesystem has to be nfs
#  - cdrom
#  - disk
# source_filesystem: ex: iso9660, ext2
# source_device: either /dev/XXX for mount, or machine name for nfs, or
#   server name with optional protocol for net, ftp:// is the default.
# source_dir: exact directory where installation files 
#   (RPMS and installer directory) reside
source="$source"
source_device="$source_device"
source_filesystem="$source_filesystem"
source_dir="$source_dir"

# net_device: example: eth0
# net_device_module: examples: ne2k-pci, auto
# net_device_options: ex: io=0x300
# net_ipaddr: ex: 192.168.1.1
# net_prefix: ex: 24
# net_gateway: ex: 192.168.1.254, or "" when none
# net_dns: space separated list of (up to 3) IP's of DNS'es
# net_v6: yes, no or auto : whatever to use IPv6 for interfaces 
#         configuration. If auto it is determined based upon 
#         net_ipaddr.
# net_proxy: http/ftp proxy to be used during installation. It is an
#            an URL. Examples: "http://squid.foobar.com:3128/",
#            "http://user:pass@squid.foobar.com:3128/"
#            NOTE: it is used only if it's set to something else then 
#            "" or "none". snarf is used for all file transfers then
#            (even rpms for poldek).
net_device="$net_device"
net_device_module="$net_device_module"
net_device_module_options="$net_device_module_options"
net_ipaddr="$net_ipaddr"
net_prefix="$net_prefix"
net_gateway="$net_gateway"
net_dns="$net_dns"
net_v6="$net_v6"
net_proxy="$net_proxy"

# space delimited SCSI hostadapters (can be auto, for PCI SCSI card)
# hardware raid modules don't need to be listed here
scsi_hostadapters="$scsi_hostadapters"

# PCMCIA controller (can be auto)
pcmcia_controller="$pcmcia_controller"


# dest_devices: space delimited list of target devices 
#   (like '/dev/hda /dev/sdb')
# dest_devices_actions: space delimited list, one of items below per device:
#  - make_new
#  - use_existing
#
# dest_partID_VAL: ID is positive integer, VAL is one of
# device: ex: /dev/hda7 (for use_existing), /dev/hda (for make_new)
# size: ex: 177 (177 megabytes), 50% of all, 100% of free
# filesystem: swap, ext2, reiserfs, jfs, ext3, xfs, md
# format_partition: yes or no
# mnt_point: ex: '/var/spool', '/', '' allowed for swaps
# options: options for fstab (ex: 'noexec')
# format_options: options for mkfs
#
# it's not allowed to have gaps in partition numbering, i.e.
# to have dest_part1_device, dest_part2_device and dest_part4_device
# set (while no dest_part3_device).
# Numeration starts at 1.
dest_devices="$dest_devices"
dest_devices_actions="$dest_devices_actions"

EOF


id=0
while :
do
  id=$(($id+1))
  eval_partition_info $id
  test "$dest_part_device" || break

  # output partitions
  cat <<EOF
#
# partition $id
#
dest_part${id}_device="$dest_part_device"
dest_part${id}_size="$dest_part_size"
dest_part${id}_filesystem="$dest_part_filesystem"
dest_part${id}_format_partition="$dest_part_format_partition"
dest_part${id}_mnt_point="$dest_part_mnt_point"
dest_part${id}_options="$dest_part_options"
dest_part${id}_format_options="$dest_part_format_options"


EOF

done

cat <<EOF
# Section: packages installation
# pkgs_installer: one of poldek, wuch, apt. Only poldek 
#                 supported at the very moment
# pkgs_install_docs: yes or no. Whatever to install documentation for
#                    packages.
# pkgs_install_langs: %_install_langs rpm macro (all, pl:pl_PL:en:en_US)
# pkgs_cpus: number or "auto". Number of CPUs in installed system. If more
#            then one -- install kernel-smp instead of normal. In future
#            we might do some configuration tweaks for SMP.
pkgs_installer="$pkgs_installer"
pkgs_install_docs="$pkgs_install_docs"
pkgs_install_langs="$pkgs_install_langs"
pkgs_cpus="$pkgs_cpus"

# Section: boot loader
# boot loader to install, possible values:
#  - lilo - generate /etc/lilo.conf, and run lilo
#  - rc-boot - generate configs for rc-boot, you need to select one of
#    lilo or grub packages to actually have some bootloader (otherwise
#    poldek will do it for you, randomly :^)
#  - none (or empty) - don't install bootloader
boot_loader="$boot_loader"

# Device to install bootloader on. If it is set to "auto", then device part
# (i.e. without partition number) of /boot (or / if no /boot) partition is 
# taken.
boot_loader_device="$boot_loader_device"

# other systems on this box, space delimited list of entries
#  - bsd:LABEL:ROOT_DEVICE - bsd, bootloader installed on ROOT_DEVICE
#  - dos:LABEL:ROOT_DEVICE - same, for dos
#  - linux:LABEL:KERNEL:[INITRD:]ROOT_DEVICE - KERNEL is sth like 
#    /boot/vmlinuz-2.4.13-debian
# example: "bsd:obsd:/dev/hdb3 dos:winda:/dev/hda2 
#           linux:debian:/boot/vmlinuz-debian:/dev/hda3 
#           linux:old-pld:/boot/initrd-pld-old:/boot/vmlinuz-pld-old:/dev/hdc3"
boot_loader_other="$boot_loader_other"

# The end.
EOF

}

save_config () {
  output_config > $conffile
}

output_sysconfig () {
cat <<EOF
# language to be used on installed system (ex: pl_PL, en_US, '')
lang="$lang"

# timezone to be set (ex: Europe/Warsaw, America/Vancuver)
timezone="$timezone"

# system clock is in UTC? ("yes" or "no")
clock_utc="$clock_utc"

# network configuration
net_config="$net_config"
net_hostname="$net_hostname"
net_domainname="$net_domainname"
EOF

for f in net_gateway net_dns net_device net_device_module net_ipaddr net_prefix ; do
  eval "val=\$${f}"
  if [ "$val" != "" ] ; then
    echo "$f=\"$val\""
  fi
done

cat <<EOF
# superuser password
root_pass="$root_pass"

# user to be added.
user_realname="$user_realname"
user_name="$user_name"
user_pass="$user_pass"

EOF
}

save_sys_config () {
  output_sysconfig > $sysconffile
}

ide_disk_items () {
  cat <<EOF
<item id="/dev/hda">/dev/hd_a: $(nls "Primary IDE master disk (most common)")
<item id="/dev/hdb">/dev/hd_b: $(nls "Primary IDE slave disk")
<item id="/dev/hdc">/dev/hd_c: $(nls "Secondary IDE master disk")
<item id="/dev/hdd">/dev/hd_d: $(nls "Secondary IDE slave disk")
EOF
}

load_parted ()
{
  if [ $real_system != no ] ; then
    return
  fi
  
  if test -x /bin/parted ; then
    return
  fi

  echo ok > /tmp/load_parted_result
  
  # hmm, should load parted
  if installer-validate-source; then
      if installer-prep-source; then 
          if load_package parted ; then
            : ok
          else
            wait_key
            info $(nls "Strange... Cannot fetch parted. Check source please.")
            echo oops > /tmp/load_parted_result
            return 1
          fi
      else
          wait_key
          info "$(nls "Running installer-prep-source failed.")<br>
		$(nls "Check whatever you have selected correct source.")"
          echo oops > /tmp/load_parted_result
          return 1
      fi
  else
      wait_key
      info "$(nls "Need to load some tools from source of distribution.")<br>
		$(nls "You must properly configure source.")"
      echo oops > /tmp/load_parted_result
      return 1
  fi
  return 0
}


# vim:ft=sh
