#!/bin/sh
# ***** BEGIN LICENSE BLOCK *****
# * Copyright (C) 2007  Alexey Gladkov <legion@altlinux.org>
# *
# * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
# ***** END LICENSE BLOCK *****

. /usr/share/alterator/build/backend3.sh

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=

empty() {
	printf '()\n'
}

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"
order="$workdir/label.order"
conf="$workdir/lilo.conf"
fdisk_out="$workdir/fdisk.out"

. 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

fdisk_output       "$fdisk_out"
normalize_liloconf "$lilo_conf" "$lilo_tmpl" > "$conf"
update_rootdev     "$conf"
find_others        "$conf"
parse_liloconf     "$conf"

on_message() {
        case "$in_action" in
		constraints)
			if [ "$in_target" != "globals" ]; then
				empty
				return
			fi
			cat <<-EOF
			(
			   method (exclude ("insecure" password1) exclude ("insecure" password2))
			   password1 (required #t equal password2)
			)
			EOF
			;;
		list)
			printf "("
			[ "$in_part" = "sections" ] &&
				show_sections_list "$in_type" ||
				getinfo
			printf ")\n"
			;;
		read)
			if [ "$in__objects" = "/" -a -z "$in_section" ]; then
				empty
				return
			fi

			[ "$in__objects" != "globals" ] &&
				section_path="$sections/$in_section" ||
				section_path="$globals"

			[ ! -f "$section_path" ] && empty && return 0

			local confonly_val='#f'
			[ -z "$confonly" ] ||
				confonly_val='#t'

			printf "("
			printf 'configonly %s\n' "$confonly_val"
			show_params_list "$section_path"
			printf ")\n"
			;;
		read-source)
			generate_liloconf "$tmpconf" &&
			printf '(source "%s")' "$(cat "$tmpconf" |simple_quote)" ||:
			;;
		write-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
			empty
			;;
		reset)
			reset_changes "$conf" "$in_part" &&
			empty
			;;
		new-section)
			create_section "$in_label" &&
			empty
			;;
		update-section)
			update_section "$in_oldlabel" "$in_label" &&
			empty
			;;
		remove-section)
			remove_section "$in_label" &&
			empty
			;;
		write)
			if [ -n "$in_confonly" ]; then
				case "$in_confonly" in
					'#t') confonly=1 ;;
					'#f') confonly=  ;;
				esac
			fi
			write_globals "$in_target" &&
			empty
			;;
		commit)
			local rc=
			generate_liloconf "$tmpconf" || rc=1
			if [ -n "$confonly" ]; then
				mv -f -- "$tmpconf" "$lilo_conf" ||
					{ error "Unable to write new lilo.conf"; rc=1; }
			else
				write_liloconf "$tmpconf" "$lilo_conf" || rc=1
			fi
			[ -n "$rc" ] ||
				empty
			;;
		*)
			printf '#f\n'
			;;
        esac
}

message_loop
