#!/bin/bash
sudo chmod -R 777 /opt/mxe/usr/i686-w64-mingw32.shared/
if [ ! -d /home/wareck/bfgminer ]
then
cd /home/wareck
git clone https://github.com/wareck/bfgminer.git
fi
if [ ! -d /home/wareck/libraries ]
then
mkdir /home/wareck/libraries
else
rm -r /home/wareck/libraries
mkdir /home/wareck/libraries
fi
function libs {
cd /home/wareck/libraries
wget https://github.com/troydhanson/uthash/archive/master.zip -O uthash.zip
unzip uthash.zip
cp /home/wareck/libraries/uthash-master/src/* /opt/mxe/usr/i686-w64-mingw32.shared/include

cd /home/wareck/libraries
wget https://github.com/downloads/libevent/libevent/libevent-2.0.10-stable.tar.gz
tar xvfz libevent-2.0.10-stable.tar.gz

cd /home/wareck/libraries/libevent-2.0.10-stable
./autogen.sh
CFLAGS="-O2 -msse2" ./configure --host=i686-w64-mingw32.shared --enable-shared --disable-openssl --disable-debug-mode --prefix=/opt/mxe/usr/i686-w64-mingw32.shared/
make
make install

cd /home/wareck/libraries
wget -c https://github.com/libusb/hidapi/archive/refs/tags/hidapi-0.13.1.zip
unzip hidapi-0.13.1.zip
cd hidapi-hidapi-0.13.1
./bootstrap
CFLAGS="-O2 -msse2" ./configure --host=i686-w64-mingw32.shared --prefix=/opt/mxe/usr/i686-w64-mingw32.shared/
make
make install

cd /home/wareck/libraries
wget -c https://github.com/libusb/libusb/releases/download/v1.0.26/libusb-1.0.26.tar.bz2
tar xvfj libusb-1.0.26.tar.bz2
cd libusb-1.0.26
CFLAGS="-O2 -msse2" ./configure --host=i686-w64-mingw32.shared --disable-static --prefix=/opt/mxe/usr/i686-w64-mingw32.shared/
make
make install

cd /home/wareck/libraries
wget -c https://github.com/Karlson2k/libmicrohttpd/releases/download/v0.9.75/libmicrohttpd-0.9.75.tar.gz
tar xvfz libmicrohttpd-0.9.75.tar.gz
cd libmicrohttpd-0.9.75
CFLAGS="-O2 -msse2" ./configure --host=i686-w64-mingw32.shared --prefix=/opt/mxe/usr/i686-w64-mingw32.shared/
make
make install
}

cd /home/wareck/
git clone https://github.com/wareck/bfgminer.git
cd bfgminer
git submodule init
git submodule update

function build_ {
cd /home/wareck/bfgminer/
autoreconf -fi
CFLAGS="-O2 -msse2" ./configure --host=i686-w64-mingw32.shared --disable-static --enable-scrypt --enable-futurebit --disable-other-drivers
make
}

function copy_dll {
cd /home/wareck/bfgminer/
if [ ! -d /home/wareck/Bureau/win32_build/bfgminer/ ];then mkdir /home/wareck/Bureau/win32_build/bfgminer/;fi
cp bfgminer.exe /home/wareck/Bureau/win32_build/bfgminer/
cp /home/wareck/bfgminer/libbase58/.libs/libbase58-0.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /home/wareck/bfgminer/libblkmaker/.libs/libblkmaker-0.1-8.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /home/wareck/bfgminer/libblkmaker/.libs/libblkmaker_jansson-0.1-8.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libcurl-4.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libevent-2-0-5.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libjansson-4.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libmicrohttpd-12.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libncurses5.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libwinpthread-1.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libusb-1.0.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libidn2-0.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libssh2-1.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libgcc_s_sjlj-1.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libintl-8.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libunistring-2.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libgcrypt-20.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libiconv-2.dll /home/wareck/Bureau/win32_build/bfgminer/
cp /opt/mxe/usr/i686-w64-mingw32.shared/bin/libgpg-error-0.dll /home/wareck/Bureau/win32_build/bfgminer/
}

build_
copy_dll

