#!/bin/sh
#
# Add or remove the Himmelblau SSO extension in the policies.json files of
# Firefox and Thunderbird. These applications read a single policies.json, so
# the extension is merged into the existing file instead of replacing it.
# Run it as root after installing the package himmelblau-sso-policies, and
# with "remove" before uninstalling it.
#
# Usage: /usr/libexec/himmelblau/sso-policies add|remove
#
# The URLs of the extensions are taken from the example files installed by the
# package. ETCDIR and DOCDIR can be set to test the script.

set -eu

ETCDIR="${ETCDIR:-/etc}"
DOCDIR="${DOCDIR:-/usr/share/doc/packages/himmelblau-sso-policies}"

# Matches the URLs of the extension of any version, so old ones are replaced
FIREFOX_PATTERN='linux_entra_sso-[0-9][0-9.]*\.xpi$'
THUNDERBIRD_PATTERN='linux_entra_sso-[0-9][0-9.]*\.thunderbird\.xpi$'

action="${1:-}"
case "$action" in
add | remove) ;;
*)
    echo "Usage: $0 add|remove" >&2
    exit 1
    ;;
esac

command -v jq >/dev/null 2>&1 || {
    echo "jq not found, the SSO extension has to be configured manually" >&2
    exit 0
}

tmp="$(mktemp)"
trap 'rm -f "$tmp"' EXIT

# Add the extension of the example file to the policies file. The file is only
# written if jq succeeded, so an invalid file is never overwritten.
add_policy() {
    file="$1"
    example="$2"
    pattern="$3"

    [ -f "$example" ] || return 1
    url="$(jq -r '.policies.Extensions.Install[0] // empty' "$example")" || return 1
    [ -n "$url" ] || return 1

    if [ ! -f "$file" ]; then
        mkdir -p "$(dirname "$file")"
        install -m 0644 "$example" "$file"
        return
    fi

    # Nothing to do if the extension is there and no other version of it is left
    if jq -e --arg url "$url" --arg pattern "$pattern" '
        (.policies.Extensions.Install[]? | select(. == $url)) and
        ([.policies.Extensions.Install[]? | select(type == "string" and test($pattern) and . != $url)] | length == 0)
        ' "$file" >/dev/null 2>&1; then
        return
    fi

    jq --arg url "$url" --arg pattern "$pattern" '
        .policies.Extensions.Install = (
            ([.policies.Extensions.Install[]? | select((type == "string" and test($pattern)) | not)] + [$url])
            | unique
        )' "$file" >"$tmp" || return 1
    # Write in place to keep the owner and the mode of the file
    cat "$tmp" >"$file"
}

# Remove the extension from the policies file and clean up what is left empty
remove_policy() {
    file="$1"
    pattern="$2"

    [ -f "$file" ] || return 0
    jq --arg pattern "$pattern" '
        if .policies.Extensions.Install then
            .policies.Extensions.Install |= map(select((type == "string" and test($pattern)) | not))
        else . end
        | if .policies.Extensions.Install == [] then del(.policies.Extensions.Install) else . end
        | if .policies.Extensions == {} then del(.policies.Extensions) else . end' "$file" >"$tmp" || return 1
    if jq -e '.policies == {}' "$tmp" >/dev/null 2>&1; then
        rm -f "$file"
    else
        cat "$tmp" >"$file"
    fi
}

status=0
for app in firefox thunderbird; do
    failed=0
    case "$app" in
    firefox) pattern="$FIREFOX_PATTERN" ;;
    thunderbird) pattern="$THUNDERBIRD_PATTERN" ;;
    esac
    file="$ETCDIR/$app/policies/policies.json"
    if [ "$action" = add ]; then
        add_policy "$file" "$DOCDIR/$app-policies.json.example" "$pattern" || failed=1
    else
        remove_policy "$file" "$pattern" || failed=1
    fi
    if [ "$failed" -ne 0 ]; then
        echo "Failed to update $file" >&2
        status=1
    fi
done
exit "$status"
