# For using the alias gw-importOfficeData or gw-importHostingAssets,
# copy the file .tc-environment to .environment (ignored by git)
# and amend them according to your external DB.

gradleWrapper () {
    if [ ! -f gradlew ]; then
        echo "No 'gradlew' found. Maybe you are not in the root dir of a gradle project?"
        return 1
    fi

    if command -v unbuffer >/dev/null 2>&1; then
        # if `unbuffer` is available in PATH, use it to print report file-URIs at the end
        TEMPFILE=$(mktemp /tmp/gw.XXXXXX)
        unbuffer ./gradlew "$@" | tee $TEMPFILE
        echo
        grep --color=never "Report:" $TEMPFILE
        rm $TEMPFILE
    else
        # if `unbuffer` is not in PATH, simply run gradle
        ./gradlew "$@"
        echo "HINT: it's suggested to install 'unbuffer' to print report URIs at the end of a gradle run"
    fi


}

postgresAutodoc () {
	if ! [ -x "$(command -v postgresql_autodoc)" ]; then
        echo "Program 'postgresql_autodoc' not found. Please install, e.g. via: sudo apt install postgresql-autodoc" >&2
		echo "See also https://github.com/cbbrowne/autodoc" >&2
        return 1
    fi

	if ! [ -x "$(command -v dot)" ]; then
        echo "Program 'graphviz dot' not found. Please install, e.g. via: sudo apt install graphviz" >&2
		echo "See also https://graphviz.org" >&2
        return 1
    fi
	postgresql_autodoc -d postgres -f build/postgres-autodoc -h localhost -u postgres --password=password \
		-m '(rbacobject|hs).*' \
		-l /usr/share/postgresql-autodoc -t neato &&
	dot -Tsvg build/postgres-autodoc.neato >build/postgres-autodoc-hs.svg && \
	echo "generated: $PWD/build/postgres-autodoc-hs.svg"

	postgresql_autodoc -d postgres -f build/postgres-autodoc -h localhost -u postgres --password=password \
		-m '(global|rbac).*' \
		-l /usr/share/postgresql-autodoc -t neato &&
	dot -Tsvg build/postgres-autodoc.neato >build/postgres-autodoc-rbac.svg && \
	echo "generated $PWD/build/postgres-autodoc-rbac.svg"
}
alias postgres-autodoc=postgresAutodoc

function importLegacyData() {
    export target=$1
    if [ -z "$target" ]; then
        echo "importLegacyData needs target argument, but none was given" >&2
    else
        source .tc-environment

        if [ -f .environment ]; then
            source .environment
        fi

        echo "using environment (with ending ';' for use in IntelliJ IDEA):"
        echo "--- BEGIN: ---"
        set | grep ^HSADMINNG_ | sed 's/$/;/'
        echo "---- END. ----"
        echo

        echo ./gradlew $target --rerun
        ./gradlew $target --rerun
    fi
}
alias gw-importOfficeData='importLegacyData importOfficeData'
alias gw-importHostingAssets='importLegacyData importHostingAssets'

alias podman-start='systemctl --user enable --now podman.socket && systemctl --user status podman.socket && ls -la /run/user/$UID/podman/podman.sock'
alias podman-stop='systemctl --user disable --now podman.socket && systemctl --user status podman.socket && ls -la /run/user/$UID/podman/podman.sock'
alias podman-use='export DOCKER_HOST="unix:///run/user/$UID/podman/podman.sock"; export TESTCONTAINERS_RYUK_DISABLED=true'

alias gw=gradleWrapper
alias pg-sql-run='docker run --name hsadmin-ng-postgres -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres:15.5-bookworm'
alias pg-sql-stop='docker stop hsadmin-ng-postgres'
alias pg-sql-start='docker container start hsadmin-ng-postgres'
alias pg-sql-remove='docker rm hsadmin-ng-postgres'
alias pg-sql-reset='pg-sql-stop; pg-sql-remove; pg-sql-run'
alias pg-sql-backup='docker exec -i hsadmin-ng-postgres /usr/bin/pg_dump --clean --create -U postgres postgres | gzip -9'
alias pg-sql-restore='gunzip --stdout | docker exec -i hsadmin-ng-postgres psql -U postgres -d postgres'

alias fp='grep -r '@Accepts' src | sed -e 's/^.*@/@/g' | sort -u | wc -l'

alias gw-spotless='./gradlew spotlessApply -x pitest -x test -x :processResources'
alias gw-check='. .aliases; . .tc-environment; gw test check -x pitest'

# HOWTO: run all 'normal' tests (no scenario+import-tests): `gw-test`
#   You can also mention specific targets: `gw-test importOfficeData`.
#   This will always use the environment from `.tc-environment`.
#
# HOWTO: re-run tests even if no changed can be detected: `gw-test --rerun`
#   You can also mention specific targets: `gw-test scenarioTest --rerun`.
#   This will always use the environment from `.tc-environment`.
#
# HOWTO: run all tests (unit, integration+acceptance, import and scenario): `gw-test --all`
#   You can also re-run all these tests, which will take ~20min: `gw-test --all --rerun`
#   This will always use the environment from `.tc-environment`.
#
function _gwTest1() {
    echo
    printf -- '=%0.s' {1..80}; echo
    echo "RUNNING gw $@"
    printf -- '-%0.s' {1..80}; echo
    ./gradlew "$@"
    printf -- '-%0.s' {1..80}; echo
    echo "DONE gw $@"
}
function _gwTest() {
  . .aliases;
  . .tc-environment;
  rm /tmp/gwTest.tmp
  if [ "$1" == "--all" ]; then
    shift # to remove the --all from $@
    # delierately in separate gradlew-calls to avoid Testcontains-PostgreSQL problem spillover
    time (_gwTest1 unitTest "$@" &&
        _gwTest1 officeIntegrationTest bookingIntegrationTest hostingIntegrationTest "$@" &&
        _gwTest1 scenarioTest "$@" &&
        _gwTest1 importOfficeData importHostingAssets "$@");
  elif [ $# -eq 0 ] || [[ $1 == -* ]]; then
    time _gwTest1 test "$@";
  else
    time _gwTest1 "$@";
  fi
  printf -- '=%0.s' {1..80}; echo
}
alias gw-test=_gwTest

alias howto=bin/howto
alias cas-curl=bin/cas-curl

# etc/docker-compose.yml limits CPUs+MEM and includes a PostgreSQL config for analysing slow queries
alias gw-importOfficeData-in-docker-compose='
        docker-compose -f etc/docker-compose.yml down &&
        docker-compose -f etc/docker-compose.yml up -d && sleep 10 &&
        time gw-importHostingAssets'

if [ ! -f .environment ]; then
    cp .tc-environment .environment
fi
source .environment

alias scenario-reports-upload='./gradlew scenarioTest convertMarkdownToHtml && ssh hsh03-hsngdev@h50.hostsharing.net "rm -f doms/hsngdev.hs-example.de/htdocs-ssl/scenarios/office/*.html" && scp build/doc/scenarios/*.html hsh03-hsngdev@h50.hostsharing.net:doms/hsngdev.hs-example.de/htdocs-ssl/scenarios/office'
alias scenario-reports-open='open https://hsngdev.hs-example.de/scenarios/office'