#!/usr/bin/env bash
case "$1" in
    test)
        set -ue
        hostname
        apt-get update ; apt-get -y install libgl1-mesa-glx libegl1 libxkbcommon-x11-0 libdbus-1-3
        python3 --version
        python3 -m venv insarviz-env
        source insarviz-env/bin/activate
        pip install pdm
        pdm install --dev
        pytest
        ;;

    docs)
        # apt-get update ; apt-get -y install libgl1-mesa-glx libegl1 libxkbcommon-x11-0 libdbus-1-3
        set -ue
        # install UV
        curl -LsSf https://astral.sh/uv/0.8.0/install.sh | sh
        export PATH="$HOME/.local/bin:$PATH"

        uv venv
        uv sync # install Insarviz
        uv pip install .
        uv pip install sphinx sphinx-rtd-theme sphinxcontrib-video

        source .venv/bin/activate
        sphinx-apidoc -e -o doc -f insarviz
        ( cd doc && sphinx-build -a -b html . html )
        tar -cvzf html-doc.tar.gz -C doc/html .
        ;;

    pages)
        # refetch tags from the repository, to purge the cache
        git fetch --prune --prune-tags
        this_tag="$(git tag --points-at HEAD)"
        tags=( $(git tag -l 'release-*' | sort -V) )
        mkdir -p public
        (
            printf '['
            comma=
            for tag in "${tags[@]}"; do
                version="${tag#release-}"
                printf '%s{ "name": "%s", "path": "%s" }' "$comma" "$version" "$tag"
                comma=,
            done
            printf ']'
        ) > "versions.json"
        for tag in "${tags[@]}"; do
            printf "Getting docs for tag: %s\n" "$tag"
            version="${tag#release-}"
            mkdir -p "public/$tag"
            {
                if [ "$tag" == "$this_tag" ]; then
                    cat html-doc.tar.gz
                else
                    curl -sL "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/jobs/artifacts/$tag/raw/html-doc.tar.gz?job=tag-docs"
                fi
            } | tar -xz -C "public/$tag"
            cp versions.json "public/$tag"
            latest="$tag"
        done

        mkdir -p public/staging
        tar -xzf html-doc.tar.gz -C public/staging
        cp versions.json "public/staging"

        ln -s "$latest" public/latest

        cat > public/index.html <<EOF
<!DOCTYPE html>
<html>
  <head>
    <script>window.location.replace('latest');</script>
  </head>
  <body>
  </body>
</html>
EOF
        ;;

    deploy)
        hostname
        function deploy_tag() {
            # find uv
            export PATH="$HOME/.local/bin:$PATH"

            printf "Going into the /usr/contrib/cycle/insarviz directory\n"
            if [ ! -e /usr/contrib/cycle/insarviz ]; then
                mkdir -p /usr/contrib/cycle/insarviz
            fi

            cd /usr/contrib/cycle/insarviz
            (
                # install the current version in a separate directory
                git fetch --prune --prune-tags
                printf "Getting worktree for %s\n" "$CI_COMMIT_TAG"
                git worktree add "$CI_COMMIT_TAG" "$CI_COMMIT_TAG"
                cd "$CI_COMMIT_TAG"
                uv venv venv
                source venv/bin/activate
                uv pip install .
            )

            # Update 'stable' and 'latest' links
            if [ -e latest ]; then
                rm -f stable
                ln -s "$(readlink latest)" stable
            fi
            rm -f latest
            ln -s "$CI_COMMIT_TAG" latest
        }
        function export-client-env() {
            declare -f deploy_tag
            declare -p CI_COMMIT_TAG
        }
        ssh ist-isdeform "$(export-client-env); deploy_tag"
        ;;

    *)
        printf "Error: unrecognized command '%s'\n" "$1"
        exit 1
        ;;
esac
