#!/bin/bash

mkdir -p build
cd build
mkdir -p X-Plane/GAConnector/64
mkdir -p X-Plane/GAConnector/recordings
mkdir -p ESP/GAConnector
mkdir -p ESP/GAConnector/recordings
rm -f CMakeCache.txt

case $1 in
  "mac")
    cmake -DDEBUG=$DEBUG -DCMAKE_TOOLCHAIN_FILE=../toolchain-mac.cmake ..
  ;;
  "lin")
    cmake -DDEBUG=$DEBUG -DBIT=64 -DCMAKE_TOOLCHAIN_FILE=../toolchain-lin.cmake ..
  ;;
  "win32")
    cmake -DDEBUG=$DEBUG -DBIT=32 -DCMAKE_TOOLCHAIN_FILE=../toolchain-win-32.cmake ..
    \cp -rf ../openSSL/win32/*.dll ESP/GAConnector/
    \cp -rf /opt/llvm-mingw/i686-w64-mingw32/bin/libc++.dll ESP/GAConnector/
    \cp -rf /opt/llvm-mingw/i686-w64-mingw32/bin/libunwind.dll ESP/GAConnector/
  ;;
  "win64")
    cmake -DDEBUG=$DEBUG -DBIT=64 -DCMAKE_TOOLCHAIN_FILE=../toolchain-win-64.cmake ..
    \cp -rf ../openSSL/win64/*.dll X-Plane/GAConnector/64/
    \cp -rf /opt/llvm-mingw/x86_64-w64-mingw32/bin/libc++.dll X-Plane/GAConnector/64
    \cp -rf /opt/llvm-mingw/x86_64-w64-mingw32/bin/libunwind.dll X-Plane/GAConnector/64
  ;;
  *)
    echo "Unknown platform, choose one of mac, lin, win64, win32"
    exit 22
  ;;
esac

make -j
code=$?
if [ "$code" -ne 0 ]
then
    exit $code
fi

if [ "$1" = "mac" ] && [ "$DEBUG" = "1" ]
then
    echo "Building dSYM"
    /opt/osxcross/target/bin/osxcross-llvm-dsymutil X-Plane/GAConnector/mac.xpl
    /opt/osxcross/target/bin/osxcross-llvm-dsymutil X-Plane/GAConnector/libixwebsocket.dylib
fi


