#!/bin/bash -e
###
# This script will compile a 32- or 64-bit Windows executable from 64-bit Linux
###
# Setting arch to "i686" will create a 32-bit Windows executable (runs everywhere)
# Setting it to "x86-64" will create a 64-bit Windows executable
arch="x86-64"
if [ "$1" != "" ]; then
	arch="$1"
fi
os=`uname`
if [ "$os" = "Linux" ]; then
        me=`whoami`
        sudo=""
        if [ "$me" != "root" ]; then
                # We need to do a priv esc
                failed=0
                which sudo && sudo=`which sudo` || true
                which doas && sudo=`which doas` || true
                if [ -z "$sudo" ]; then
                        echo "Error: Not running as root and couldn't find sudo nor doas"
                        exit 1
                fi
        fi

	$sudo apt-get update
	$sudo apt install -y wget git cmake mingw-w64-common asciidoctor pkg-config libbotan-2-dev xz-utils zlib1g
	# Don't re-download the tar file if we already have the /usr/lib/mxe dir
	if [ ! -d /usr/lib/mxe ]; then
		wget -O- https://hax0rbana.org/mirror/mxe_snapshot.tar.xz | $sudo tar -JxC /
	fi
	export PATH=/usr/lib/mxe/usr/bin:$PATH
	git submodule update --init
	cd keepassxc
	${arch/-/_}-w64-mingw32.static-cmake -DWITH_XC_ALL=OFF -DWITH_XC_AUTOTYPE=OFF .
	cd ..
	/usr/lib/mxe/usr/${arch/-/_}-w64-mingw32.static/qt5/bin/qmake TARGET_ARCH=${arch/-/_} client/client.pro
	make -j $(nproc) release
	# Creates: release/signet.exe
else
        echo "This script only runs on Linux"
fi
