#!/bin/bash

function list_targets {
    ls buildConfig | grep 'build_' | sed 's/build_\([a-z0-9_]*\).cfg/\1/'
    exit
}

function cleanup_after_build {
    echo "Restoring engine.cfg and export.cfg"
    if [ -e engine_stored.cfg ]
    then
        mv engine_stored.cfg engine.cfg
        echo "Restored engine.cfg"
    else
        echo "No engine.cfg to restore. Skipping."
    fi
    if [ -e export_stored.cfg ]
    then
        mv export_stored.cfg export.cfg
        echo "Restored export.cfg"
    else
        echo "No export.cfg to restore. Skipping."
    fi
}

function check_file_exists {
    if [ ! -e buildConfig/$1_$2.cfg ]
    then
        echo "Missing $1_$2.cfg."
        exit 1
    else
        echo "File $1_$2.cfg found."
    fi
}

function bootstrap_configs {
    echo "Preserving engine.cfg and export.cfg"
    mv engine.cfg engine_stored.cfg
    mv export.cfg export_stored.cfg
    echo "Copying target specific configs"
    cp buildConfig/engine_$1.cfg engine.cfg
    cp buildConfig/export_$2.cfg export.cfg
}

function prepare_build_config {
    bootstrap_configs $1 $2

    if [ ! -d "$3/$4" ]
    then
        echo "Target subdir not exists. Creating: $3/$4"
        mkdir $3/$4
    else
        echo "Target dir exists: $3/$4"
    fi
}

function command_exists {
    hash "$1" 2>/dev/null ;
}

function run_target {
    echo "Running target: $1"
    if [ "$2" == "" ]
    then
        echo "Please specify destination directory"
        exit 1
    fi
    if [ ! -d $2 ]
    then
        echo "Specified destination directory does not exiss or is not writable: $2"
        exit
    fi
    if [ "$3" == "" ]
    then
        echo "Please specify base for resulting file name"
        exit 1
    fi

    godot_command=""
    if command_exists linux_server.64 ; then
        echo "Godot exporter linux_server.64 found."
        godot_command="linux_server.64"
    else
        echo "Godot exporter linux_server.64 not found!"

        if command_exists godot_headless ; then
            echo "Godot exporter godot_headless found."
            godot_command="godot_headless"
        else
            echo "Godot exporter godot_headless not found!"
        fi
    fi

    if [ "$godot_command" == "" ]
        then
        echo "No suitable godot exporter found"
    fi

    check_file_exists 'build' $1
    target_subdir="`grep 'subdir' buildConfig/build_$1.cfg | sed 's/subdir: //'`"
    godot_target="`grep 'godot_target' buildConfig/build_$1.cfg | sed 's/godot_target: //'`"
    engine_file="`grep 'engine_file' buildConfig/build_$1.cfg | sed 's/engine_file: //'`"
    export_file="`grep 'export_file' buildConfig/build_$1.cfg | sed 's/export_file: //'`"
    file_extension="`grep 'file_extension' buildConfig/build_$1.cfg | sed 's/file_extension: //'`"
    extra_params="`grep 'extra_params' buildConfig/build_$1.cfg | sed 's/extra_params://'`"

    check_file_exists 'engine' $engine_file
    check_file_exists 'export' $export_file

    prepare_build_config $engine_file $export_file $2 $target_subdir

    echo "Running export. Logging into $2/$target_subdir/build.log"
    echo "$godot_command -export '$godot_target' $2/$target_subdir/$3_$file_extension"
    $godot_command -export "$godot_target" "$2/$target_subdir/$3_$file_extension" > $2/$target_subdir/build.log 2>&1

    if [ ! -e "$2/$target_subdir/$3_$file_extension" ]
        then
        echo "Build probably failed! Check the log!"
    fi

    cleanup_after_build
}

function run_all {
    all_targets=`list_targets`
    for target_name in $all_targets
    do
        run_target $target_name $1 $2
    done
    exit
}


if [ "$1" == "list" ]
then
    list_targets
    exit
fi

if [ "$1" == "all" ]
then
    run_all $2 $3
    exit
fi

if [ "$1" == "cleanup" ]
then
    cleanup_after_build
    exit
fi

if [ "$1" == "build" ]
then
    run_target $2 $3 $4
    exit
fi

if [ "$1" == "bootstrap" ]
then
    check_file_exists 'build' $2
    engine_file="`grep 'engine_file' buildConfig/build_$2.cfg | sed 's/engine_file: //'`"
    export_file="`grep 'export_file' buildConfig/build_$2.cfg | sed 's/export_file: //'`"

    check_file_exists 'engine' $engine_file
    check_file_exists 'export' $export_file

    bootstrap_configs $engine_file $export_file
    exit
fi

echo "Valid commands:"
echo "$1 list"
echo "$1 bootstrap (target name)"
echo "$1 build (target_name) (destination dir) (base file name)"
echo "$1 cleanup"
echo "$1 all (destination dir) (base file name)"
exit 1