#!/bin/sh

alterator_api_version=1
po_domain=alterator-alternatives

. alterator-sh-functions
. /usr/share/alternatives/functions

alternatives_list()
{
    alternatives-list|
	sed -ne 's,^\(.*\) points to.*,\1,p'|
	sort
}

on_message()
{
	case "$in_action" in
		list)
			case "$in__objects" in
			    /)
				alternatives_list|write_enum
				;;
			    avail_candidate)
				[ -n "$in_name" ] || in_name="$(alternatives_list|head -n1)"
				[ -n "$in_name" ] || return

				grep -h "^$in_name[[:space:]]" "$auto_dir"/* |
				    sed 's,[[:space:]]\+,\t,g' |
				    cut -f2|
				    write_enum
				;;
			esac
			;;
		read)
			[ -n "$in_name" ] || in_name="$(alternatives_list|head -n1)"
			[ -n "$in_name" ] || return
			write_string_param name "$in_name"
			local alternative="/$in__objects"
			
			
			! grep -qs "^$in_name[[:space:]].*manual$" "$manual_file"
			write_bool_param manual "$?"

			write_string_param current "$(alternatives-list "$in_name" | sed -ne 's,^.* points to \(.*\)$,\1,p')"
			;;
		write)
			local alternative="/$in__objects"
			if test_bool "$in_manual"; then
				alternatives-manual "$in_name" "$in_current"
			else
				alternatives-auto "$in_name"
			fi

			alternatives-update
			;;
		*)
			echo '#f'
	esac
}

message_loop
