#!/bin/sh
set -e

DB_CONTAINER_NAME='songwhip-api-database-dev'
DIR="$(cd "$(dirname "$0")" && pwd)"

# create a database container which exposes localhost:5433
# we use a different port to the dev database to avoid collisions
if [ "$POSTGRES_HOST" = "localhost" ]; then
  echo "\nstarting local database '$DB_CONTAINER_NAME' …\n"

  "$DIR"/database/start --name="$DB_CONTAINER_NAME" --port="$POSTGRES_PORT"

  onExit() {
    echo "\nstopping database '$DB_CONTAINER_NAME' …\n"
    "$DIR"/database/stop  --name="$DB_CONTAINER_NAME"
  }

  # Trap to call onExit function on script exit
  trap onExit EXIT
fi

GIT_COMMIT=development tsx watch --max-http-header-size=2000000 --no-check --clear-screen=false --include "./app" ./index.ts
