#!/bin/sh
config_dir="${XDG_CONFIG_HOME:-$HOME/.config}/mpf"
config="$config_dir/config.json"
aaru_p=aaru5
dic_p=DiscImageCreator.out
red_p=redumper-mpf
# Without a HOME there is no sane home-relative output directory; leave the
# key alone rather than rewriting it to a root-owned "/ISO".
out_p=""
[ -n "$HOME" ] && out_p="$HOME/ISO"
mkdir -p "$config_dir" 2>/dev/null

# Does a configured tool value resolve the way MPF (#979) resolves it? A
# value containing a separator must exist as a file; a bare name must be
# found on $PATH (command -v mirrors MPF's runtime-dir + $PATH lookup).
resolves() {
    case "$1" in
        "")  return 1 ;;
        */*) [ -e "$1" ] ;;
        *)   command -v "$1" >/dev/null 2>&1 ;;
    esac
}

# The output path must be absolute: a relative one resolves against the
# working directory, which for a /usr-installed app is wherever it was
# started from. The directory need not exist -- MPF creates it.
is_abs() {
    case "$1" in /*) return 0 ;; *) return 1 ;; esac
}

if [ ! -s "$config" ]; then
    cat > "$config" <<JSON
{
  "AaruPath": "$aaru_p",
  "DiscImageCreatorPath": "$dic_p",
  "RedumperPath": "$red_p",
  "DefaultOutputPath": "${out_p:-ISO}"
}
JSON
elif command -v jq >/dev/null 2>&1; then
    ca=$(jq -r '.AaruPath // ""' "$config" 2>/dev/null)
    cd_=$(jq -r '.DiscImageCreatorPath // ""' "$config" 2>/dev/null)
    cr=$(jq -r '.RedumperPath // ""' "$config" 2>/dev/null)
    co=$(jq -r '.DefaultOutputPath // ""' "$config" 2>/dev/null)
    fa=0; fd=0; fr=0; fo=0
    resolves "$ca"  || fa=1
    resolves "$cd_" || fd=1
    resolves "$cr"  || fr=1
    [ -n "$out_p" ] && { is_abs "$co" || fo=1; }
    if [ $((fa + fd + fr + fo)) -gt 0 ]; then
        tmp=$(mktemp -p "$config_dir" .config.json.XXXXXX 2>/dev/null)
        if [ -n "$tmp" ] && jq \
            --arg ap "$aaru_p" --arg dp "$dic_p" --arg rp "$red_p" \
            --arg op "$out_p" \
            --argjson fa "$fa" --argjson fd "$fd" --argjson fr "$fr" \
            --argjson fo "$fo" '
            (if $fa == 1 then .AaruPath = $ap else . end)
            | (if $fd == 1 then .DiscImageCreatorPath = $dp else . end)
            | (if $fr == 1 then .RedumperPath = $rp else . end)
            | (if $fo == 1 then .DefaultOutputPath = $op else . end)
            ' "$config" > "$tmp" 2>/dev/null; then
            mv "$tmp" "$config"
        else
            [ -n "$tmp" ] && rm -f "$tmp"
        fi
    fi
fi

exec /usr/lib64/mpf-gui/MPF.Avalonia "$@"
