#!/opt/homebrew/bin/bash

# this watch.sh requires bash 4.0 or higher
# `brew install bash` on macos
bash_version=$(bash --version | head -n 1 | awk '{print $4}')

echo ""
echo "bash version is $bash_version -----------------------"
echo "watch.sh requires bash 4.0 or higher ----------------"
echo ""

build_mod() {
    template_mod="src/generated.rs"

    echo '// Auto generated by $$ make build_template' >$template_mod
    echo "// Don't edit this file by HAND, edit the ../template directory instead." >>$template_mod
    echo "// Then keep running watch.sh" >>$template_mod
    echo "" >>$template_mod

    os=$(uname -s)
    opts=""

    if [ "$os" = "Linux" ]; then
        opts="-w0"
    fi

    # dir: template/src
    echo "pub const FILE_APP_CONFIG_RS: &str = \"$(cat template/src/app_config.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
    echo "pub const FILE_APP_DATA_RS: &str = \"$(cat template/src/app_data.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
    echo "pub const FILE_APP_MENU_RS: &str = \"$(cat template/src/app_menu.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod

    echo "pub const FILE_BUILDER_RS: &str = \"$(cat template/src/builder.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
    echo "pub const FILE_GENERATED_RS: &str = \"$(cat template/src/generated.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod

    echo "pub const FILE_MAIN_RS: &str = \"$(cat template/src/main.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
    echo "pub const FILE_UTIL_RS: &str = \"$(cat template/src/util.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod

    # dir: template
    echo "pub const FILE_BUILD_RS: &str = \"$(cat template/build.rs | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
    echo "pub const FILE_CARGO_TOML: &str = \"$(cat template/Cargo.toml | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
    echo "pub const FILE_CARGO_LOCK: &str = \"$(cat template/Cargo.lock | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
    echo "pub const FILE_TAURI_CONF_JSON: &str = \"$(cat template/tauri.conf.json | base64 $opts)\";" >>$template_mod && echo "" >>$template_mod
}

build_template_gen() {

    script_js="template/src/script.js"
    out="template/src/generated.rs"

    echo "// This file is generated by watch.sh" >"$out"
    echo "// Do not edit it directly" >>"$out"
    echo "" >>"$out"
    echo "pub const INIT_SCRIPT: &str = r#\"" >>"$out"
    echo "$(cat "$script_js")" >>"$out"
    echo "\"#;" >>"$out"
}

autogen() {
    build_template_gen
    build_mod
}

watch_files() {
    # array of allowed files to watch
    files=(
        "template/src/app_config.rs"
        "template/src/app_data.rs"
        "template/src/app_menu.rs"
        "template/src/builder.rs"
        "template/src/generated.rs"
        "template/src/main.rs"
        "template/src/util.rs"
        "template/src/script.js"
        "template/build.rs"
        "template/Cargo.toml"
        "template/Cargo.lock"
        "template/tauri.conf.json"
        "template/icons/app-icon.png"
    )

    # map of states of files (map key is the sha256 of $file and state value is the string of : ls -l $file)
    declare -A states

    # init states
    for file in "${files[@]}"; do
        echo "watching $file"

        if [ ! -f "$file" ]; then
            echo "file $file does not exist skipping"
            continue
        fi

        key="$(sha256sum $file)"
        value="$(ls -l $file)"
        states[$key]="$value"
    done

    # watch loop
    while true; do
        for file in "${files[@]}"; do
            value="$(ls -l $file)"
            key="$(sha256sum $file)"

            if [ ! -f "$file" ]; then
                echo "file $file does not exist skipping"
                continue
            fi

            if [ "${states[$key]}" != "$value" ]; then
                echo "file $file changed"
                states[$key]="$value"
                autogen
            fi
        done
        sleep 1
    done
}

autogen
watch_files
