#!/bin/sh
# blame: @dmilith
# 2019-01-29-1540-1548772806


clear

_params="${*}"

_uname="$(uname 2>/dev/null)"
_cargo_project_dir="$(pwd 2>/dev/null)"
_release_type="${1:-release}"
_project_name="Krecik"
_bin_name="krecik-server"
_debug_target="target/debug/${_bin_name}"
_release_target="target/release/${_bin_name}"

_cargo_color_flag="--color always"
_cargo_verbose_flag="--verbose"
if [ "release" != "${_release_type}" ]; then
    export RUST_BACKTRACE=1
    export DEBUG=1
    _cargo_flags="--jobs 4 ${_cargo_verbose_flag} ${_cargo_color_flag}"
else
    unset DEBUG RUST_BACKTRACE
    _cargo_flags="--release ${_cargo_color_flag}"
fi


failure () {
    printf "FATAL: %b\n" "${@}"
    exit 1
}


print_project_info () {
    printf "Project: %b\n" "${_project_name}"
    printf "Project directory: %b\n" "${_cargo_project_dir}"
    printf "Project build flags: %b\n" "${_cargo_flags}"
    printf "System type: %b\n" "${_uname}"
}


sanity_checks () {
    if [ ! -x "$(which curl)" ]; then
        failure "Unavailable utility: Curl >= 7.x! (Expected 'curl' in \$PATH!)"
    fi

    if [ ! -x "$(which curl-config)" ]
    then
        failure "Unavailable devel-files of: Curl >= 7.x! (Expected 'curl-config' in \$PATH!)"
    fi

    if [ ! -x "$(which rustc)" ] \
    || [ ! -x "$(which cargo)" ]
    then
        failure "Unavailable Rust compiler >= 1.32.0! (Expected 'rustc' and 'cargo' in \$PATH!)"
    fi

    if [ ! -x "$(which perl)" ]
    then
        failure "Unavailable Perl >= 5.x! (Expected 'perl' in \$PATH!)"
    fi

    if [ ! -x "$(which gmake)" ]
    then
        failure "Unavailable utility: Gmake! (Expected 'gmake' in \$PATH!)"
    fi

    if [ ! -x "$(which cmake)" ]
    then
        failure "Unavailable utility: Cmake! (Expected 'cmake' in \$PATH!)"
    fi

    if [ ! -x "$(which openssl)" ]
    then
        failure "Unavailable utility: OpenSSL! (Expected 'openssl' in \$PATH!)"
    fi

    if [ ! -x "$(which grep)" ] \
    || [ ! -x "$(which awk)" ] \
    || [ ! -x "$(which head)" ] \
    || [ ! -x "$(which rm)" ] \
    || [ ! -x "$(which ps)" ] \
    || [ ! -x "$(which seq)" ]
    then
        failure "Unavailable base system utilities! (Expected utilities: 'curl', 'ps', 'grep', 'awk', 'head', 'seq', 'rm', 'perl'. 'make', …)"
    fi
}


build_project () {
    cargo build ${_cargo_flags}
    if [ "0" != "${?}" ]; then
        printf "Build Failed!\n" \
            && exit 1
    fi
}


run_project () {
    case "${_release_type}" in
        release)
            bin/build \
                && ${SHELL} -c "${_release_target}"
            ;;

        *)
            bin/build "dev" \
                && ${SHELL} -c "${_debug_target}"
            ;;
    esac
}


test_project () {
    cargo test \
        --jobs 4 \
        --lib \
        --tests \
        ${_cargo_color_flag} \
        -- \
            || failure "Test failed!"
}


continuous_development () {
    if [ -z "${1}" ]; then
        printf "Initializing continuous code watch (build triggered by code changes)\n"
        DEBUG=1 \
            cargo watch \
                --clear \
                --postpone \
                --exec clippy \
                --shell "${SHELL}" \
                --watch "src" \
                    || failure "Cargo watch failed!"
    else
        printf "Starting code watch on continuous build\n"
        DEBUG=1 \
            cargo watch \
                --clear \
                --exec clippy \
                --shell "${SHELL}" \
                --watch "src" \
                    || failure "Cargo watch failed!"
    fi
}
