#!/data/data/com.termux/files/usr/bin/bash
# adl-create-launcher.sh — generate a one-command desktop launcher.
# Runs in the Termux host environment. Writes ~/start-desktop.sh from your
# configuration; backs up any existing launcher first.
set -euo pipefail

VERSION="1.0.0"
DISTRO=""
DESKTOP=""
USERNAME=""
DISPLAY_NUM=":1"
AUDIO=1
DPI=""

usage() {
  cat <<'EOF'
Usage: adl-create-launcher.sh --distro <debian|ubuntu|alpine|archlinux>
                              --desktop <xfce|mate|lxqt|plasma>
                              --username <name>
                              [--display :1] [--no-audio] [--dpi N]

Writes ~/start-desktop.sh, which starts (in order): PulseAudio forwarding
(unless --no-audio), the Termux:X11 display, and your desktop session as the
given user. An existing launcher is backed up with a timestamp suffix.

Options:
  --display :N   X display number (default :1).
  --no-audio     Skip PulseAudio startup.
  --dpi N        Pass -dpi N to termux-x11 (for high-DPI screens).
  --version      Print version.   --help   Show this help.
EOF
}

while [ $# -gt 0 ]; do
  case "$1" in
    --distro) shift; DISTRO="${1:-}" ;;
    --desktop) shift; DESKTOP="${1:-}" ;;
    --username) shift; USERNAME="${1:-}" ;;
    --display) shift; DISPLAY_NUM="${1:-}" ;;
    --no-audio) AUDIO=0 ;;
    --dpi) shift; DPI="${1:-}" ;;
    --version) printf '%s\n' "$VERSION"; exit 0 ;;
    --help|-h) usage; exit 0 ;;
    *) printf 'Unknown option: %s\n' "$1" >&2; usage >&2; exit 2 ;;
  esac
  shift
done

# Allowlist validation of every value that reaches the generated script.
case "$DISTRO" in debian|ubuntu|alpine|archlinux) ;; *) printf 'Invalid --distro.\n' >&2; exit 2 ;; esac
case "$DESKTOP" in
  xfce) SESSION="startxfce4" ;;
  mate) SESSION="mate-session" ;;
  lxqt) SESSION="startlxqt" ;;
  plasma) SESSION="startplasma-x11" ;;
  *) printf 'Invalid --desktop.\n' >&2; exit 2 ;;
esac
if ! printf '%s' "$USERNAME" | grep -Eq '^[a-z][a-z0-9_-]{0,31}$'; then
  printf 'Invalid --username.\n' >&2; exit 2
fi
if ! printf '%s' "$DISPLAY_NUM" | grep -Eq '^:[0-9]{1,2}$'; then
  printf 'Invalid --display (use :0 to :99).\n' >&2; exit 2
fi
if [ -n "$DPI" ] && ! printf '%s' "$DPI" | grep -Eq '^[0-9]{2,3}$'; then
  printf 'Invalid --dpi (use a number like 120).\n' >&2; exit 2
fi

if [ ! -d "/data/data/com.termux/files/usr" ]; then
  printf 'This script must run inside the Termux app.\n' >&2
  exit 1
fi

OUT="$HOME/start-desktop.sh"
if [ -f "$OUT" ]; then
  backup="$OUT.$(date +%Y%m%d-%H%M%S).bak"
  cp "$OUT" "$backup"
  printf 'Existing launcher backed up to %s\n' "$backup"
fi

X11_ARGS="$DISPLAY_NUM"
if [ -n "$DPI" ]; then X11_ARGS="$DISPLAY_NUM -dpi $DPI"; fi
ENV_VARS="DISPLAY=$DISPLAY_NUM"
if [ "$AUDIO" -eq 1 ]; then ENV_VARS="$ENV_VARS PULSE_SERVER=127.0.0.1"; fi

{
  printf '#!/data/data/com.termux/files/usr/bin/bash\n'
  printf '# ADL desktop launcher — generated by adl-create-launcher.sh v%s\n' "$VERSION"
  printf 'set -eu\n'
  if [ "$AUDIO" -eq 1 ]; then
    printf 'pulseaudio --start --load="module-native-protocol-tcp auth-ip-acl=127.0.0.1 auth-anonymous=1" --exit-idle-time=-1 || true\n'
  fi
  printf 'termux-x11 %s >/dev/null 2>&1 &\n' "$X11_ARGS"
  printf 'sleep 3\n'
  printf 'proot-distro login %s --user %s --shared-tmp -- env %s dbus-launch --exit-with-session %s\n' \
    "$DISTRO" "$USERNAME" "$ENV_VARS" "$SESSION"
} > "$OUT"
chmod +x "$OUT"

printf 'Launcher written: %s\n' "$OUT"
printf 'Start your desktop with:  ~/start-desktop.sh   (then open the Termux:X11 app)\n'
