#!/bin/bash

## Linbo GUI build generic srcipt
## Copyright (C) 2020  Dorian Zedler <dorian@itsblue.de>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU Affero General Public License as published
## by the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Affero General Public License for more details.
##
## You should have received a copy of the GNU Affero General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
## This script will be modified by the build.sh script
## so it can be used for both, 64 and 32 bit building!
## all lines starting with
## "## 64: " will just be used for 64 bit building
## and all lines starting with
## "## 32: " will just be used for 32 bit building

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
cd $DIR

QT_ARCHIVE=../qt-everywhere-src-5.15.2.tar.xz
QT_DIR=./qt-everywhere-src-5.15.2

DO_CLEAN=1
DO_WAIT=1

while test $# -gt 0
do
    case "$1" in
        --noclean) DO_CLEAN=0
            ;;
        --nowait) DO_WAIT=0
            ;;
    esac
    shift
done

# Make sure, we have exactly one architecure defined
ARCH_VALID=0
## 64: ARCH_VALID=$(($ARCH_VALID + 1))
## 32: ARCH_VALID=$(($ARCH_VALID + 1))

if [[ $ARCH_VALID -ne 1 ]]; then
   echo "ERROR: There was no or more than one valid architecture defined for this buildscript!"
   echo "Note: this script is not designed to be used standalone, don't execute it!"
   exit 1
fi

sudo apt update

## 64: sudo apt install build-essential gcc g++ libudev-dev libinput-dev libxkbcommon-dev  libmtdev-dev libwacom-dev -y

## 32: sudo dpkg --add-architecture i386
## 32: sudo apt update
## 32: sudo apt install gcc-multilib g++-multilib libudev-dev:i386 libinput-dev:i386 libxkbcommon-dev:i386 -y

# Check for errors in apt install
if [[ $? -ne 0 ]]; then
   echo "There was an error when running apt install!"
   exit 1
fi

# export pkgconfig path for 32 bit libraries
## 32: export PKG_CONFIG_PATH=/usr/lib/i386-linux-gnu/pkgconfig

if test ! -f "$QT_ARCHIVE"
then
    echo "Qt archive is not present, downloading it now ..."
    wget -O $QT_ARCHIVE "https://download.qt.io/archive/qt/5.15/5.15.2/single/qt-everywhere-src-5.15.2.tar.xz"
fi

if [ ! -d "Qt5" ]
then
	echo "Qt archive is still packed, extracting it now ..."
	rm -rf $QT_DIR
	mkdir $QT_DIR
	echo "This may take a long time..."
	tar -xf $QT_ARCHIVE -C ./

	cd $QT_DIR

	# set default platform plugin
	export QT_QPA_PLATFORM=linuxfb

	# configure Qt
	./configure \
	 -prefix ../Qt5 \
## 32:  -platform linux-g++-32 \
	 -opensource \
	 -confirm-license \
	 -release \
	 -static \
	 -verbose \
	 -qt-zlib \
	 -qt-libpng \
	 -libudev \
	 -libinput \
	 -nomake examples \
	 -no-gif \
	 -no-libjpeg \
	 -no-openssl \
	 -no-iconv \
	 -no-dbus \
	 -no-opengl \
	 -no-glib \
	 -no-cups \
	 -no-accessibility \
	 -no-libproxy \
	 -no-sql-sqlite \
	 -no-feature-textodfwriter \
	 -no-feature-calendarwidget \
	 -no-feature-printpreviewwidget \
	 -no-feature-colordialog \
	 -no-feature-filedialog \
	 -no-feature-fontdialog \
	 -no-feature-printpreviewdialog \
	 -no-feature-progressdialog \
	 -no-feature-wizard \
	 -no-feature-ftp \
	 -no-feature-udpsocket \
	 -no-feature-networkproxy \
	 -no-feature-socks5 \
	 -no-feature-networkdiskcache \
	 -no-feature-bearermanagement \
	 -skip qt3d \
	 -skip qtandroidextras \
	 -skip qtcanvas3d \
	 -skip qtcharts \
	 -skip qtconnectivity \
	 -skip qtdatavis3d \
	 -skip qtdoc \
	 -skip qtgamepad \
	 -skip qtnetworkauth \
	 -skip qtpurchasing \
	 -skip qtwayland \
	 -skip qtlocation \
	 -skip qtscript \
	 -skip qtdeclarative \
	 -skip qtlocation \
	 -skip qtmultimedia \
	 -skip qtquickcontrols \
	 -skip qtquickcontrols2 \
	 -skip qtsensors \
	 -skip qtwebsockets \
	 -skip qtwinextras \
	 -skip qtwebchannel \
	 -skip qtwebengine \
	 -skip qttools \
	 -skip qtserialbus \
	 -skip qtserialport \
	 -skip qtspeech \
	 -skip qtx11extras

	# Check for errors in configure
	if [[ $? -ne 0 ]]; then
	   echo "There was an error when configuring Qt (see above)!"
	   exit 1
	fi

	make -j8
	# Check for errors in make
	if [[ $? -ne 0 ]]; then
	   echo "There was an error when building Qt (see above)!"
	   exit 1
	fi

	make install
	# Check for errors in make install
	if [[ $? -ne 0 ]]; then
	   echo "There was an error when installing Qt (see above)!"
	   exit 1
	fi

	echo "---------------------------------------"
	echo "- Qt has been installed successfully! -"
	echo "---------------------------------------"

	cd ..
else
    echo "Qt install dir exists, assuming Qt has already been built"

	if [[ $DO_WAIT -ne 0 ]]; then
		echo "continuing with building in 5 seconds"
		sleep 5
	fi
fi

echo "-------------------------------"
echo "- Starting to build linbo_gui -"
echo "-------------------------------"

if [[ $DO_CLEAN -ne 0 ]]; then
	rm -rf ./build
	rm -rf ./linbofs
fi

mkdir build
cd build

../Qt5/bin/qmake ../../../linboGUI.pro

make -j8

# Check for errors in make
if [[ $? -ne 0 ]]; then
   echo "There was an error when building!"
   exit 1
fi

strip linbo_gui
chmod +x linbo_gui

# Deploy all files to the linbofs folder
cd ..

# main binary
mkdir -p ./linbofs/usr/bin/
cp ./build/linbo_gui ./linbofs/usr/bin/

# shared libraries
mkdir -p ./linbofs/lib/
# Get all dependencies | split at path separator | remove everything but path | copy the libs to linbofs/lib
ldd ./build/linbo_gui | grep "=> /" | awk '{print $3}' | xargs -I '{}' cp -v '{}' ./linbofs/lib/

# Keyboard layouts
mkdir -p ./linbofs/usr/share/X11/
cp -r /usr/share/X11/xkb/ ./linbofs/usr/share/X11/

exit 0
