#!/usr/bin/env bash
set -euo pipefail

emsdk_env="$HOME/programs/emsdk/emsdk_env.sh"
modules="RAYLIB_MODULE_AUDIO=FALSE RAYLIB_MODULE_RAYGUI=FALSE RAYLIB_MODULE_MODELS=FALSE"
linux_library_dest="../../linux"
windows_library_dest="../../windows"
darwin_library_dest="../../darwin"
wasm_library_dest="../../wasm"

config_cflags="\
-DSUPPORT_MODULE_RSHAPES=1 \
-DSUPPORT_MODULE_RTEXTURES=1 \
-DSUPPORT_MODULE_RTEXT=1 \
-DSUPPORT_MODULE_RMODELS=0 \
-DSUPPORT_MODULE_RAUDIO=0 \
-DSUPPORT_TRACELOG=0 \
-DSUPPORT_CAMERA_SYSTEM=0 \
-DSUPPORT_GESTURES_SYSTEM=0 \
-DSUPPORT_RPRAND_GENERATOR=0 \
-DSUPPORT_MOUSE_GESTURES=0 \
-DSUPPORT_SSH_KEYBOARD_RPI=0 \
-DSUPPORT_WINMM_HIGHRES_TIMER=0 \
-DSUPPORT_BUSY_WAIT_LOOP=0 \
-DSUPPORT_PARTIALBUSY_WAIT_LOOP=0 \
-DSUPPORT_SCREEN_CAPTURE=0 \
-DSUPPORT_COMPRESSION_API=0 \
-DSUPPORT_AUTOMATION_EVENTS=0 \
-DSUPPORT_CUSTOM_FRAME_CONTROL=0 \
-DSUPPORT_CLIPBOARD_IMAGE=0 \
-DRLGL_ENABLE_OPENGL_DEBUG_CONTEXT=0 \
-DRLGL_SHOW_GL_DETAILS_INFO=0 \
-DSUPPORT_QUADS_DRAW_MODE=0 \
-DSUPPORT_FILEFORMAT_PNG=0 \
-DSUPPORT_FILEFORMAT_BMP=0 \
-DSUPPORT_FILEFORMAT_TGA=0 \
-DSUPPORT_FILEFORMAT_JPG=0 \
-DSUPPORT_FILEFORMAT_GIF=0 \
-DSUPPORT_FILEFORMAT_QOI=0 \
-DSUPPORT_FILEFORMAT_PEP=0 \
-DSUPPORT_FILEFORMAT_PSD=0 \
-DSUPPORT_FILEFORMAT_DDS=0 \
-DSUPPORT_FILEFORMAT_HDR=0 \
-DSUPPORT_FILEFORMAT_PIC=0 \
-DSUPPORT_FILEFORMAT_PNM=0 \
-DSUPPORT_FILEFORMAT_KTX=0 \
-DSUPPORT_FILEFORMAT_ASTC=0 \
-DSUPPORT_FILEFORMAT_PKM=0 \
-DSUPPORT_FILEFORMAT_PVR=0 \
-DSUPPORT_IMAGE_EXPORT=0 \
-DSUPPORT_IMAGE_GENERATION=0 \
-DSUPPORT_FILEFORMAT_TTF=0 \
-DSUPPORT_FILEFORMAT_FNT=0 \
-DSUPPORT_FILEFORMAT_BDF=0 \
-DSUPPORT_FILEFORMAT_OBJ=0 \
-DSUPPORT_FILEFORMAT_MTL=0 \
-DSUPPORT_FILEFORMAT_IQM=0 \
-DSUPPORT_FILEFORMAT_GLTF=0 \
-DSUPPORT_FILEFORMAT_VOX=0 \
-DSUPPORT_FILEFORMAT_M3D=0 \
-DSUPPORT_MESH_GENERATION=0 \
-DSUPPORT_GPU_SKINNING=0 \
-DSUPPORT_FILEFORMAT_WAV=0 \
-DSUPPORT_FILEFORMAT_OGG=0 \
-DSUPPORT_FILEFORMAT_MP3=0 \
-DSUPPORT_FILEFORMAT_QOA=0 \
-DSUPPORT_FILEFORMAT_FLAC=0 \
-DSUPPORT_FILEFORMAT_XM=0 \
-DSUPPORT_FILEFORMAT_MOD=0"

source $emsdk_env

pushd raylib/src
echo "Compiling raylib static library for linux"
make --silent $modules RAYLIB_CONFIG_FLAGS="$config_cflags" CUSTOM_CFLAGS="-Os -ffunction-sections -fdata-sections" PLATFORM_OS=LINUX
mv libraylib.a $linux_library_dest
make --silent clean

# echo "Compiling static library for windows"
# make --silent $modules RAYLIB_CONFIG_FLAGS="$config_cflags" CC=x86_64-w64-mingw32-gcc PLATFORM_OS=WINDOWS
# mv libraylib.a $windows_library_dest
# make --silent clean

echo "Compiling static library for web"
make --silent $modules RAYLIB_CONFIG_FLAGS="$config_cflags" PLATFORM=PLATFORM_WEB
mv libraylib.web.a libraylib.a
mv libraylib.a $wasm_library_dest
make --silent clean
popd

pushd raygui
echo "Compiling raygui static library for linux"
gcc -c -Os -ffunction-sections -fdata-sections -fPIC -x c -DRAYGUI_IMPLEMENTATION -I../raylib/src raygui.h -o raygui.o
ar rcs ../linux/libraygui.a raygui.o
rm raygui.o

echo "Compiling raygui static library for web"
emcc -c -Os -ffunction-sections -fdata-sections -x c -DRAYGUI_IMPLEMENTATION -DPLATFORM_WEB -I../raylib/src raygui.h -o raygui_wasm.o
emar rcs ../wasm/libraygui.a raygui_wasm.o
rm raygui_wasm.o
popd
