#!/bin/bash

set -e

GCC_LIBS="stdc++-6 gcc_s_sjlj-1"
DEP_LIBS="ogg-0 vorbis-0 vorbisfile-3 FLAC-8 readline6"
DEP_LIBS_NOPREFIX="iup"
ALLEGRO_LIBS="allegro allegro_acodec allegro_audio allegro_dialog \
allegro_font allegro_image allegro_primitives allegro_color"

HOST=i686-w64-mingw32
BUILD=x86_64-linux-gnu
PREFIX=/usr/i686-w64-mingw32

# build MININIM
if [ -f Makefile ]; then make clean; fi
mv build-aux/texinfo.tex{,.bkp}
autoreconf --install --force
mv build-aux/texinfo.tex{.bkp,}
if [ "$1" != "--enable-debug" ]
then ./configure --host=$HOST --build=$BUILD --prefix=$PREFIX
else ./configure --host=$HOST --build=$BUILD --prefix=$PREFIX --enable-debug
fi
make -j4

# build documentation
cd doc
make mininim.pdf
cd ..

# determine version and release directory
VERSION=$(grep -Po '#define VERSION "\K.*?(?=")' config.h)-win32
if [ "$1" = "--enable-debug" ]; then VERSION=$VERSION-debug; fi
RELEASE_DIR=release/mininim-$VERSION

# create release directory
rm -rf $RELEASE_DIR
mkdir -p $RELEASE_DIR

# copy MININIM executable, data and documentation
cp -r data mininim.exe doc/mininim.pdf $RELEASE_DIR
find $RELEASE_DIR -name '*~' -delete

# copy libs
for f in $GCC_LIBS
do cp -v /usr/lib/gcc/$HOST/4.9-win32/lib$f.dll $RELEASE_DIR
done

for f in $DEP_LIBS
do cp -v /usr/$HOST/bin/lib$f.dll $RELEASE_DIR
done

for f in $DEP_LIBS_NOPREFIX
do cp -v /usr/$HOST/bin/$f.dll $RELEASE_DIR
done

for f in $ALLEGRO_LIBS
do
    # if [ "$1" != "--enable-debug" ]
    # then cp -v /usr/$HOST/bin/$f-5.2.dll $RELEASE_DIR
    # else cp -v /usr/$HOST/bin/$f-debug-5.2.dll $RELEASE_DIR
    # fi
# cp -v /usr/$HOST/bin/$f-5.2.dll $RELEASE_DIR
cp -v /usr/$HOST/bin/$f-debug-5.2.dll $RELEASE_DIR
done

# copy Allegro configuration
unix2dos -n allegro5.cfg $RELEASE_DIR/allegro5.cfg

# convert Lua scripts line ending
for f in $(find data -iname '*.lua')
do unix2dos -n $f $RELEASE_DIR/$f
done

# convert shaders line ending
for f in $(find data -iname '*.frag' -or -iname '*.vert')
do unix2dos -n $f $RELEASE_DIR/$f
done

# strip them all
if [ "$1" != "--enable-debug" ]
then $HOST-strip $RELEASE_DIR/{*.dll,*.exe}
fi

# # create package
# cd release
# rm -rf ${RELEASE_DIR#release/}.zip
# zip -9r ${RELEASE_DIR#release/} ${RELEASE_DIR#release/}
# cd ..

# # remove temporary release directory
# rm -rf $RELEASE_DIR
