#!/bin/bash

version=VARA%20Products/VARA%20FM%20v4.4.0%20setup.zip

APPDIR="${HOME}"/.local/share/applications

#Installing wine
if [ $arch == 32 ]; then
  "$DIRECTORY/manage" install-if-not-installed 'Wine (x86)' || error 'Failed to install wine'
elif [ $arch == 64 ]; then
  "$DIRECTORY/manage" install-if-not-installed 'Wine (x64)' || error 'Failed to install wine'
fi

#Configuring wineprefix for VARA programs
BOX86_NOBANNER=1 BOX64_NOBANNER=1 winetricks -q vb6run pdh_nt4 sound=alsa

#Downloading VARA FM
VARAFMDIR=/tmp/VARAFM
mkdir -p "${VARAFMDIR}"
wget "https://downloads.winlink.org/$version" -O "${VARAFMDIR}/VARAFM-setup.zip" || error 'Failed to download VARA FM Installer from winlink.org'

#Extracting VARA FM installer archives
unzip -o "${VARAFMDIR}/VARAFM-setup.zip" -d "${VARAFMDIR}" || error 'Failed to unzip VARA FM archive'
VARAINSTALLEREXE=$(find "${VARAFMDIR}" -type f -iname "*setup*.exe")

#Manually installing VARA FM program files
# extracting program files from VARA FM installer
install_packages innoextract libsdl2-2.0-0 libxkbregistry0 || exit 1
innoextract -d "${VARAFMDIR}" "${VARAINSTALLEREXE}" || error 'Failed to extract program files from VARA FM installer'
# finding your default wineprefix directory
if [ -z "${WINEPREFIX}" ]; then
    PREFIXDIR="${HOME}"/.wine #if "WINEPREFIX=" envvar is unset or set to empty string, wineprefix location is "~/.wine"
else
    PREFIXDIR="${WINEPREFIX}" #if "WINEPREFIX=" envvar was previously set, wineprefix location is "$WINEPREFIX"
fi
# moving VARA FM program files to user's default wineprefix
mkdir "${PREFIXDIR}/drive_c/VARAFM" 2>/dev/null #VARAFM folder with old settings might already exist
cp -r "${VARAFMDIR}"/app/* "${PREFIXDIR}"/drive_c/VARAFM/ || error 'Could not copy VARA program files to your wineprefix' #will not overwrite old settings
# registering VB6 OCX and DLL files with your wineprefix
# NOTE: on 64-bit (new-WoW64) wine the only regsvr32 is 64-bit and cannot load these 32-bit OCX
# ('Failed to load DLL'), so this step fails on arm64/Wine (x64). That is non-fatal: the VB6
# controls VARA needs were already registered by 'winetricks vb6run' above, and the app files are
# already in place. So warn instead of aborting the whole install.
BOX86_NOBANNER=1 BOX64_NOBANNER=1 WINEDEBUG=-all wine regsvr32 "${PREFIXDIR}"/drive_c/VARAFM/OCX/*.*[A-Z] /s || warning 'VARA FM OCX self-registration skipped (64-bit regsvr32 cannot load 32-bit OCX; winetricks vb6run already registered the VB6 controls).'

#Configuring box64 dynarec for VARA FM (ARM64 / box64)
# VARA FM is multi-threaded. box64's default weak memory model races VARA's threads and corrupts
# its callsign validator, so MYCALL always returns "WRONG" and NOTHING connects via RMS Express or
# Pat (VERSION works, so the TCP link looks fine - it's the command handling that's broken). The
# two flags below are the MINIMAL set that fixes it - both STRONGMEM and SAFEFLAGS are required
# (STRONGMEM 1/2/3 all work; 2 chosen). IMPORTANT: do NOT add more box64 flags here
# (BIGBLOCK/CALLRET/FASTNAN/FASTROUND/X87DOUBLE) - they are not needed for the fix AND they break
# VARA's VB6 GUI (windows become unclickable, even the X/close button stops working). Written to
# ~/.box64rc so they apply whenever VARAFM.exe runs (incl. when RMS Express / Pat auto-launch it).
if command -v box64 >/dev/null 2>&1; then
  if ! grep -q '^\[VARAFM\.exe\]' "${HOME}/.box64rc" 2>/dev/null; then
    cat >> "${HOME}/.box64rc" <<'BOX64RC'

[VARAFM.exe]
BOX64_DYNAREC_STRONGMEM=2
BOX64_DYNAREC_SAFEFLAGS=2
BOX64RC
    status 'Added [VARAFM.exe] box64 dynarec flags to ~/.box64rc (required for VARA to work on ARM64).'
  fi
fi

#Removing tmp files
rm -rf "${VARAFMDIR}"

#Adding the user to the USB dialout group so that they can access radio USB CAT control later if needed
sudo usermod -a -G dialout $USER

#Creating Desktop Entry
mkdir -p "${APPDIR}"/VARA
echo "[Desktop Entry]
Name=VARA FM
GenericName=VARA FM
Comment=VARA FM is a shareware ham radio OFDM software modem for RMS Express and other messaging clients.
Exec=env WINEPREFIX=\"${PREFIXDIR}\" WINEDEBUG=-all wine \"${PREFIXDIR}/drive_c/VARAFM/VARAFM.exe\"
Icon=$(dirname "$0")/icon-64.png
Terminal=false
StartupNotify=false
Type=Application
StartupWMClass=varafm.exe
Categories=Utility;" > "${APPDIR}"/VARA/varafm.desktop || error 'Failed to create menu button!'
sudo rm -rf "${APPDIR}/wine/Programs/VARA FM" # remove wine's auto-generated VARA FM program icon from the start menu

true
