#!/bin/bash -e
###
# This script builds the signet executable for the platform that you are on.
# This means on 32-bit Linux, you'll get a 32-bit ELF; on 64-bit Linux, you'll
# get a 64-bit ELF, and so on.
#
# This script should be run as a normal user.  All dependencies should already
# be installed (which can be done with dependencies.sh).
###
arch="x86-64"
if [ "$1" != "" ]; then
	# pass in "i686" to build a 32-bit ELF
	arch="$1"
fi
os=`uname`
git submodule update --init
if [ "$os" = "Linux" ]; then
        me=`whoami`
        sudo=""
        if [ "$me" != "root" ]; then
                which sudo && sudo=`which sudo` || true
        fi
	if [ -z "$CONFIG" ]; then
		CONFIG="release"  # default value
	fi
	echo "Compiling $CONFIG build for $os $arch"
	cd keepassxc
	if [ "$arch" = "i686" ]; then
		export PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig
		# We have to have the normal botan package to make keepassxc happy
		# It'd be great if we can find a fix for this :-/
		$sudo apt-get install -y libbotan-2-dev
	fi
	cmake -DWITH_XC_ALL=OFF -DWITH_XC_AUTOTYPE=OFF .
	cd ..
	if [ "$arch" = "i686" ]; then
		# We have to have the i386 version for signet
		$sudo apt-get install -y libbotan-2-dev:i386
	fi
	qmake client/client.pro TARGET_ARCH=${arch/-/_} CONFIG+="$CONFIG"
	if [ "$arch" = "i686" ]; then
		# We had to use the native qmake to get the environment bootstrapped
		# Next we need to use the 32-bit qmake to get the right search paths
		/usr/lib/i386-linux-gnu/qt5/bin/qmake client/client.pro TARGET_ARCH=${arch/-/_} CONFIG+="$CONFIG"
	fi
	# Now we can finally compile the software
	make -j $(nproc)
	# After it's built, we want to clean up the keepassxc directory
	cd keepassxc && git clean -fd; cd ..
else
	echo "Platform not yet implemented"
fi
