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
    }

    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":
    @[ $(docker ps | grep aim_tests_https | wc -l) -eq 0 ] && docker run {{args}} -it --rm --name aim_tests_https -v $(pwd):/src -p {{server_port}}:80 {{docker_image}} > /dev/null || 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 -X PUT -d `cat test.file` http://localhost:{{server_port}}/_test_curl_put_text_file
    remote_contents=$(curl -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 -X PUT --data-binary @binary_file.tar.gz  http://localhost:{{server_port}}/_test_curl_put_binary_file
    curl -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 -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
