# 
FROM ghcr.io/minetest/minetest:latest as builder
ARG DEPENDENCIES
ARG ADDITIONAL_FILES=""
ARG MINETEST_ADD_CONF=""

ENV STOP_SERVER=true
ENV FAILFAST=false
ENV MINETEST_GAME_PATH=/usr/local/share/minetest/games
ENV SHOW_DEBUG=false
ENV CURRENT_MOD="test_harness"
ENV ADDITIONAL_MODS=""

USER root

RUN mkdir -p /usr/local/share/minetest/games && \
    apk add --no-cache bash curl jq

# WORKDIR /config/.minetest/games/devtest/mods
RUN <<EOF
echo "DEPENDENCIES: $DEPENDENCIES"
while read dep; do
    url=$(echo "$dep" | jq -cr ".url")
    dep_path=$(echo "$dep" | jq -cr ".path")
    strip=$(echo "$dep" | jq -cr ".strip // 1")
    echo "Download $url to $dep_path with strip=$strip"
    mkdir -p "$dep_path" && curl -s -L "$url" | tar zxf - -C "$dep_path" --strip-components="$strip"
done < <(echo "$DEPENDENCIES" | jq -cr '.[]')
EOF

RUN <<EOF
echo "ADDITIONAL_FILES"
while read file_def; do
    content=$(echo "$file_def" | jq -rc ".content//\"\"")
    dep_path=$(echo "$file_def" | jq -rc ".path//\"\"")
    if ! [ -z "$dep_path" ]; then
        echo "Creating file $dep_path"
        mkdir -p $(dirname "$dep_path")
        echo "$content" > "$dep_path"
    fi
done < <(echo "$ADDITIONAL_FILES" | jq -c '.[]')
EOF

COPY <<"EOF" /usr/local/sbin/run_minetest
#!/usr/bin/env bash

function clean_up {

    # Perform program exit housekeeping
    kill -9 $mtpid
    if [ "$SHOW_DEBUG" == "true" ]; then
      echo "---------------------------- DEBUG.TXT -------------------------"
      test -f /var/lib/minetest/.minetest/debug.txt && cat /var/lib/minetest/.minetest/debug.txt
      echo "---------------------------- DEBUG.TXT -------------------------"
    fi
    test -f /var/lib/minetest/.minetest/world/tests_ok || exit 1
    exit 0
}

rm -f /etc/minetest/minetest.conf
cat /etc/minetest/minetest.conf.base - <<-EOT > /etc/minetest/minetest.conf
    test_harness_mods=${CURRENT_MOD}${ADDITIONAL_MODS}
    test_harness_stop_server=${STOP_SERVER}
    test_harness_failfast=${FAILFAST}
EOT

/usr/local/bin/minetestserver --config /etc/minetest/minetest.conf --gameid minetest "$@" &
mtpid=$!

trap clean_up SIGHUP SIGINT SIGTERM
echo "SHOW_DEBUG: $SHOW_DEBUG"
wait $mtpid

if [ "$SHOW_DEBUG" == "true" ]; then
    echo "---------------------------- DEBUG.TXT -------------------------"
    test -f /var/lib/minetest/.minetest/debug.txt && cat /var/lib/minetest/.minetest/debug.txt
    echo "---------------------------- DEBUG.TXT -------------------------"
fi
test -f /var/lib/minetest/.minetest/world/tests_ok || exit 1
exit 0
EOF

COPY <<-EOF /var/lib/minetest/.minetest/world/map_meta.txt
    mg_name=singlenode
    [end_of_params]
EOF

# Customize the test_harness_mods value to list the mods to test. Empty value means all, including the test harness itself
COPY <<-EOF /etc/minetest/minetest.conf.base
    test_harness_run_tests=true
    max_forceloaded_blocks=9999
    name=admin
    creative_mode=true
    debug_log_level=verbose
    show_debug=true
    enable_console=true
    static_spawnpoint=(0,0,0)
    mg_name=flat
    water_level=-2
    mgflat_ground_level=-1
    mg_flags=nocaves,nodungeons,nolight,nodecorations,nobiomes,noores
    ${MINETEST_ADD_CONF}
EOF

RUN chown -R minetest:minetest /var/lib/minetest/.minetest /usr/local/sbin/run_minetest && \
    chmod 755 /usr/local/sbin/run_minetest && \
    chown -R minetest:minetest /etc/minetest


USER minetest

ENTRYPOINT /usr/local/sbin/run_minetest

CMD []
