#!/bin/bash

src_dir=${SRCDIR:=./TheAdversary}
out_dir=${OUTDIR:=./out}

local=false

while getopts :l opts; do
    case $opts in
        l) local=true;;
    esac
done

pwd=$PWD

if ! $local; then
    read -p "Git username: " username
    read -sp "$username git password: " password

    for name in backend frontend; do
        path=$src_dir/$name
        if [ -d "$path" ]; then
            cd $path
            git pull
            cd $pwd
        else
            git clone https://$username:$password@mhsl.eu/gitea/TheAdversary/$name.git $path
    fi
    done
fi

if [ ! -d "$out_dir" ]; then
    mkdir $out_dir
fi

# echo "INFO: Building sass files"
# for f in $(ls $src_dir/frontend/sass/*.sass); do
#     sass --update $f $src_dir/frontend/sass/$(basename $f .sass).css
# done

echo "INFO: Building backend"
cd $src_dir/backend
go build . TheAdversary
cd $pwd

echo "INFO: Copying output to output directory"
for f in $src_dir/backend/{TheAdversary,database.sqlite3,.env}; do
    cp -rf $f $out_dir
done

for f in $src_dir/frontend; do
    cp -rf $f $out_dir/frontend/
done
