#! /bin/bash

# This is my personal script. Things might not make sense for you; but they do for me. :)
#
# And this is the script that the Godot developers use:
#   https://github.com/okamstudio/godot/wiki/compiling_batch_templates
# Detailed build information:
#   https://github.com/okamstudio/godot/wiki/advanced
#
# TO DO (by you, the reader):
# 1. Change path_godot and path_emsdk to the source locations.
# 2. Make sure ANDROID_NDK_ROOT is set properly.
# 3. Read the bottom of this file to find out how to „install“.
# 4. To make it work with Portage, follow http://wiki.gentoo.org/wiki/SCons
#    and maybe patch $path_godot/SConstruct as seen here: http://dev.gentoo.org/~vapier/scons-blows.txt
#    (I don’t think SCons blows that much. It’s just more free and flexible,
#     for cases where that is required. Like for Godot. IMHO)

update_emsdk=false

path_godot="$SRC_DIR/godot"
path_emsdk="$SRC_DIR/emsdk_portable"

if [[ "$PWD" != "$path_godot" ]]; then
  cd "$path_godot"
fi

. /etc/portage/make.conf # This should obviously not be like this when using portage.

if [[ "$1" != "--templates"   ]]; then
  git pull
  scons            platform=x11     bits=64 tools=yes target=release_debug $MAKEOPTS # editor
fi

debug=false; release=false
if [[ "$1" == "--debug"   ]]; then shift; debug=true;               fi
if [[ "$1" == "--release" ]]; then shift;             release=true; fi
if [[ "$1" == "--all"     ]]; then shift; debug=true; release=true; fi

if [[ "$release" == "true" || "$debug" == "true" ]]; then shift

  # Linux
  $debug ||   scons platform=x11     bits=64 tools=no  target=release       $MAKEOPTS # template: Linux
  $debug &&   scons platform=x11     bits=64 tools=no  target=debug         $MAKEOPTS # template: Linux, debug

  # Android
  mkdir -p "platform/android/java/libs/armeabi"
  $debug || { scons platform=android bits=32 tools=no  target=release       $MAKEOPTS # template: Android, 32-bit
    cp -f "bin/libgodot.android.opt.32.so" "platform/android/java/libs/armeabi/libgodot_android.so"
    ant -s platform/android/java/build.xml release
  }
  $debug && { scons platform=android bits=32 tools=no  target=debug         $MAKEOPTS # template: Android, 32-bit, debug
    cp -f "bin/libgodot.android.debug.32.so" "platform/android/java/libs/armeabi/libgodot_android.so"
    ant -s platform/android/java/build.xml debug
  }

  # JavaScript
  source "$path_emsdk/emsdk_env.sh"
  $update_emsdk && { pushd "$path_emsdk"/
    ./emsdk update
    ./emsdk install  latest
    ./emsdk activate latest
    source ./emsdk_env.sh
  popd; }
  $debug || { scons platform=javascript                target=release       $MAKEOPTS # template HTML5
    zip --junk-paths bin/javascript_release.zip bin/godot.javascript.opt.html   bin/godot.javascript.opt.js   tools/html_fs/filesystem.js
  }
  $debug && { scons platform=javascript                target=debug         $MAKEOPTS # template HTML5, debug
    zip --junk-paths bin/javascript_debug.zip   bin/godot.javascript.debug.html bin/godot.javascript.debug.js tools/html_fs/filesystem.js
  }

fi

# Now I softlink from my bin/godot and my .godot/templates/* to the files in $path_godot/bin/ like this:
#
# user@workstation ~ $ ll .godot/templates/
# total 512
# drwxr-xr-x 2 user user 656 2014-12-19 14:29 ./
# drwxr-xr-x 6 user user 200 2014-12-21 03:41 ../
# lrwxrwxrwx 1 user user  51 2014-12-19 08:55 GodotiOSXCode.zip -> $godot/templates.bin/GodotiOSXCode.zip
# lrwxrwxrwx 1 user user  61 2014-12-19 08:57 android_debug.apk -> $godot/platform/android/java/bin/Godot-debug.apk
# lrwxrwxrwx 1 user user  72 2014-12-19 08:57 android_release.apk -> $godot/platform/android/java/bin/Godot-release-unsigned.apk
# lrwxrwxrwx 1 user user  44 2014-12-19 14:29 javascript_debug.zip -> $godot/bin/javascript_debug.zip
# lrwxrwxrwx 1 user user  46 2014-12-19 14:29 javascript_release.zip -> $godot/bin/javascript_release.zip
# lrwxrwxrwx 1 user user  49 2014-12-19 08:55 linux_server_32 -> $godot/templates.bin/linux_server_32*
# lrwxrwxrwx 1 user user  49 2014-12-19 08:55 linux_server_64 -> $godot/templates.bin/linux_server_64*
# lrwxrwxrwx 1 user user  52 2014-12-19 08:55 linux_x11_32_debug -> $godot/templates.bin/linux_x11_32_debug*
# lrwxrwxrwx 1 user user  54 2014-12-19 08:55 linux_x11_32_release -> $godot/templates.bin/linux_x11_32_release*
# lrwxrwxrwx 1 user user  42 2014-12-19 08:57 linux_x11_64_debug -> $godot/bin/godot.x11.debug.64*
# lrwxrwxrwx 1 user user  40 2014-12-19 08:57 linux_x11_64_release -> $godot/bin/godot.x11.opt.64*
# lrwxrwxrwx 1 user user  41 2014-12-19 08:55 osx.zip -> $godot/templates.bin/osx.zip
# lrwxrwxrwx 1 user user  54 2014-12-19 08:55 windows_32_debug.exe -> $godot/templates.bin/windows_32_debug.exe*
# lrwxrwxrwx 1 user user  56 2014-12-19 08:55 windows_32_release.exe -> $godot/templates.bin/windows_32_release.exe*
# lrwxrwxrwx 1 user user  54 2014-12-19 08:55 windows_64_debug.exe -> $godot/templates.bin/windows_64_debug.exe*
# lrwxrwxrwx 1 user user  56 2014-12-19 08:55 windows_64_release.exe -> $godot/templates.bin/windows_64_release.exe*
#
# “templates.bin” is where I use the default templates from the templates package off the godot website, since I don’t build or use them myself.
# So to build templates for all platforms, this of course has to be changed, along with extending this script.
