#!/bin/bash

# Based on http://qt-project.org/wiki/Build_Qt5_mysql_plugin_for_Android

export ANDROID_NDK_ROOT="$HOME/.Qt/android-ndk-r10e/"
export QT_ROOT="$HOME/.Qt/5.5/"

SR="$ANDROID_NDK_ROOT/platforms/android-17/arch-arm/"
BR="$ANDROID_NDK_ROOT/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86_64/bin/arm-linux-androideabi-"

here="$(dirname "$0")"
find "$SR" > "$here/rootfs_files_before.txt" || exit 1
checkChanges() {
        find "$SR" > "$here/rootfs_files_after.txt" || exit 1
        echo "Changes:"
        diff -u "$here/rootfs_files_before.txt" "$here/rootfs_files_after.txt"
}
trap checkChanges EXIT

# OpenSSL
#pkg=openssl-1.0.1f.tar.gz
#pkg=openssl-1.0.1e.tar.gz
pkg=openssl-1.0.2d.tar.gz
dir=$(basename $pkg .tar.gz)

if [ ! -d $dir ]; then
        wget -c http://www.openssl.org/source/$pkg
        tar -xf $pkg || exit 1
fi

pushd $dir
        RANLIB="$BR"ranlib CC="$BR"gcc ./Configure android-armv7 --prefix=$SR/usr
        ANDROID_DEV=$SR/usr make #|| exit 1
        ANDROID_DEV=$SR/usr make || exit 1
        make install_sw || exit 1
popd


# libiconv
pkg=libiconv-1.14.tar.gz
dir=$(basename $pkg .tar.gz)

if [ ! -d $dir ]; then
        wget -c http://ftp.gnu.org/pub/gnu/libiconv/$pkg
        tar -xf $pkg || exit 1
fi

pushd $dir
        STRIP="$BR"strip RANLIB="$BR"ranlib OBJDUMP="$BR"objdump AR="$BR"ar CC="$BR"gcc CFLAGS=--sysroot=$SR CPP="$BR"cpp CPPFLAGS=$CFLAGS ./configure --host=arm --prefix=$SR/usr --with-sysroot=$SR
        make || exit 1
        make install || exit 1
popd


# mariadbclient

version=2.0.0
pkg=mariadb_client-$version-src.tar.gz
url=https://downloads.mariadb.org/f/client-native-$version/src/$pkg

# FIXME: This one fails to build, linker errors
#version=2.1.0
#pkg=mariadb-connector-c-$version-src.tar.gz
#url=https://downloads.mariadb.org/f/connector-c-$version/source-tgz/$pkg

dir=$(basename $pkg .tar.gz)
if [ ! -d $dir ]; then
        wget -c $url
        tar -xf $pkg || exit 1
fi

pushd $dir
        sed -i -e "s|ADD_SUBDIRECTORY(unittest/libmariadb)|#ADD_SUBDIRECTORY(unittest/libmariadb)|" CMakeLists.txt
        sed -i -e "N; s|typedef unsigned short ushort;\n#endif|#endif\ntypedef unsigned short ushort;|" include/my_global.h
        sed -i -e "N; s|SET_TARGET_PROPERTIES(libmariadb PROPERTIES VERSION.*||" libmariadb/CMakeLists.txt
        sed -i -e "N; s|SOVERSION \${CPACK_PACKAGE_VERSION_MAJOR})||" libmariadb/CMakeLists.txt # Pig disgusting: the previous multiline thingie should've done it
        sed -i -e "N; s|\${CPACK_PACKAGE_VERSION_MAJOR}||" libmariadb/CMakeLists.txt # Pig disgusting: what the fuck
        mkdir build
        pushd build
        PKG_CONFIG_PATH=$SR/usr/lib/pkgconfig cmake \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_C_FLAGS=--sysroot="$SR" \
		-DCMAKE_INSTALL_PREFIX="$SR/usr" \
		-DCMAKE_C_COMPILER="$BR"gcc \
		-DCMAKE_LINKER="$BR"ld \
		-DCMAKE_AR="$BR"ar \
		-DCMAKE_NM="$BR"nm \
		-DCMAKE_OBJCOPY="$BR"objcopy \
		-DCMAKE_OBJDUMP="$BR"objdump \
		-DCMAKE_RANLIB="$BR"ranlib \
		-DCMAKE_STRIP="$BR"strip \
		-DICONV_INCLUDE_DIR="$SR/usr/include" \
		-DICONV_LIBRARIES="$SR/usr/lib/libiconv.a" \
		-DWITH_EXTERNAL_ZLIB=ON \
		-DZLIB_INCLUDE_DIR="$SR/usr/include" \
		-DZLIB_LIBRARY="$SR/usr/lib/libz.so" ../ || exit 1
        make install || exit 1
        popd

        # ???
        pwd
        cp build/libmariadb/*.{a,so} "$SR/usr/lib/mariadb/" || exit 1
popd


# qt
qmake="$QT_ROOT/android_armv7/bin/qmake"
[ ! -f "$qmake" ] && { echo "Could not find qmake in '$qmake'"; exit 1; }
[ ! -x "$qmake" ] && { echo "Qmake is not executable in '$qmake'"; exit 1; }
qtVersion=$(qmake -query QT_VERSION)

#pkg=qt-everywhere-opensource-src-$qtVersion.tar.gz
#pkg=qtbase-opensource-src-$qtVersion.tar.gz
pkg=qtbase-opensource-src-5.5.1.tar.gz
dir=$(basename $pkg .tar.gz)

if [ ! -d $dir ]; then
#        wget -c http://download.qt.io/official_releases/qt/${qtVersion%.*}/$qtVersion/submodules/$pkg
#        wget -c http://download.qt.io/official_releases/qt/${qtVersion%.*}/$qtVersion/$pkg
        wget -c http://download.qt.io/official_releases/qt/5.5/5.5.1/submodules/qtbase-opensource-src-5.5.1.tar.gz
        tar -xf $pkg || exit 1
fi

pushd $dir/src/plugins/sqldrivers/mysql/
        $qmake "INCLUDEPATH+=$SR/usr/include/mariadb" "LIBS+=$SR/usr/lib/mariadb/libmariadbclient.a $SR/usr/lib/libssl.a $SR/usr/lib/libcrypto.a $SR/usr/lib/libiconv.a" "LIBPATH+=$SR/usr/lib/mariadb" -o Makefile mysql.pro
        make || exit 1
        make install || exit 1
popd


echo
echo "BOOYAH!!!"
echo
