#!/bin/sh

alterator_api_version=1

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

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

list_candidate()
{
    [ -n "$in_name" ] || return

    grep -h "^$in_name[[:space:]]" "$package_dir"/* |
        sed 's,[[:space:]]\+,\t,g' |
	cut -f2|
	write_enum
}

read_alternative()
{
    [ -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_alternative()
{
    if test_bool "$in_manual"; then
	alternatives-manual "$in_name" "$in_current"
    else
	alternatives-auto "$in_name"
    fi >/dev/null 2>/dev/null

    alternatives-update
}

alterator_export_proc read_alternative
alterator_export_proc write_alternative
alterator_export_proc list_candidate
alterator_export_proc list_alternative

message_loop
