#!/bin/bash

set -e


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

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


mkdir app
cd app
curl -L -o build.zip https://github.com/GravitLauncher/Launcher/releases/latest/download/LaunchServerBuild.zip
unzip build.zip
rm build.zip
curl -L -o ServerWrapper.jar https://github.com/GravitLauncher/Launcher/releases/latest/download/ServerWrapper.jar
curl -L -o ServerWrapperInline.jar https://github.com/GravitLauncher/Launcher/releases/latest/download/ServerWrapperInline.jar
cd ..

mkdir launcher-modules
curl -L -o launcher-modules/JavaRuntime.jar https://github.com/GravitLauncher/LauncherRuntime/releases/latest/download/JavaRuntime.jar
mkdir runtime
cd runtime
curl -L -o runtime.zip https://github.com/GravitLauncher/LauncherRuntime/releases/latest/download/runtime.zip
unzip runtime.zip
rm runtime.zip
cd ..

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
