#! /usr/bin/bash

DIR="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"

REPO_DIR="$DIR/repo"
SOURCE_DIR="$REPO_DIR/source"

rm -rf "$SOURCE_DIR/.import/"

BRANCH="$1"

if [ ! -z "$BRANCH" ]; then
	echo "Setting repo to branch $BRANCH"
	git -C $REPO_DIR reset --hard HEAD
	git -C $REPO_DIR checkout $BRANCH
fi

echo "Updating source..."
git -C $REPO_DIR pull

GAME_DIR="$DIR/game"
rm -rf $GAME_DIR
mkdir $GAME_DIR

echo "Creating import files..."

godot --path "$SOURCE_DIR" -e -q

echo "Building..."
godot --path "$SOURCE_DIR" --export-debug "HTML5" "$GAME_DIR/index.html"
