#!/bin/sh
# SPDX-License-Identifier: MIT
# Only explicit, interactive system maintenance.
set -u
report_dir=
reported=0
report_result() {
    [ -n "$report_dir" ] || return 0
    if printf '%s\n' "$1" > "$report_dir/result.tmp" && mv -- "$report_dir/result.tmp" "$report_dir/result"; then
        reported=1
    fi
}
if [ "${1:-}" = --report ]; then
    [ "$#" -ge 3 ] || exit 2
    report_dir=$2
    shift 2
    [ -d "$report_dir" ] || exit 2
    trap 'code=$?; [ "$reported" -eq 1 ] || report_result "$code"' EXIT
    trap 'exit 129' HUP
    trap 'exit 130' INT
    trap 'exit 143' TERM
    printf '%s\n' "$$" > "$report_dir/started" || exit 1
fi
action=${1:-}
case "$action" in
    update) set -- apt-get update ;;
    upgrade) set -- apt-get upgrade ;;
    autopurge) set -- apt-get autoremove --purge ;;
    clean) set -- apt-get clean ;;
    setup-printing) ;;
    language) shift; [ "$#" -eq 2 ] || exit 2
        set -- /usr/lib/erdos/configure-language apply "$@" ;;
    charge-limit) shift; [ "$#" -eq 2 ] || exit 2
        set -- /usr/lib/erdos/configure-power charge "$@" ;;
    lid) shift; [ "$#" -eq 3 ] || exit 2
        set -- /usr/lib/erdos/configure-power "$@" ;;
    translations) [ "$#" -eq 1 ] || exit 2
        set -- /usr/lib/erdos/configure-language translations ;;
    *) echo 'Usage: erdos-maintenance {update|upgrade|autopurge|clean|setup-printing|language LOCALE FORMATS|translations|lid BATTERY PLUGGED_IN DOCKED|charge-limit BATTERY {on|off}}' >&2; exit 2 ;;
esac

run_step() {
    printf 'Command: sudo'; printf ' %s' "$@"; printf '\n\n'
    sudo -- "$@"
}

if [ "$action" = setup-printing ]; then
    printf 'ErdOS Printing\n\n'
    printf 'Setup installs CUPS and grants your account printer administration access.\n\n'
elif [ "$action" = charge-limit ]; then
    printf 'ErdOS Battery Charge Limit\n\nChanges apply immediately and persist after reboot.\n\n'
elif [ "$action" = lid ]; then
    printf 'ErdOS Lid Behavior\n\nChanges apply to all users after restarting the computer.\n\n'
elif [ "$action" = language ] || [ "$action" = translations ]; then
    printf 'ErdOS Language & Region\n\nChanges apply to all users. Log out and sign in again afterward.\n\n'
else
    printf 'ErdOS Software\n\n'
fi
if [ "$action" = charge-limit ]; then
    printf 'Save the selected charge limit. Ctrl+C cancels.\n\n'
elif [ "$action" = lid ]; then
    printf 'Save lid-close behavior. No service will be restarted. Ctrl+C cancels.\n\n'
else
    printf 'Review package changes before confirming. Ctrl+C cancels.\n\n'
fi
result=0
if [ "$action" = setup-printing ]; then
    account=$(id -un) &&
        run_step apt update &&
        run_step apt install --no-install-recommends cups &&
        run_step adduser -- "$account" lpadmin &&
        run_step systemctl enable --now cups.service || result=$?
else
    run_step "$@" || result=$?
fi
report_result "$result"
printf '\n'
if [ "$result" -eq 0 ]; then
    printf 'Finished successfully.\n'
    if [ "$action" = setup-printing ]; then
        printf 'Log out and back in to use your printer administration access.\n'
        if [ -t 0 ] && [ -n "${LABWC_PID:-}" ]; then
            printf '\nSave your work before logging out.\n'
            printf 'Press Enter to log out now, or type later and press Enter to stay signed in: '
            if IFS= read -r answer && [ -z "$answer" ]; then
                if ! labwc --exit; then
                    printf '\nCould not end the desktop session. Use Logout from the desktop menu.\n'
                    printf 'Press Enter to close this terminal.'
                    IFS= read -r _ || :
                fi
            fi
            exit 0
        fi
    fi
else
    printf 'Command failed or was cancelled (exit %s). Read the output above.\n' "$result"
fi
printf '\nPress Enter to close this terminal.'
IFS= read -r _ || :
exit "$result"
