#!/bin/bash
# Script to install ADMS-14 in Wine on Debian Trixie (Windows 10, 32-bit prefix, .NET)

set -e

# Variables
WINEPREFIX="$HOME/.wine-adms14"
WINEARCH=win32
INSTALLER="$HOME/Downloads/FT5D_ADMS-14_ENG/setup.exe"
APPDIR="C:\\Program Files\\Yaesu Musen\\FT5D ADMS-14 EXP"
APPBIN="$APPDIR\\ADMS-14.exe"
DESKTOPFILE="$HOME/.local/share/applications/adms14.desktop"

export WINEPREFIX WINEARCH

# Ensure system is up-to-date
echo "[*] Updating package list..."
sudo apt update

# Install Wine and Winetricks
echo "[*] Installing Wine and Winetricks..."
sudo apt install -y wine winetricks wget cabextract

# Create a fresh 32-bit Wine prefix
if [ ! -d "$WINEPREFIX" ]; then
    echo "[*] Creating 32-bit Wine prefix at $WINEPREFIX"
    wineboot --init
fi

# Configure Wine to Windows 10
echo "[*] Setting Wine to Windows 10..."
winetricks -q settings win10

# Install .NET Framework 4.8
echo "[*] Installing .NET Framework 4.8..."
winetricks -q dotnet48

# Install Microsoft Visual C++ runtimes
echo "[*] Installing Visual C++ runtimes..."
winetricks -q vcrun2019

# Run the ADMS-14 installer
if [ -f "$INSTALLER" ]; then
    echo "[*] Running ADMS-14 installer..."
    wine "$INSTALLER"
else
    echo "[!] Installer not found: $INSTALLER"
    exit 1
fi

# Create .desktop launcher
echo "[*] Creating desktop shortcut..."
mkdir -p "$(dirname "$DESKTOPFILE")"
cat > "$DESKTOPFILE" <<EOF
[Desktop Entry]
Name=ADMS-14 (Yaesu FT5D)
Exec=env WINEPREFIX=$WINEPREFIX wine "$APPBIN"
Type=Application
StartupNotify=true
Icon=wine
Categories=Utility;
EOF

echo
echo "[✓] Installation complete."
echo "To run ADMS-14 from terminal:"
echo "WINEPREFIX=$WINEPREFIX wine \"$APPBIN\""
echo
echo "Or launch it from your applications menu as 'ADMS-14 (Yaesu FT5D)'."
