#!/usr/bin/env bash
# Build AyaanScriptPlus-Setup.msi from Linux using wixl (msitools).
#
# Why MSI instead of a custom .exe?
#   - Higher SmartScreen / Defender reputation (standard Windows format)
#   - Proper Add/Remove Programs entry, uninstall, upgrade handling
#   - msiexec is whitelisted; custom mingw .exes are not
#
# What it does at install time:
#   1. Drops install-ayaanscript.ps1 into %LOCALAPPDATA%\AyaanScriptPlus
#   2. Launches it in a visible PowerShell window which:
#        - Installs rustup (if cargo isn't already on PATH)
#        - Runs `cargo install ayaan --force`
#        - Adds %USERPROFILE%\.cargo\bin to user PATH
#   3. Creates Start-Menu shortcuts pointing at ~/.cargo/bin/ayaan.exe

set -e
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

OUT="AyaanScriptPlus-Setup.msi"

if ! command -v wixl >/dev/null 2>&1; then
    echo "[!] wixl not found. Install with:"
    echo "      sudo dnf install -y msitools          # Fedora"
    echo "      sudo apt install -y wixl              # Debian/Ubuntu"
    exit 1
fi

if [ ! -f installer.wxs ]; then
    echo "[!] installer.wxs missing in $SCRIPT_DIR"
    exit 1
fi
if [ ! -f install-ayaanscript.ps1 ]; then
    echo "[!] install-ayaanscript.ps1 missing in $SCRIPT_DIR"
    exit 1
fi

rm -f "$OUT"
wixl -v installer.wxs -o "$OUT"

if [ -f "$OUT" ]; then
    SIZE=$(du -h "$OUT" | cut -f1)
    echo ""
    echo "+================================================+"
    echo "|  Built: $OUT"
    echo "|  Size:  $SIZE"
    echo "|  Path:  $SCRIPT_DIR/$OUT"
    echo "+================================================+"
    echo ""
    echo "Users open this with a double-click."
    echo "Windows runs it via msiexec — no SmartScreen \"unrecognized publisher\""
    echo "block (unless the file lacks Mark-of-the-Web from your distribution)."
fi
