#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only
# Activates Bananarchy Duress Mode (blank profile in /tmp/duress_home,
# Nordic theme & wallpaper, SD card unmounted, restricted apps disabled).

export DISPLAY="${DISPLAY:-:0}"
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/10000}"

# Cache real TAC before switching if not already cached
if [ ! -f /tmp/bananarchy_real_tac ]; then
  doas -n /usr/libexec/msm-modem-uim-selection imei 2>/dev/null | \
    awk -F= '/^IMEI[12]=/ && length($2) >= 8 { print substr($2, 1, 8) }' > /tmp/bananarchy_real_tac.tmp 2>/dev/null || true
  if [ -s /tmp/bananarchy_real_tac.tmp ]; then
    mv -f /tmp/bananarchy_real_tac.tmp /tmp/bananarchy_real_tac
  else
    rm -f /tmp/bananarchy_real_tac.tmp
  fi
fi

printf '1\n' > /tmp/bananarchy_duress_mode
chmod 0644 /tmp/bananarchy_duress_mode 2>/dev/null || true

# Generate session-persistent Luhn-valid random IMEIs from the device TAC
if [ ! -s /tmp/bananarchy_duress_imei ]; then
  TAC1="$(sed -n '1p' /tmp/bananarchy_real_tac 2>/dev/null | tr -cd '0-9' | cut -c1-8)"
  TAC2="$(sed -n '2p' /tmp/bananarchy_real_tac 2>/dev/null | tr -cd '0-9' | cut -c1-8)"
  [ "${#TAC1}" -ne 8 ] && TAC1="35829909"
  [ "${#TAC2}" -ne 8 ] && TAC2="$TAC1"
  S1="$(od -An -N4 -tu4 /dev/urandom 2>/dev/null | tr -d ' ')"
  S2="$(od -An -N4 -tu4 /dev/urandom 2>/dev/null | tr -d ' ')"
  awk -v t1="$TAC1" -v t2="$TAC2" -v s1="${S1:-12345}" -v s2="${S2:-67890}" '
    function gen_imei(tac, seed,   snr, body, sum, i, d, chk) {
      srand(seed)
      snr = sprintf("%06d", int(rand() * 900000) + 100000)
      body = substr(tac, 1, 8) snr
      sum = 0
      for (i = 1; i <= 14; i++) {
        d = substr(body, i, 1) + 0
        if (i % 2 == 0) {
          d = d * 2
          if (d > 9) d -= 9
        }
        sum += d
      }
      chk = (10 - (sum % 10)) % 10
      return body chk
    }
    BEGIN {
      i1 = gen_imei(t1, s1 + 0)
      i2 = gen_imei(t2, s2 + 17)
      if (i2 == i1) i2 = gen_imei(t2, s2 + 99991)
      printf "IMEI1=%s\nIMEI2=%s\nIMEI_SV=01\n", i1, i2
    }
  ' > /tmp/bananarchy_duress_imei
fi

# Unmount SD card immediately
doas -n /usr/bin/sxmo_sdcard.sh --force-unmount >/dev/null 2>&1 || true

# Initialize clean blank profile directory
DURESS_HOME="/tmp/duress_home"
rm -rf "$DURESS_HOME" 2>/dev/null || true
mkdir -p \
  "$DURESS_HOME/Desktop" \
  "$DURESS_HOME/Documents" \
  "$DURESS_HOME/Downloads" \
  "$DURESS_HOME/Music" \
  "$DURESS_HOME/Pictures" \
  "$DURESS_HOME/Projects" \
  "$DURESS_HOME/Videos" \
  "$DURESS_HOME/.config/sxmo" \
  "$DURESS_HOME/.config/dunst" \
  "$DURESS_HOME/.local/share/sxmo/modem" \
  "$DURESS_HOME/.local/share/sxmo/block" \
  "$DURESS_HOME/.local/share/sxmo/notifications" \
  "$DURESS_HOME/.cache/sxmo" \
  "$DURESS_HOME/.local/state" \
  "$DURESS_HOME/.xthemes"

: > "$DURESS_HOME/.config/sxmo/contacts.tsv"
: > "$DURESS_HOME/.config/sxmo/block.tsv"
: > "$DURESS_HOME/.local/share/sxmo/modem/modemlog.tsv"
: > "$DURESS_HOME/.local/share/sxmo/block/modemlog.tsv"

# Clear active notification files
rm -rf "$XDG_RUNTIME_DIR/sxmo_notifications"/* 2>/dev/null || true

# Set Nordic theme (NordFrost.xth) and nordic-peaks.png wallpaper only
NORD_SRC="/etc/skel/.xthemes/NordFrost.xth"
[ ! -r "$NORD_SRC" ] && NORD_SRC="/home/user/.xthemes/NordFrost.xth"
if [ -r "$NORD_SRC" ]; then
  cp -f "$NORD_SRC" "$DURESS_HOME/.xthemes/NordFrost.xth"
  cp -f "$NORD_SRC" "$DURESS_HOME/.Xresources"
fi

printf '/usr/share/wallpapers/nordic-peaks.png\n' > "$DURESS_HOME/.config/sxmo/wallpaper"
printf 'fill\n' > "$DURESS_HOME/.config/sxmo/wallpaper_mode"

if [ -r "$DURESS_HOME/.Xresources" ]; then
  xrdb -load "$DURESS_HOME/.Xresources" >/dev/null 2>&1 || true
fi
feh --no-fehbg --bg-fill /usr/share/wallpapers/nordic-peaks.png >/dev/null 2>&1 </dev/null &

# Terminate any open terminals, active privacy tunnels, and restricted apps
pkill -9 -x st >/dev/null 2>&1 || true
doas -n /usr/bin/bananarchy-privacy-ctl stop-all >/dev/null 2>&1 || true

# Notify all plugins to close overlays and reload theme/profile
for p in dmenu xdrawer passcode tox irc sip quagr engmenu privacy dialer calllog contacts sms files camera themes modem networks ringtones wallpapers music email smallweb webarchy gps; do
  fifo="/tmp/dwm_plugin_${p}.fifo"
  if [ -p "$fifo" ]; then
    printf 'CLOSE\nRELOAD_THEME\n' > "$fifo" 2>/dev/null || true
  fi
done

/usr/bin/bananarchy-sync-dunst-theme >/dev/null 2>&1 &
dunstctl close-all >/dev/null 2>&1 || true
exit 0
