#!/bin/sh -f

alterator_api_version=1
po_domain="alterator-wizardface"

. alterator-sh-functions

XBINDKEYS_PID=
start_watchkeys() {
    cat <<EOF >"$TMPDIR"/a11y.xbindkeysrc
"installer-a11yctl -q reader toggle"
  control + r
EOF
    xbindkeys -n -f "$TMPDIR"/a11y.xbindkeysrc &
    XBINDKEYS_PID=$!
}

installer_a11yctl() {
    installer-a11yctl -q "$@"
}

installer_a11yctl_err() {
    local err=
    local ret=0
    err="$(installer-a11yctl -q "$@" 2>&1)" || ret=$?
    if [ $ret -ne 0 ]; then
	write_error "$err"
    fi
    return $ret
}

cleanup() {
    installer_a11yctl_err reader stop ||:
    installer_a11yctl_err keyboard stop ||:
    installer_a11yctl_err magnifier stop ||:
    if [ -n "$XBINDKEYS_PID" ]; then
	kill "$XBINDKEYS_PID"
    fi
}
trap 'cleanup' EXIT

init() {
    # Run the screen reader if specified in the kernel command line:
    if grep -qsw 'a11y=\(.*,\)\?reader' /proc/cmdline
    then
	installer_a11yctl_err reader start
    fi
    start_watchkeys
}

on_message()
{
    set_locale

    case "$in_action" in
        type)
            case "$in__objects" in
                /)
                    write_type_item screen_reader boolean
                    write_type_item screen_reader_available boolean
                    write_type_item onscreen_keyboard boolean
                    write_type_item onscreen_keyboard_available boolean
                    write_type_item magnifier boolean
                    write_type_item magnifier_available boolean
                    ;;
            esac
            ;;
        read)
            case "$in__objects" in
                /)
		    if installer_a11yctl reader status; then
			write_bool_param screen_reader 'on'
		    else
			write_bool_param screen_reader 'off'
		    fi
		    if installer_a11yctl reader available; then
			write_bool_param screen_reader_available 'on'
		    else
			write_bool_param screen_reader_available 'off'
		    fi

		    if installer_a11yctl keyboard status; then
			write_bool_param onscreen_keyboard 'on'
		    else
			write_bool_param onscreen_keyboard 'off'
		    fi
		    if installer_a11yctl keyboard available; then
			write_bool_param onscreen_keyboard_available 'on'
		    else
			write_bool_param onscreen_keyboard_available 'off'
		    fi

		    if installer_a11yctl magnifier status; then
			write_bool_param magnifier 'on'
		    else
			write_bool_param magnifier 'off'
		    fi
		    if installer_a11yctl magnifier available; then
			write_bool_param magnifier_available 'on'
		    else
			write_bool_param magnifier_available 'off'
		    fi
                    ;;
            esac
            ;;
        write)
            case "$in__objects" in
                /)
                    if test_bool "$in_screen_reader"; then
                        installer_a11yctl_err reader start
                    else
                        installer_a11yctl_err reader stop
                    fi
                    if test_bool "$in_onscreen_keyboard"; then
                        installer_a11yctl_err keyboard start
                    else
                        installer_a11yctl_err keyboard stop
                    fi
                    if test_bool "$in_magnifier"; then
                        installer_a11yctl_err magnifier start
                    else
                        installer_a11yctl_err magnifier stop
                    fi
                    ;;
            esac
            ;;
    esac
}

alterator_export_proc init

message_loop
