#!/bin/sh
# The first-run setup's choices, applied to this user: at their first sign-in
# (sg-run-explorer runs this before the shell), and again only if the
# first-run setup is run again. The region (format and country), the keyboard
# layouts Windows programs see, the per-user privacy switches and voice typing
# (on with the microphone), into HKCU
# with one reg.exe import. A user's later changes in Settings stay theirs.
#
# --welcome: only the region and the keyboard layouts, for the system accounts
# that draw the sign-in and lock screens (sg-services-start, sg-oobed) -- as
# Windows' setup copies them to the welcome screen and system accounts. The
# lock screen's clock was in US English (11:13 for 23:13) on an en-GB machine.
#
# SPDX-License-Identifier: AGPL-3.0-or-later
set -u
SG_LIB="${SG_LIB:-/usr/lib/stained-glass}"
# shellcheck source=lib/sg-common.sh
. "$SG_LIB/sg-common.sh"
sg_wine_env
CONF="${SG_OOBE_CONF:-/etc/stained-glass/oobe.conf}"
[ -r "$CONF" ] || exit 0

welcome=0; [ "${1:-}" = --welcome ] && [ -z "${SG_MUTANT_OOBE_NO_WELCOME:-}" ] && welcome=1
get() { sed -n "s/^$1=//p" "$CONF" | tail -1; }
stamp=$(get STAMP)
locale=$(get REGION_LOCALE) geo=$(get REGION_GEO) ids=$(get KEYBOARD_IDS)
printf '%s' "$stamp" | grep -Eq '^[0-9]+$' || { sg_log "oobe: no stamp in $CONF"; exit 0; }
key='HKCU\Software\Stained Glass\OOBE'
val=Applied; [ "$welcome" = 1 ] && val=AppliedWelcome
applied=$(wine reg query "$key" /v "$val" 2>/dev/null | tr -d '\r' | awk -v v="$val" '$1 == v { print $3 }')
[ "$applied" = "$stamp" ] && exit 0

onoff() { [ "$1" = 1 ] && echo Allow || echo Deny; }
reg=$(mktemp "${XDG_RUNTIME_DIR:-/tmp}/sg-oobe-user.XXXXXX") || exit 0
cs='HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore'
{
    printf 'Windows Registry Editor Version 5.00\r\n\r\n'
    if printf '%s' "$locale" | grep -Eq '^[a-z]{2,3}-[A-Z]{2}$'; then
        printf '[HKEY_CURRENT_USER\\Control Panel\\International]\r\n"LocaleName"="%s"\r\n\r\n' "$locale"
    fi
    if printf '%s' "$geo" | grep -Eq '^[0-9]{1,6}$'; then
        printf '[HKEY_CURRENT_USER\\Control Panel\\International\\Geo]\r\n"Nation"="%s"\r\n' "$geo"
        # The country's two letters, as SetUserGeoID keeps them: the region's.
        case "$locale" in *-??) printf '"Name"="%s"\r\n' "${locale##*-}" ;; esac
        printf '\r\n'
    fi
    if printf '%s' "$ids" | grep -Eq '^[0-9a-f]{8}(,[0-9a-f]{8})?$'; then
        printf '[HKEY_CURRENT_USER\\Keyboard Layout\\Preload]\r\n'
        n=1; for id in $(echo "$ids" | tr ',' ' '); do printf '"%d"="%s"\r\n' "$n" "$id"; n=$((n + 1)); done
        printf '\r\n'
    fi
    # the rest is a person's own: not the system accounts'
    if [ "$welcome" = 0 ]; then
        for cap in microphone location; do
            case $cap in microphone) v=$(get MICROPHONE) ;; *) v=$(get LOCATION) ;; esac
            case "$v" in 0|1) ;; *) continue ;; esac
            printf '[%s\\%s]\r\n"Value"="%s"\r\n\r\n' "$cs" "$cap" "$(onoff "$v")"
            printf '[%s\\%s\\NonPackaged]\r\n"Value"="%s"\r\n\r\n' "$cs" "$cap" "$(onoff "$v")"
        done
        # The microphone page names voice typing ("Let voice typing (Start key +
        # H) and other programs use the microphone"): On turns voice typing on
        # too -- it has a switch of its own, off by default, and Win+H said
        # "Voice typing is off" after the user had just said yes.
        [ "$(get MICROPHONE)" = 1 ] && \
            printf '[HKEY_CURRENT_USER\\Software\\Stained Glass\\Speech]\r\n"Enabled"=dword:00000001\r\n\r\n'
        v=$(get ADVERTISING); case "$v" in 0|1)
            printf '[HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\AdvertisingInfo]\r\n"Enabled"=dword:0000000%s\r\n\r\n' "$v" ;; esac
        v=$(get TAILORED); case "$v" in 0|1)
            printf '[HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Privacy]\r\n"TailoredExperiencesWithDiagnosticDataEnabled"=dword:0000000%s\r\n\r\n' "$v" ;; esac
    fi
    printf '[HKEY_CURRENT_USER\\Software\\Stained Glass\\OOBE]\r\n"%s"="%s"\r\n' "$val" "$stamp"
} > "$reg"
if wine reg import "$(winepath -w "$reg" 2>/dev/null)" >/dev/null 2>&1; then
    sg_log "oobe: applied the first-run setup's choices (region $locale)"
else
    sg_log "oobe: could not apply the first-run setup's choices"
fi
rm -f "$reg"
exit 0
