default:
    @just --list

tool := "nginx_webdav"
docker_container_registry := "docker.pkg.github.com"
docker_user_repo := "mihaigalos/docker"
docker_image_version := "latest"
docker_image := docker_container_registry + "/" + docker_user_repo + "/" + tool + ":" + docker_image_version
server_port := "8081"

build:
    docker build -t {{docker_image}} .

push:
    docker push {{docker_image}}


test: _start && _stop
    #!/bin/bash
    cat <<'EOF' > common.sh
    #!/bin/bash
    function err() {
        echo -e "\e[1;31m${@}\e[0m" >&2
        exit 1
    }

    function ok() {
        echo -e "\e[1;32mOK\e[0m"
    }

    function highlight() {
        echo -en "\e[1;37m${@}\e[0m"
    }
    export PATH=$PATH:$(realpath ../../target/debug/)
    EOF
    
    source common.sh
    highlight "\nRunning tests in $PWD\n\n"
    for test in $(grep ^_test_ Justfile | cut -d':' -f1);
    do
        highlight "$test "
        just $test
    done

_start +args="-d":
    #!/bin/bash
    function wait_docker() {
        while [ $(docker ps | grep aim_tests_https | wc -l) - 0 ]; do
          sleep 0.1
        done 
    }
    docker run {{args}} \
        -it \
        --rm \
        --name aim_tests_https \
        -v $(pwd):/src \
        -p {{server_port}}:80 \
        {{docker_image}} > /dev/null \
    && wait_docker \
    || true

_stop:
    @rm common.sh
    @docker stop aim_tests_https > /dev/null 2>&1
    @docker rm --force aim_tests_https > /dev/null 2>&1

_test_curl_put_text_file:
    #!/bin/bash
    source common.sh
    curl -s -X PUT -d `cat test.file` http://localhost:{{server_port}}/_test_curl_put_text_file
    remote_contents=$(curl -s -s http://localhost:{{server_port}}/_test_curl_put_text_file)
    [ "$remote_contents" = "$(cat test.file)" ] && ok || err "ERROR: remote contents doesn't equal sent content."

_test_curl_put_binary_file:
    #!/bin/bash
    source common.sh
    sha_input=$(sha256sum binary_file.tar.gz | cut -d' ' -f1)
    curl -s -X PUT --data-binary @binary_file.tar.gz  http://localhost:{{server_port}}/_test_curl_put_binary_file
    curl -s -s localhost:{{server_port}}/_test_curl_put_binary_file -o _test_curl_put_binary_file
    sha_output=$(sha256sum _test_curl_put_binary_file | cut -d' ' -f1)
    [ "$sha_input" = "$sha_output" ] && ok || err "ERROR: input and output SHA256s don't match."
    rm _test_curl_put_binary_file

_test_aim_get_binary_file:
    #!/bin/bash
    source common.sh
    sha_input=$(sha256sum binary_file.tar.gz | cut -d' ' -f1)
    aim -s http://localhost:8081/_test_curl_put_binary_file _test_aim_get_binary_file
    sha_output=$(sha256sum _test_aim_get_binary_file | cut -d' ' -f1)
    [ "$sha_input" = "$sha_output" ] && ok || err "ERROR: input and output SHA256s don't match."
    rm _test_aim_get_binary_file

_test_aim_put_binary_file:
    #!/bin/bash
    source common.sh
    sha_input=$(sha256sum binary_file.tar.gz | cut -d' ' -f1)
    aim -s binary_file.tar.gz http://localhost:8081/_test_aim_put_binary_file
    aim -s http://localhost:8081/_test_aim_put_binary_file _test_aim_put_binary_file
    sha_output=$(sha256sum _test_aim_put_binary_file | cut -d' ' -f1)
    [ "$sha_input" = "$sha_output" ] && ok || err "ERROR: input and output SHA256s don't match."
    rm _test_aim_put_binary_file

_test_aim_put_resume_binary_file:
    #!/bin/bash
    source common.sh
    sha_input=$(sha256sum binary_file.tar.gz | cut -d' ' -f1)
    curl -s -X PUT --data-binary @binary_file.tar.gz.part1  http://localhost:{{server_port}}/_test_aim_put_resume_binary_file.tar.gz
    aim -s binary_file.tar.gz.part2 http://localhost:8081/_test_aim_put_resume_binary_file.tar.gz
    aim -s http://localhost:8081/_test_aim_put_resume_binary_file.tar.gz _test_aim_put_resume_binary_file
    sha_output=$(sha256sum _test_aim_put_resume_binary_file | cut -d' ' -f1)
    [ "$sha_input" = "$sha_output" ] && ok || ok "ERROR: input and output SHA256s don't match."
    rm _test_aim_put_resume_binary_file

_test_aim_checksum_works:
    #!/bin/bash
    source common.sh
    sha_expected="0e0f0d7139c8c7e3ff20cb243e94bc5993517d88e8be8d59129730607d5c631b"
    aim -s https://github.com/XAMPPRocky/tokei/releases/download/v12.0.4/tokei-x86_64-unknown-linux-gnu.tar.gz _test_aim_checksum_works $sha_expected
    [ $? -eq 0 ] && ok || err "ERROR: expected and actual SHA256s don't match."
    rm _test_aim_checksum_works

_test_aim_checksum_fails_when_checksum_mismatch:
    #!/bin/bash
    source common.sh
    sha_expected="AAAA0d7139c8c7e3ff20cb243e94bc5993517d88e8be8d59129730607d5c631b"
    aim -s https://github.com/XAMPPRocky/tokei/releases/download/v12.0.4/tokei-x86_64-unknown-linux-gnu.tar.gz _test_aim_checksum_fails_when_checksum_mismatch $sha_expected
    [ $? -ne 0 ] && ok || err "ERROR: expected checksum mismatch, got equal."
    rm _test_aim_checksum_fails_when_checksum_mismatch