#!/bin/bash

conffile="$1"

if [ -z "$conffile" ] ; then
	echo "Usage: $0 <config.json>" >&2
	exit 1
fi

set -e

# key, json
json_get() {
	jq -r "$1" <<< "$2"
}

# key, json
json_get_json() {
	jq -c "$1" <<< "$2"
}

has_error=""
error() {
	printf "[error] %s\n" "$1" >&2
	has_error="y"
}

matches() {
	TEXT="$1"
	shift 1
	printf "%s\n" "$TEXT" | grep -q "$@"
}

json_config=""
if [ -r "$conffile" ] ; then
	if ! json_config="$(jq -c < "$conffile")" ; then
		error "Configuration contains invalid json."
		exit 1
	fi
else
	error "Configuration file does not exist or is not readable."
	echo "[info] PWD is $PWD"
	exit 1
fi

arguments=()

### Extract global options

home="$(json_get '.home // ""' "$json_config")"
fallback="$(json_get '.fallback // ""' "$json_config")"
icon_size="$(json_get '."icon-size" // 24' "$json_config")"
stylesheet="$(json_get '.stylesheet // ""' "$json_config")"
bottom_bar="$(json_get '."bottom-bar" // false' "$json_config")"

if ! matches "$icon_size" "^[0-9]*$" ; then
	error ".icon-size must be a positive integer!"
	exit 2
fi

if ! [ -r "$stylesheet" ] ; then
	error ".stylesheet must be name of a readable file."
	exit 2
fi

[ -z "$home" ] || arguments+=("--home" "$home")
[ -z "$fallback" ] || arguments+=("--fallback" "$fallback")
[ -z "$icon_size" ] || arguments+=("--icon-size" "$icon_size")
[ -z "$stylesheet" ] || arguments+=("--css" "$stylesheet")
[ "_$bottom_bar" != "_true" ] || arguments+=("--bottom-bar")

IFS=$'\n'

for host in $(json_get '."allow-hosts" // [] |.[]' "$json_config"); do
	arguments+=("--allow-host" "$host")
done

cookie_count=0
for cookie in $(json_get_json '.cookies // [] |.[]' "$json_config") ; do
	site="$(json_get ".site" "$cookie")"
	name="$(json_get ".name" "$cookie")"
	value="$(json_get ".value" "$cookie")"
	path="$(json_get '.path // "/"' "$cookie")"
	lifetime="$(json_get '.lifetime // "-1"' "$cookie")"
	[ -n "$site" ] || error ".cookie[$cookie_count].site must be present (and not empty)"
	[ -n "$name" ] || error ".cookie[$cookie_count].name must be present (and not empty)"
	[ -n "$value" ] || error ".cookie[$cookie_count].value must be present (and not empty)"
	matches "$lifetime" -P "^\-?[0-9]+$" || error ".cookie[$cookie_count].lifetime must be an integer (is: '$lifetime')"
	arguments+=("--add-cookie" "$name" "$value" "$site" "$path" "$lifetime")
	cookie_count="$((cookie_count+1))"
done

#json number [-special]
function nav() {
	title="$(json_get '.title // ""' "$1")"
	url="$(json_get '.url // ""' "$1")"
	icon="$(json_get '.icon // ""' "$1")"
	[ -n "$site" ] || error ".nav$3[$2].title must be present (and not empty)"
	[ -n "$url" ] || error ".nav$3[$2].url must be present (and not empty)"
	arguments+=("--nav$3" "$url" "$title")
	if [ -n "$icon" ] ; then
		if [ ! -r "$icon" ] && matches "$icon" "/"; then
			
			error ".nav$3[$2].icon must be empty or point to a readable file"
		else
			arguments+=("--nav-icon" "$icon")
		fi
	fi
	for class in $(json_get '.classes // [] |.[]' "$1") ; do
		arguments+=("--nav-add-class" "$class")
	done
}

nav_count="0"
for nav in $(json_get_json '.nav // [] |.[]' "$json_config") ; do
	nav "$nav" "$nav_count"
	nav_count="$(( nav_count + 1 ))"
done

nav_count="0"
for nav in $(json_get_json '."nav-special" // [] |.[]' "$json_config") ; do
	nav "$nav" "$nav_count" "-special"
	nav_count="$(( nav_count + 1 ))"
done

[ -z "$has_error" ] || exit 2

### use those options

exec interactive-kiosk-browser "${arguments[@]}"
