default:
    @just --list

tool := "vsftp_alpine"

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 := "21"

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

_start +args="-d":
    #!/bin/bash
    function wait_docker() {
        while [ $(docker ps | grep aim_tests_ftp | wc -l) -eq 0 ]; do
          sleep 0.1
        done 
    }
    [ $(docker ps | grep aim_tests_ftp | wc -l) -eq 0 ] && \
        docker run {{args}} \
            -it \
            --rm \
            --name aim_tests_ftp \
            -e PASV_ADDRESS=127.0.0.1 \
            -e PASV_MIN_PORT=21100 \
            -e PASV_MAX_PORT=21110 \
            -v $(pwd):/src \
            -p {{server_port}}:21 \
            -p 20:20 \
            -p 21100-21110:21100-21110 \
            {{docker_image}} > /dev/null \
        && wait_docker \
        || true

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

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

_test_curl_login:
    #!/bin/bash
    source common.sh
    response=$(curl -s -w "%{http_code}" "ftp://localhost:21/" --user "user:pass")
    [ $response = "226" ] && ok || "ERROR: Response code $response."

_test_aim_checksum_works:
    #!/bin/bash
    source common.sh
    sha_expected="1fda8bdf225ba614ce1e7db8830e4a2e9ee55907699521d500b1b7beff18523b"
    aim -s ftp://ftp.fau.de:21/gnu/MailingListArchives/README _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 ftp://ftp.fau.de:21/gnu/MailingListArchives/README _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

_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 ftp://user:pass@127.0.0.1:21/_test_aim_put_binary_file
    aim -s ftp://user:pass@127.0.0.1:21/_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)
    aim -s binary_file.tar.gz.part1 ftp://user:pass@127.0.0.1:21/_test_aim_put_resume_binary_file.tar.gz
    aim -s binary_file.tar.gz.part2 ftp://user:pass@127.0.0.1:21/_test_aim_put_resume_binary_file.tar.gz
    aim -s ftp://user:pass@127.0.0.1:21/_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