#!/bin/bash

FFTW=fftw-3.3.9
if [ ! -d $FFTW ]
then
	wget "http://fftw.org/$FFTW.tar.gz"
	tar xvf "$FFTW.tar.gz"
	rm "$FFTW.tar.gz"
fi
cd $FFTW
./configure \
                 F77=gfortran \
                 --with-gcc \
                 --prefix=/usr \
                 --infodir=/usr/share/info \
                 --enable-type-prefix \
                 --enable-shared \
                 --enable-threads \
                 --enable-openmp \
                 --enable-mpi
make -j8
sudo make install
cd ..

set -e

qmake APPIMAGE=1 CONFIG+=release PREFIX=/usr DEFINES+=GIT_HASH_SHORT=$(git rev-parse --short HEAD)
make -j$(nproc) CXX=${CXX} CC=${CC} LINK=${CXX}
make INSTALL_ROOT=appdir install ; find appdir/
wget -c -nv "https://github.com/probonopd/linuxdeployqt/releases/download/6/linuxdeployqt-6-x86_64.AppImage"
chmod a+x linuxdeployqt-*.AppImage
unset QTDIR; unset QT_PLUGIN_PATH ; unset LD_LIBRARY_PATH
export VERSION=$(git rev-parse --short HEAD)
./linuxdeployqt-*.AppImage appdir/usr/share/applications/*.desktop -bundle-non-qt-libs -verbose=2 -unsupported-allow-new-glibc
mkdir -p appdir/usr/optional/ ; wget -c https://github.com/darealshinji/AppImageKit-checkrt/releases/download/continuous/exec-x86_64.so -O ./appdir/usr/optional/exec.so
mkdir -p appdir/usr/optional/libstdc++/ ; cp /usr/lib/libstdc++.so.6 ./appdir/usr/optional/libstdc++/
./linuxdeployqt-*.AppImage --appimage-extract
rm ./appdir/AppRun ; cp ./resources/AppRun appdir/ ; chmod a+x ./appdir/AppRun
PATH=./squashfs-root/usr/bin:$PATH ./squashfs-root/usr/bin/appimagetool -g ./appdir/
