#!/bin/bash

set -e

# just in case...
unset CC
unset CXX

#PREFIX=i686-w64-mingw32
PREFIX=x86_64-w64-mingw32

export PATH="/usr/$PREFIX/bin:$PATH"
export INSTALLDIR="$PWD/$PREFIX"
export PKG_CONFIG_PATH=$INSTALLDIR/lib/pkgconfig:/usr/$PREFIX/lib/pkgconfig
export PKG_CONFIG_LIBDIR="$INSTALLDIR/lib/pkgconfig"
export PKG_CONFIG="/usr/bin/${PREFIX}-pkg-config"
export CPPFLAGS="-I$INSTALLDIR/include"
export LDFLAGS="-L$INSTALLDIR/lib"

# somehow, the GnuTLS build needs a static gcc library. It comes with MinGW, but we have to find it's directory.
GCCLIB=$(dirname $(find /usr/lib/gcc/$PREFIX -name libgcc_s_seh-1.dll|head -1))

# without WINEPATH our local DLLs won't be find when running tests
export WINEPATH="$INSTALLDIR/bin;$INSTALLDIR/lib;/usr/$PREFIX/bin;/usr/$PREFIX/lib;$PWD/libwget/.libs;$GCCLIB"

# let mingw compiler be less verbose
export CFLAGS="-O2 -Wall -Wno-format"

if [ ! -d libunistring ]; then
  git clone https://git.savannah.gnu.org/git/libunistring.git
  cd libunistring
else
  cd libunistring
  git pull
fi
./gitsub.sh pull
./autogen.sh
./configure --build=x86_64-pc-linux-gnu --host=$PREFIX --enable-shared --prefix=$INSTALLDIR
make clean
make -j$(nproc)
make install
cd ..

if [ ! -d libidn2 ]; then
  git clone https://gitlab.com/libidn/libidn2.git
  cd libidn2
else
  cd libidn2
  git pull
fi
./bootstrap
./configure --build=x86_64-pc-linux-gnu --host=$PREFIX --enable-shared --disable-doc --disable-gcc-warnings --prefix=$INSTALLDIR
make clean
make -j$(nproc)
make install
cd ..

if [ ! -d libpsl ]; then
  git clone --recursive https://github.com/rockdaboot/libpsl.git
  cd libpsl
else
  cd libpsl
  git pull
fi
./autogen.sh
./configure --build=x86_64-pc-linux-gnu --host=$PREFIX --enable-shared --disable-gtk-doc --enable-runtime=libidn2 --enable-builtin=libidn2 --prefix=$INSTALLDIR
make clean
make -j$(nproc)
make install
cd ..

if [ ! -d nettle ]; then
  git clone https://git.lysator.liu.se/nettle/nettle.git
  cd nettle
else
  cd nettle
  git pull
fi
bash .bootstrap
./configure --host=$PREFIX --enable-mini-gmp --enable-shared --disable-documentation --prefix=$INSTALLDIR
make clean
make -j$(nproc)
make install
cd ..

if [ ! -d gnutls ]; then
  git clone https://gitlab.com/gnutls/gnutls.git
  cd gnutls
else
  cd gnutls
  git pull
fi
./bootstrap
./configure --build=x86_64-pc-linux-gnu --host=$PREFIX --prefix=$INSTALLDIR \
  --with-nettle-mini --enable-shared --with-included-libtasn1 \
  --with-included-unistring --without-p11-kit --disable-doc --disable-tests --disable-full-test-suite --disable-tools \
  --disable-cxx --disable-maintainer-mode --disable-libdane --disable-hardware-acceleration --disable-guile

make -j$(nproc)
make install
cd ..

# Install dlfcn-win32
if [ ! -d dlfcn-win32 ]; then
  git clone --recursive https://github.com/dlfcn-win32/dlfcn-win32.git
  cd dlfcn-win32
else
  cd dlfcn-win32
  git pull
fi
./configure --prefix=$PREFIX --cc=$PREFIX-gcc
make
cp -p libdl.a ../$PREFIX/lib/
cp -p src/dlfcn.h ../$PREFIX/include/
cd ..

# Install Libmicrohttpd from source
if [ ! -d libmicrohttpd ]; then
  git clone --recursive https://git.gnunet.org/libmicrohttpd.git
  cd libmicrohttpd
else
  cd libmicrohttpd
  git pull
fi
./bootstrap
./configure --build=x86_64-pc-linux-gnu --host=$PREFIX --prefix=$INSTALLDIR --disable-doc --disable-examples --enable-shared
make clean
make -j$(nproc)
make install
cd ..

# Build Wget2 and run checks
./bootstrap
./configure --build=x86_64-pc-linux-gnu --host=$PREFIX --enable-shared --without-gpgme
make clean
make -j
make check -j LOG_COMPILER=wine
