#!/bin/sh

po_domain="alterator-setup"
alterator_api_version=1
postinstall_hooks_dir=/usr/lib/alterator/hooks/setup-postinstall.d

. alterator-sh-functions

dirs="/usr/share/alterator-setup/notes"

find_htmlfile()
{
	local name="$1"
	local languages="$2"
	local path= html_file=

	for path in $dirs; do
		for lang in $languages; do
			case "$lang" in
			POSIX|C|all|"")	# might be unset yet
				lang="all";;
			*)
				lang="$(printf %s "${lang%%_*}" |
					tr '[A-Z]' '[a-z]')";;
			esac
			html_file="$path/$name.$lang.html"
			if [ -s "$html_file" ]; then
				echo "$html_file"
				return
			fi
		done
	done
}

on_message() {
	case "$in_action" in
	write)
		case "$in__objects" in
		/) ;;
		run-postinstall)
			run-parts "$postinstall_hooks_dir"
			;;
		esac
		;;
	read)
		local languages= html_file=
		languages="$(printf '%s' "$in_language" |tr ';' ' ') all"
		html_file="$(find_htmlfile "setup-finish" "$languages")"
		if [ -s "$html_file" ]; then
			write_string_param "url" "$html_file"
		else
			write_error "`_ "File not found"`"
		fi
		;;
	esac
}

message_loop
