#!/bin/sh

echo "TODO: Version von cmake prüfen und evtl. warning throwen"

echo "Install emscripten environment for porting google protobuf library to javascript."

mkdir -p emscripten
cd emscripten
wget -N https://s3.amazonaws.com/mozilla-games/emscripten/releases/emsdk-portable.tar.gz
tar -xvf emsdk-portable.tar.gz
cd emsdk_portable
./emsdk update
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
apt-get install clang-3.4
echo "Set symbolic link to clang++"
ln -s /usr/bin/clang++ clang/fastcomp/build_master_64/bin/clang++
echo "TODO: Test installation"
emcc -v
cd ../..

echo "Get Sources for installing google protobuf 2.6.0 on system."
echo "... and porting protobuf 2.6.0 to javascript using emscripten (and embind)"
mkdir -p google-protobuf
cd google-protobuf
wget -N https://protobuf.googlecode.com/svn/rc/protobuf-2.6.0.tar.gz
tar -xzf protobuf-2.6.0.tar.gz
echo "protobuf for C++"
cp protobuf-2.6.0 protobuf-2.6.0_emscripten -R
cd protobuf-2.6.0
sh ./autogen.sh 
./configure
make
sudo make install
cd ..

echo "protobuf for emscripten"
cd protobuf-2.6.0_emscripten
cp ../../customized_pb_headers/atomicops.h src/google/protobuf/stubs -f
cp ../../customized_pb_headers/platform_macros.h src/google/protobuf/stubs -f
cp ../../customized_pb_headers/atomicops_internals_emscripten.h src/google/protobuf/stubs -f
sh ./autogen.sh
emconfigure ./configure
emmake make

echo "copy protobuf headers to src folder"
cp google-protobuf/protobuf-2.6.0_emscripten/src/.libs/libprotobuf.so src -i
