#!/usr/bin/env bash

# SPDX-License-Identifier: MIT

set -euo pipefail

root=${PLASMA_ALWAYS_SHOW_UNLOCK_ROOT:-/}
patch_file=${PLASMA_ALWAYS_SHOW_UNLOCK_PATCH:-/usr/share/plasma-always-show-unlock/LockScreenUi.patch}
visibility_patch=${PLASMA_ALWAYS_SHOW_UNLOCK_VISIBILITY_PATCH:-${patch_file%/*}/LockScreenUi-visibility.patch}
target_dir="$root/usr/share/plasma/shells/org.kde.plasma.desktop"
target="$target_dir/contents/lockscreen/LockScreenUi.qml"

usage() {
    printf 'Usage: %s {apply|revert|status}\n' "${0##*/}" >&2
}

patch_options=(
    --silent
    --force
    --fuzz=0
    --ignore-whitespace
    --no-backup-if-mismatch
    -p1
    -d "$target_dir"
)

run_patch() {
    local selected_patch=$1
    shift
    patch "${patch_options[@]}" "$@" < "$selected_patch"
}

check_patch() {
    local selected_patch=$1
    shift
    run_patch "$selected_patch" --dry-run "$@" >/dev/null 2>&1
}

write_patch() {
    local selected_patch=$1
    shift
    local temporary
    temporary=$(mktemp "$target.XXXXXX")
    cp --preserve=all -- "$target" "$temporary"
    if run_patch "$selected_patch" "$@" "$temporary"; then
        mv -f -- "$temporary" "$target"
    else
        rm -f -- "$temporary" "$temporary.rej"
        return 1
    fi
}

supports_visibility_policy() {
    local id
    for id in lockScreenUi lockScreenRoot fadeoutTimer; do
        [[ $(grep -Ec "^[[:space:]]*id[[:space:]]*:[[:space:]]*$id([[:space:];]|$)" "$target") -eq 1 ]] || return 1
    done
    grep -Eq 'property[[:space:]]+bool[[:space:]]+uiVisible[[:space:]]*:' "$target" &&
        grep -Eq 'running[[:space:]]*:.*!authenticator\.unlocked.*parent\.uiVisible' "$target"
}

apply_patch() {
    local selected_patch
    for selected_patch in "$patch_file" "$visibility_patch"; do
        if check_patch "$selected_patch" --reverse; then
            printf 'Plasma unlock prompt patch is already applied.\n'
            return
        fi
    done

    if check_patch "$patch_file"; then
        write_patch "$patch_file"
    elif supports_visibility_policy && check_patch "$visibility_patch"; then
        write_patch "$visibility_patch"
    else
        printf 'Error: %s does not have a supported Plasma lock-screen interface.\n' "$target" >&2
        return 1
    fi
    printf 'Applied the Plasma unlock prompt patch.\n'
}

revert_patch() {
    local selected_patch
    for selected_patch in "$patch_file" "$visibility_patch"; do
        if check_patch "$selected_patch" --reverse; then
            write_patch "$selected_patch" --reverse
            printf 'Reverted the Plasma unlock prompt patch.\n'
            return
        fi
    done
    if check_patch "$patch_file" || { supports_visibility_policy && check_patch "$visibility_patch"; }; then
        printf 'Plasma unlock prompt patch is not applied.\n'
        return
    fi
    printf 'Error: %s is neither a supported original nor patched file.\n' "$target" >&2
    return 1
}

if (( $# != 1 )); then
    usage
    exit 2
fi

if [[ ! -f $target ]]; then
    printf 'Error: Plasma lock screen not found at %s.\n' "$target" >&2
    exit 1
fi
if [[ ! -r $patch_file || ! -r $visibility_patch ]]; then
    printf 'Error: unlock prompt patch files are missing.\n' >&2
    exit 1
fi

case $1 in
    apply)
        apply_patch
        ;;
    revert)
        revert_patch
        ;;
    status)
        if check_patch "$patch_file" --reverse || check_patch "$visibility_patch" --reverse; then
            printf 'Plasma unlock prompt patch is applied.\n'
        elif check_patch "$patch_file" || { supports_visibility_policy && check_patch "$visibility_patch"; }; then
            printf 'Plasma unlock prompt patch is not applied.\n'
            exit 1
        else
            printf 'Plasma lock screen is not supported by this patch.\n' >&2
            exit 2
        fi
        ;;
    *)
        usage
        exit 2
        ;;
esac
