#! /usr/bin/env bash

RPH_UID=${RPH_UID:-${UID}}

# Identify source path (even if symlinked)
SOURCE=${BASH_SOURCE[0]}
while [ -L "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )
  SOURCE=$(readlink "$SOURCE")
  [[ $SOURCE != /* ]] && SOURCE=$DIR/$SOURCE # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SCRIPT_DIR=$( cd -- "$( dirname -- "${SOURCE}" )" &> /dev/null && pwd )

BASE_NAME="$(basename -- "$0")"
BASE_BASE_NAME="${BASE_NAME%.*}"

RUNTIPI_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
RUNTIPI_CLI="${RUNTIPI_ROOT}/runtipi-cli"
set -e

cd "${RUNTIPI_ROOT}" > /dev/null

runtipi-cli() {
    if ! [ -x "${RUNTIPI_CLI}" ]; then
        >&2 printf '%s\n' \
            "ERROR: ${RUNTIPI_CLI} not found or not executable!"
        return 1
    fi
    "${RUNTIPI_CLI}" "${@}"    
}

append_file_param() {
    [ -e "${2:?File name missing}" ] || return
    echo "${*}"
}

runtipi-app-docker-compose() {
    local APP="${1:-Must supply app name}"

    docker compose \
        $(append_file_param --env-file user-config/.env.local) \
        $(append_file_param --env-file app-data/${APP}/app.env) \
        $(append_file_param --env-file user-config/${APP}/app.env) \
        --project-name ${APP} \
        $(if [ "$APP" != "runtipi" ]; then
          echo --file apps/${APP}/docker-compose.yml
          append_file_param --file repos/29ca930bfdaffa1dfabf5726336380ede7066bc53297e3c0c868b27c97282903/apps/docker-compose.common.yml
          append_file_param --file "user-config/${APP}/docker-compose.yml"
        else
          append_file_param --file "docker-compose.yml"
          append_file_param --file "user-config/tipi-compose.yml"
        fi) \
        ${@:2}
}

dls() {
    local base='{{.Status}}\t{{.ID}}\t{{.Names}}\t{{.Image}}' #'\t{{.Networks}}\t{{.Ports}}\t{{.Mounts}}'
    local compose='{{.Label "com.docker.compose.project"}}\t{{.Label "com.docker.compose.service"}}'
    local format="table $compose\t$base"
    docker container ls --all --format "$format" | ( sed -u '1s/.*/\U&/; q'; sed -Ee 's|^|555|; s|^555runtipi|000runtipi|;' | sort | sed -Ee 's/^[[:digit:]]{3}//' )
}

case "${1}" in
    cli)
        runtipi-cli "${@:2}"
        ;;
    log|logs)
        POSTGRES_PASSWORD=_ TIPI_VERSION=_ LOCAL_DOMAIN=_ DOMAIN=_ runtipi-app-docker-compose "${2:-runtipi}" logs ${3:+"${@:3}"}
        ;;
    start)
        ROOT_FOLDER_HOST="${RUNTIPI_ROOT}" RUNTIPI_APP_DATA_PATH="${RUNTIPI_ROOT}" \
        runtipi-cli start --env-file user-config/.env.local --no-permissions
        ;;
    update)
        if [ -z "${2}" ]; then
            ${SCRIPT_DIR}/checkver.sh
        else
            runtipi-cli update --env-file user-config/.env.local --no-permissions "${2:?Must supply version}" "${@:3}"
        fi
        ;;
    docker)
        docker "${@:2}"
        ;;
    dls)
        dls "${@:2}"
        ;;
    app)
        runtipi-app-docker-compose "${@:2}"
        ;;
    dockge)
        cd "${RUNTIPI_ROOT}/../dockge/stacks/${2:?Must supply stack name}" > /dev/null
        docker compose "${@:3}"
        ;;
    shell)
        runtipi-app-docker-compose "${2:?}" exec ${5:+"${@:5}"} -it "${4:-${2}}" "${3:-bash}"
        ;;
    setup)
        ln -s $2 "$(cd -- "${SCRIPT_DIR}" && pwd)/${BASE_NAME}" "${3:-$HOME/.local/bin/}"
        ;;
    edit)
        ${VISUAL:-${EDITOR:-vi}} $0
        ;;
    *)
        printf '%s    %-12s %s\n' \
            "" "" ""\
            "Usage:" "" ""\
            "  ${BASE_NAME} <command> [args...]" "" ""\
            "" "" "" \
            "Available commands:" "" ""\
            "" "" "" \
            "runtipi" "" ""\
            "" "cli"        "runtipi-cli" \
            "" "log"        "runtipi docker stack logs" \
            "" "start"      "start runtipi" \
            "" "update"     "update runtipi to a specific version" \
            "" "" "" \
            "docker/docker-compose" "" ""\
            "" "app"        "docker compose for runtipi apps" \
            "" "dls"        "stylized docker ls" \
            "" "docker"     "docker" \
            "" "dockge"     "docker compose for dockge stacks" \
            "" "shell"      "enter an insteractive shell" \
            "" "" "" \
            "misc." "" ""\
            "" "exec"       "execute within the shell, START_DIR env applies" \
            "" "" "" \
            "" "setup"      "setup runtipictl in user's .local/bin dir" \
            "" "" "${BASE_NAME} setup" \
            "" "" "${BASE_NAME} setup '' ~/.local/bin/${BASE_BASE_NAME}" \
            "" "" "${BASE_NAME} setup '' ~/.local/bin/runtipictl" \
            "" "" "" \
            "Related env. vars:" "" "" \
            "" "VISUAL EDITOR RPH_UID QUIET START_DIR ROOT_EXEC" ""

        ;;
esac

# vim: set ft=sh expandtab tabstop=4 shiftwidth=4:
