#!/bin/bash

## Copyright (C) 2023 - 2023 ENCRYPTED SUPPORT LP <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 -B WhonixStarter.lpr --cpu=x86_64 --os=win64 --compiler=/usr/bin/ppcrossx64

## lazbuild requires build dependency packages from Debian trixie.
dpkg -l | grep fp-units-win-base >/dev/null
dpkg -l | grep fp-units-win-rtl >/dev/null
dpkg -l | grep fp-units-win-fcl >/dev/null
dpkg -l | grep fp-units-win-misc >/dev/null

lazbuild -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


true "$0: SUCCESS"
