#!/bin/sh

alterator_api_version=1
. alterator-sh-functions

po_domain="alterator-lilo"

unset \
	LANG \
	LANGUAGE \
	LC_CTYPE \
	LC_NUMERIC \
	LC_TIME \
	LC_COLLATE \
	LC_MONETARY \
	LC_MESSAGES \
	LC_PAPER \
	LC_NAME \
	LC_ADDRESS \
	LC_TELEPHONE \
	LC_MEASUREMENT \
	LC_IDENTIFICATION \
	LC_ALL \
	||:

destdir=
[ -z "$INSTALLER" ] ||
	destdir="/mnt/destination"

PATH="/usr/share/alterator-lilo/helpers:$PATH"
lilo_tmpl="/usr/share/alterator-lilo/template/lilo.conf"
lilo_conf="/etc/lilo.conf"
tmpconf="/tmp/lilo.conf"
confonly=
workdir=

exit_handler() {
	local rc=$?
	trap - EXIT
	rm -rf -- "$workdir"
	exit $rc
}

trap exit_handler HUP PIPE INT QUIT TERM EXIT
workdir="$(mktemp -dt "${0##*/}.XXXXXXXXXX")" || exit 1

globals="$workdir/globals"
sections="$workdir/sections"
tmp_section="$workdir/section.tmp" # for section editing 
order="$workdir/label.order"
conf="$workdir/lilo.conf"
fdisk_out="$workdir/fdisk.out"

others_list="$workdir/others"

liloopts="-H"

. lilo-common.sh
. lilo-device.sh
. lilo-raid-boot.sh
. lilo-parser.sh
. lilo-other-system.sh
. lilo-bootable-flag.sh
. lilo-fix-cdrom.sh
. lilo-conf.sh

conf_exists="yes"
[ -f $lilo_conf ] || conf_exists="no";

fdisk_output       "$fdisk_out"
normalize_liloconf "$lilo_conf" "$lilo_tmpl" > "$conf"
update_rootdev     "$conf"
find_others > "$others_list"
[ "$conf_exists" = "yes" ] || add_others "$conf" < "$others_list"
parse_liloconf     "$conf" "$conf_exists"

first_disk=$(disk_list)
first_disk=${first_disk%% *}

DEBUG=
on_message() {

    write_debug "=== action: %s ===\n" $in_action
    write_debug "objects: %s\n" $in__objects
    write_debug "section: %s\n" $in_section

    case "$in_action" in
	list)
	  case "$in__objects" in
	    disks)
	      local dec_pt=$(LC_NUMERIC="$in_language" locale decimal_point)
              tblabel="`_ "Tb"`"
              gblabel="`_ "Gb"`"
              mblabel="`_ "Mb"`"
              kblabel="`_ "Kb"`"
	      disk_list |
	        sed "s/\./$dec_pt/;s/TB/$tblabel/;s/GB/$gblabel/;s/MB/$mblabel/;s/KB/$kblabel/" |
	      while read dev size; do
	        write_enum_item "$dev" "${dev##*/} ($size)"
	      done
	    ;;
	    others)
	      # test_other_section needs fresh "$tmpconf"!
              if ! generate_liloconf "$tmpconf"; then
                write_error "can't generate liloconf"
                return
              fi
	      # show all bootable partitions with boot loaders
	      # enabled parameter - is this partition appeares in config file
	      list_others "$tmpconf" < "$others_list"
	    ;;
	    sections)
	      local def= def_label= label= sect_type=
	      def_label="$(show_default)"

	      while read -r sect_type label; do
	        [ -n "$label" -a -f "$sections/$label" ] || continue
		[ "$label" != "$def_label" ] && def="no" || def="yes"

		label="$(quote_shell "$label")"
                _write_begin
                write_string_param "name"    "$label"
                write_string_param "type"    "$sect_type"
                write_bool_param   "default" "$def"
                [ "$label" = "failsafe" ] && write_string_param "label" "`_ "failsafe"`"
                _write_end
	      done < "$order"
#	      show_sections_list
	    ;;
	    install_list)
	      write_enum_item "menu" "`_ "graphics"`"
	      write_enum_item "text" "`_ "text"`"
	    ;;
	    raid_extra_boot_list)
	      write_enum_item "" ""
	      write_enum_item "none"     "`_ "none"`"
	      write_enum_item "auto"     "`_ "auto"`"
	      write_enum_item "mbr"      "`_ "mbr"`"
	      write_enum_item "mbr-only" "`_ "mbr-only"`"
	    ;;
	    std_timeouts)
	      write_enum_item "0"      "`_ "no wait"`"
	      write_enum_item "10"     "`_ "1 second"`"
	      write_enum_item "50"     "`_ "5 seconds"`"
	      write_enum_item "100"    "`_ "10 seconds"`"
	      write_enum_item "200"    "`_ "20 seconds"`"
	      write_enum_item "600"    "`_ "1 minute"`"
	      write_enum_item ""       "`_ "infinite"`"
	    ;;
	  esac
	  ;;
	read)
	  case "$in__objects" in
	    dev)
	      local current_dev=$(get_curr_dev)
	      [ -z "${currend_dev#/dev/}" ] && current_dev=$first_disk
	      write_string_param "boot"    "$(get_boot_dev)"
	      write_string_param "current" "$current_dev"
	      write_debug "$current_dev -- $first_disk"
	      ;;
	    globals)
	      [ -f "$globals" ] && show_params_list "$globals"
	      ;;
	    sections)
	      [ -f "$sections/$in_section" ] && show_params_list "$sections/$in_section"
	      ;;
	    source)
	      if ! generate_liloconf "$tmpconf"; then
	        write_error "can't generate liloconf"
	        return
	      fi
	      write_string_param "source"   "$(cat "$tmpconf")"
	      write_string_param "liloopts" "$liloopts"
	      ;;
	  esac
	  ;;
	write)
	  case "$in__objects" in
	    source)
	      if [ -n "$in_source" ]; then
	        printf '%s\n' "$in_source" > "$tmpconf"
	        [ ! -f "$globals" ]  || rm -f -- "$globals"
	        [ ! -f "$sections" ] || rm -f -- "$sections"
	        [ ! -f "$order" ]    || rm -f -- "$order"
	        normalize_liloconf "$tmpconf" "$lilo_tmpl" > "$conf"
	        parse_liloconf     "$conf"
	      fi
	      liloopts="$in_liloopts"
	    ;;
	    timeout) # for new simple interface
	      write_globals "timeout"
	    ;;
	    globals)
	      write_debug "Writing %s\n" "$in_target"
	      write_globals "$in_target"
	    ;;
	    section)
	      write_debug "Writing section %s\n" "$in_label"
	      write_section "$in_oldlabel" "$in_label"
	    ;;
	    section-remove)
	      write_debug "Removing section %s\n" "$in_label"
	      remove_section "$in_label"
	    ;;
	    reset)
	      reset_changes "$conf" "$in_part"
	    ;;
	    simple_sections)
	      # test_other_section needs fresh "$tmpconf"!
              if ! generate_liloconf "$tmpconf"; then
                write_error "can't generate liloconf"
                return
              fi

	      for s in ${in_sections//;/ }; do
	        echo "section>> $s"
	        # if section is on but is not in conf -- add it
	        if [ -z "${s%+*}" ]; then 
	          local dev="${s#+}"
	          local label="$(test_other_section "$dev" "$tmpconf")"
	          if [ -z "$label" ]; then
	            # find right label in $others_list
	            while read d l; do
	              [ "$d" = "$dev" ] && label="$l"
	              echo "Adding $d $dev - $l $label"
	            done < "$others_list"
	            echo "Adding $dev - $label"
	            if [ -n "$label" ]; then
	              in_type="other"
	              in_other="$dev"
	              in_label="$label"
	              in_alias=""
	              in_table=""
	              in_boot_as=""
	              write_section "" "$label"
	            fi
	          fi
	        fi
	        # if section is off but is in conf -- remove it
	        if [ -z "${s%-*}" ]; then
	          local dev="${s#-}"
	          local label="$(test_other_section "$dev" "$tmpconf")"
	          if [ -n "$label" ]; then
	            echo "Removing $dev - $label"
	            remove_section "$label"
	          fi
	        fi
	      done
	    ;;
	    commit)
	      if ! generate_liloconf "$tmpconf"; then
	        write_error "Unable to create temp config file"
	        return
	      fi
	      if test_bool "$in_confonly"; then
	        write_debug "Installing lilo.conf to system\n"
	        mv -f -- "$tmpconf" "$lilo_conf" ||
	        write_error "Unable to write new lilo.conf"
	      else
	        write_debug "Installing lilo.conf to system and running lilo\n"
	        write_liloconf "$tmpconf" "$lilo_conf" "$liloopts"
	      fi
	    ;;
	  esac
	  ;;
    esac
}

message_loop
