#!/bin/bash

## Copyright (C) 2023 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -x
set -e
set -o pipefail
set -o nounset

true "$0: START"

## 1) sanity tests

command -v wixl >/dev/null
command -v xmllint >/dev/null
command -v lazbuild >/dev/null
## from package libfile-mimeinfo-perl
command -v mimetype >/dev/null

## 2) build Whonix-Starter (WhonixStarter.exe)

## Was functional without build dependency packages from Debian trixie.
#lazbuild --lazarusdir=/usr/lib/lazarus/4.0 -B WhonixStarter.lpr --cpu=x86_64 --os=win64 --compiler=/usr/bin/ppcrossx64

lazbuild --lazarusdir=/usr/lib/lazarus/4.0 -B WhonixStarter.lpr --cpu=x86_64 --os=win64

## 3) read values from xml file WhonixStarter.lpi

VERSION=$(xmllint --xpath 'string(//StringTable/@ProductVersion)' WhonixStarter.lpi)
MANUFACTURE=$(xmllint --xpath 'string(//StringTable/@CompanyName)' WhonixStarter.lpi)
DESCRIPTION=$(xmllint --xpath 'string(//StringTable/@Comments)' WhonixStarter.lpi)

## 4) build msi package for Whonix-Starter

wixl \
  --verbose \
  --arch x64 \
  --define whonixStarterVersion="$VERSION" \
  --define whonixStarterManufacturer="$MANUFACTURE" \
  --define whonixStarterDescription="$DESCRIPTION" \
  --output WhonixStarterInstaller.msi \
  WhonixStarterInstaller.wxs

## Debugging.
ls -la WhonixStarterInstaller.msi
du -sh WhonixStarterInstaller.msi
mimetype WhonixStarterInstaller.msi

cp WhonixStarterInstaller.msi "../deps/WhonixStarterInstaller.msi"

true "$0: SUCCESS"
