#!/usr/bin/env bash

#ddev-generated
# Support for TablePlus, https://tableplus.com/
# This command is available on macOS and WSL2 if TablePlus is installed in the default location.
## Description: Run tableplus with current project database
## Usage: tableplus
## Example: "ddev tableplus"
## OSTypes: darwin,wsl2
## HostBinaryExists: /Applications/TablePlus.app,/Applications/Setapp/TablePlus.app,/mnt/c/Program Files/TablePlus/TablePlus.exe

if [ "${DDEV_PROJECT_STATUS}" != "running" ] && [ -z "$no_recursion" ]; then
  echo "Project ${DDEV_PROJECT} is not running, starting it"
  ddev start
  start_exit_code=$?
  if [ $start_exit_code -ne 0 ]; then
    exit $start_exit_code
  fi
  # run this script again, as the environment is updated after "ddev start"
  no_recursion=true ddev "$(basename "$0")" "$@"
  exit $?
fi

dbtype=${DDEV_DBIMAGE%:*}
driver=mysql
if [[ $dbtype == "postgres" ]]; then
    driver=$dbtype
fi
query="${driver}://db:db@127.0.0.1:${DDEV_HOST_DB_PORT}/db?Enviroment=local&Name=ddev-${DDEV_SITENAME}"

case $OSTYPE in
  "linux-gnu")
    "/mnt/c/Program Files/TablePlus/TablePlus.exe" $query >/dev/null &
    ;;
  "darwin"*)
    set -eu -o pipefail
    if [ -d "/Applications/Setapp/TablePlus.app" ]; then
        open "$query" -a "/Applications/Setapp/TablePlus.app/Contents/MacOS/TablePlus"
    else
        open "$query" -a "/Applications/TablePlus.app/Contents/MacOS/TablePlus"
    fi
    ;;
esac
