#!/bin/bash
set -e
export JOBS=8
cd ${HOME}
mkdir src
cd src
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
cd ..
git clone --recursive https://github.com/claudeha/libpd.git
cd libpd
git checkout emscripten
cd pure-data
git remote add claudeha https://github.com/claudeha/pure-data.git
git fetch claudeha
git checkout claudeha/emscripten
cd ..
mkdir build
cd build
emcmake cmake .. -DPD_UTILS:BOOL=OFF -DCMAKE_BUILD_TYPE=Release
emmake make -j ${JOBS}
cd ../samples/emscripten/pdtest
emmake make
cd ../pdtest_gui/
emmake make
cd ${HOME}/src
git clone https://github.com/claudeha/glu.git
cd glu
git checkout emscripten
./autogen.sh
GL_CFLAGS="-s USE_REGAL=1 -DAPIENTRY=" GL_LIBS="-s USE_REGAL=1" emconfigure ./configure --enable-static --disable-shared
GL_CFLAGS="-s USE_REGAL=1 -DAPIENTRY=" GL_LIBS="-s USE_REGAL=1" emmake make -j ${JOBS}
cd ..
git clone https://github.com/claudeha/Gem.git
cd Gem
git checkout emscripten
./autogen.sh
GL_CFLAGS="-s USE_REGAL=1" GL_LIBS="-s USE_REGAL=1" GLU_CFLAGS="-I$(pwd)/../glu/include" GLU_LIBS="$(pwd)/../glu/.libs/libGLU.a" emconfigure ./configure --enable-static --disable-shared --with-pd="$(pwd)/../libpd/pure-data" --disable-multicontext
GL_CFLAGS="-s USE_REGAL=1" GL_LIBS="-s USE_REGAL=1" GLU_CFLAGS="-I$(pwd)/../glu/include" GLU_LIBS="$(pwd)/../glu/.libs/libGLU.a" emmake make -k -j ${JOBS} || true
cd src
rm Gem.a
emar rs Gem.a Gem_la-Version.o `find -iname "lib*.o"`
cd ../../libpd/samples/emscripten/gemtest
emmake make
cd ..
python -m SimpleHTTPServer 8080 &
