#!/bin/bash

set -e


if ! command -v git >/dev/null 2>&1
then
    echo "git could not be found"
    exit 1
fi

if ! command -v java >/dev/null 2>&1
then
    echo "java could not be found"
    exit 1
fi

git clone --recursive -b dev https://github.com/GravitLauncher/Launcher.git src
cd src
chmod +x ./gradlew && ./gradlew build installDist
cd ..

git clone -b dev https://github.com/GravitLauncher/LauncherRuntime.git srcRuntime
cd srcRuntime
chmod +x ./gradlew && ./gradlew build
cd ..

ln -s src/components/launchserver/build/install/launchserver app
ln -s srcRuntime/build/libs/JavaRuntime.jar JavaRuntime.jar
ln -s srcRuntime/runtime runtime


cat <<EOF > start.sh
#!/bin/bash
export APP_HOME=app
export JAVA_OPTS=-Xmx512M

exec app/bin/launchserver \$\@
EOF
chmod +x start.sh
chmod +x app/bin/launchserver

echo "=================================================="
echo "Install completed"
echo "Usage 'modules launcher-load JavaRuntime.jar' in LaunchServer console to load JavaRuntime module"
