#!/bin/sh

set -ex

print() {
    printf '%s\n' "$*"
}

die() {
    st=${?:-0}
    if [ $st -eq 0 ]; then
        st=2
    fi
    print "$*" >&2
    exit $st
}

usage() {
    die 'build [ -n -pg1[234] -profile release ] ( test-crates | test-extension | install | test-doc | test-updates | clippy)'
}

require_pg_version() {
    [ -n "$pg_version" ] || die 'specify one of -pg15 | -pg16 | -pg17 | -pg18'
}

find_pg_config() {
    if [ -z "$pg_config" ]; then
        require_pg_version
        pg_config=`which $(sed -ne 's/"//g' -e "s/^pg$pg_version *= *//p" ~/.pgrx/config.toml)`
    fi
    [ -x "$pg_config" ] || die "$pg_config not executable"
}

require_cargo_pgrx() {
    [ -n "$cargo_pgrx" ] || die 'specify path to cargo-pgrx (0.4 series or newer)'
}

require_cargo_pgrx_old() {
    [ -n "$cargo_pgrx_old" ] || die 'specify path to cargo-pgrx (0.2-0.3 series)'
}

find_profile() {
    [ -n "$profile" ] || profile=dev
}

[ $# -ge 1 ] || usage

# check versions
cargo --version
rustc --version
rustup --version

while [ $# -gt 0 ]; do
    arg="$1"
    shift
    case "$arg" in
        -n)
            nop=:
            ;;

        -pgconfig)
            pg_config="$1"
            shift
            ;;

        -cargo-pgrx)
            cargo_pgrx="$1"
            shift
            ;;

        -cargo-pgrx-old)
            cargo_pgrx_old="$1"
            shift
            ;;

        -pgport)
            pg_port="$1"
            shift
            ;;

        -pg1[0-9])         # If this script survives to postgresql 19, WE WIN!
            pg_version=${arg#-pg}
            pg=pg$pg_version
            [ -z "$pg_port" ] && pg_port=288$pg_version
            ;;

        -profile)
            profile="$1"
            shift
            ;;

        clippy)
            find_profile
            $nop cargo fetch
            $nop cargo clippy --profile $profile --workspace --features pg_test -- -D warnings
            ;;

        test-crates)
            # Should find no dependency crates to fetch.  If it finds any, we need to update the cache key.
            find_profile
            $nop cargo fetch
            $nop cargo test --profile $profile --workspace --exclude timescaledb_toolkit
            ;;

        test-extension)
            cd extension
            find_profile
            require_pg_version
            $nop cargo fetch
            $nop cargo test --profile $profile --features "$pg pg_test" --no-default-features
            ;;

        install)
            find_profile
            require_pg_version
            find_pg_config
            (cd extension && $nop cargo pgrx install --profile $profile -c "$pg_config")
            $nop cargo run --manifest-path tools/post-install/Cargo.toml "$pg_config"
            ;;

        test-doc)
            find_profile
            require_pg_version
            $nop cargo pgrx start --package timescaledb_toolkit $pg || (cat /home/postgres/.pgrx/${pg_version}.log; false)
            $nop cargo run --profile $profile -p sql-doctester -- \
                 -h localhost \
                 -p $pg_port \
                 docs
            $nop cargo pgrx stop --package timescaledb_toolkit $pg
            ;;

        test-updates)
            find_profile
            require_pg_version
            find_pg_config
            require_cargo_pgrx
            require_cargo_pgrx_old
            $nop cargo pgrx start --package timescaledb_toolkit $pg || (cat /home/postgres/.pgrx/${pg_version}.log; false)
            $nop cargo run --profile $profile --manifest-path tools/update-tester/Cargo.toml -- full-update-test-source \
                 -h localhost \
                 -p $pg_port \
                 --cache old-versions \
                 "$pg_config" \
                 "$cargo_pgrx" \
                 "$cargo_pgrx_old"
            $nop cargo pgrx stop --package timescaledb_toolkit $pg
            ;;

        *)
            usage
            ;;
    esac
done
