#!/bin/bash
set -euo pipefail

readonly scriptDir=$(dirname $0)
source $scriptDir/gamename.sh

readonly tmpDir=/tmp/deliver-$$
trap "rm -rf $tmpDir" EXIT
mkdir -p $tmpDir

function main
{
  echo "Delivering [$NAME]"
  #make clean
  #./check

  echo "-------------------------------------"
  echo "Building resources"
  rm -rf res
  make -j`nproc` resources >/dev/null

  (deliverBinaryVersion)
  (deliverWebVersion)
  echo OK
}

function deliverBinaryVersion
{
  local N=$NAME
  local version="$(date +"%Y%m%d")_$(git rev-parse HEAD)"

  mkdir $tmpDir/$N

  echo "Version: $version"

  echo "-------------------------------------"
  echo "Building binaries"

  # Windows binaries
  BIN_WIN=$tmpDir/bin/w64
  CXXFLAGS="-O3" \
  BIN=$BIN_WIN \
    ./scripts/w64-make -j`nproc` \
    VERSION="$version" \
    $BIN_WIN/rel/game.exe \
    >/dev/null

  gccVersion=$(x86_64-w64-mingw32-g++ -dumpversion)

  cp $BIN_WIN/rel/game.exe                                           $tmpDir/$N/$N.exe
  cp /usr/x86_64-w64-mingw32/lib/libwinpthread-1.dll                 $tmpDir/$N
  cp /usr/lib/gcc/x86_64-w64-mingw32/$gccVersion/libstdc++-6.dll     $tmpDir/$N
  cp /usr/lib/gcc/x86_64-w64-mingw32/$gccVersion/libgcc_s_seh-1.dll  $tmpDir/$N
  cp /opt/envs/x86_64-w64-mingw32/bin/SDL2.dll                       $tmpDir/$N

  # GNU/Linux binaries
  export PATH="/opt/toolchains/x86_64-ace-linux-gnu/bin:$PATH"
  CROSS_COMPILE=x86_64-ace-linux-gnu- \
  PKG_CONFIG_LIBDIR="/opt/envs/x86_64-ace-linux-gnu/lib/pkgconfig" \
  CXXFLAGS="-include extra/glibc_version.h -O3" \
  BIN=$tmpDir/bin/gnu \
    make -j`nproc` \
    VERSION="$version" \
    >/dev/null

  echo -n "Game required GLIBC: "
  nm -D $tmpDir/bin/gnu/rel/game.exe | grep GLIBC_ | sed 's/.*GLIBC_//g' | sort -Vru | head -n 1

  cp -a $tmpDir/bin/gnu/rel/game.exe                                 $tmpDir/$N/$N.x86_64

  # Strip executables
  strip -s $tmpDir/$N/*.{exe,dll,x86_64}

  # Compress executables
  upx -9 $tmpDir/$N/*.{exe,dll,x86_64}

  echo "$version" > "$tmpDir/$N/version.txt"

  # Copy data
  cp -a res                                                        $tmpDir/$N/res

  (
    cd $tmpDir
    zip -q $N.zip -r $N
  )

  cp $tmpDir/$N.zip /tmp/
  du -hs /tmp/$N.zip
}

function deliverWebVersion
{
  echo "-------------------------------------"
  echo "Building web version"

  if ! which emcc >/dev/null ; then
    echo "emcc was not found in PATH" >&2
    echo "Did you forget to: source ~/source/emsdk/emsdk_env.sh" ? >&2
    return 1
  fi

  mkdir $tmpDir/pkgconfig

  genPkgConfigSdl2 > $tmpDir/pkgconfig/sdl2.pc

  export BIN=$tmpDir/bin/web
  export PKG_CONFIG_LIBDIR=$tmpDir/pkgconfig
  export CXX=emcc
  export EXT=".html"
  export DBGFLAGS=""
  export CXXFLAGS="-O3 -g0 -DNDEBUG"
  export LDFLAGS=" -O3 -g0 --use-preload-plugins --pre-js \"my-pre.js\" --preload-file res -s USE_WEBGL2=1 -s TOTAL_MEMORY=$((64 * 1024 * 1024)) -s PRECISE_F32=1"

  make -j`nproc` game >/dev/null

  rm -f /tmp/$NAME-web.zip
  (cd $tmpDir/bin/web/rel && zip /tmp/$NAME-web.zip -r . )

  du -hs /tmp/$NAME-web.zip
}

function genPkgConfigSdl2
{
  echo "Name: sdl2"
  echo "Version: 2.0.0"
  echo "Description: placeholder pc file"
  echo "Libs: -s USE_SDL=2"
  echo "Cflags:  -s USE_SDL=2"
}

main
