#!/bin/sh -f

alterator_api_version=1

. alterator-sh-functions

# <path1>:<path2>:... <head> <tail> -> <head><path1><tail> <head><path2><tail> ...
print_path_list(){
  local IFS=":"
  for d in $1; do
    echo -n " $d$2"
  done
}

helpdirs="$(print_path_list "$ALTERATOR_DATADIR" "/help")"

search_help()
{
    local languages="$1";shift
    local name="$1";shift
    local help_file=

    for lang in $languages; do
	for dir in $helpdirs; do
	    help_file="$dir/$lang/$name.html"
	    if [ -s "$help_file" ]; then
		write_string_param url "$help_file"
		return 0
	    fi
	done
    done
    return 1
}

on_message()
{
	case "$in_action" in
		read)
			local languages="$(printf '%s' "$in_language" |tr ';' ' ') en_US"
			local topic=${in_topic}
			[ -n "$topic" ] || topic="${in__objects}"

			search_help "$languages" "$topic" ||
			    search_help "$languages" "notfound" ||
			    write_string_param url ""
	esac
}

message_loop
