#!/bin/bash

# Determine the path to this script (we'll use this to figure out relative positions of other files)
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  SCRIPT_PATH="$( 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
export SCRIPT_PATH="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

# Check if we are running 'Dev Mode' and if so, override values for devs
QUARTO_SRC_DIR="`cd "$SCRIPT_PATH/../../../src" > /dev/null 2>&1 && pwd`"
DEV_PATH=$QUARTO_SRC_DIR/quarto.ts
if [ -f "$DEV_PATH" ]; then

  # Caller can point deno at another entry point (e.g. the typescript file)
  if [ -z ${QUARTO_ACTION+x} ]; then
    QUARTO_ACTION=run
  fi

  # Local import map
  QUARTO_IMPORT_ARGMAP=--importmap=$QUARTO_SRC_DIR/import_map.json

  # Allow calls to override the target
  if [ -z ${QUARTO_TARGET+x} ]; then
    QUARTO_TARGET=$DEV_PATH
  fi
  export QUARTO_BIN_PATH=$SCRIPT_PATH
  export QUARTO_SHARE_PATH="`cd "$SCRIPT_PATH/../../../src/resources/";pwd`"
  export QUARTO_DEBUG=true

  # Check for deno update
  QUARTO_SRC_DIR=$QUARTO_BIN_PATH/../../..
  QUARTO_DIST_CONFIG=$QUARTO_BIN_PATH/../config
  mkdir -p $QUARTO_DIST_CONFIG
  DENO_VERSION_FILE=$QUARTO_DIST_CONFIG/deno-version
  DENO_SOURCE_VERSION=`(cd $QUARTO_SRC_DIR && source ./configuration && echo $DENO)`
  if [ -f "$DENO_VERSION_FILE" ]; then
    DENO_INSTALLED_VERSION=`cat $DENO_VERSION_FILE`
    if [ "$DENO_SOURCE_VERSION" != "$DENO_INSTALLED_VERSION" ]; then
      # configure will call back into this script so we need to update the
      # version so that the check will pass next time through
      echo $DENO_SOURCE_VERSION > $DENO_VERSION_FILE
      if [ "$(uname)" = "Darwin" ]; then
        (cd $QUARTO_SRC_DIR && ./configure-macos.sh)
      else
        (cd $QUARTO_SRC_DIR && ./configure-linux.sh)
      fi
      echo ""
      printf "\\033[0;31mQuarto required reconfiguration to install Deno ${DENO_SOURCE_VERSION}. Please try command again.\\033[0m\n\n"
      exit 1
    fi
  else
    echo $DENO_SOURCE_VERSION > $DENO_VERSION_FILE
  fi

  if [ "$1" == "--version" ]; then
    echo "99.9.9"
    exit 0
  fi

else 
  
  QUARTO_ACTION=run
  QUARTO_TARGET=${SCRIPT_PATH}/quarto.js
  export QUARTO_BIN_PATH=$SCRIPT_PATH


  # If Quarto is bundled into an `.app` file, it will be looking for the 
  # share directory over in the resources folder.
  if [[ $SCRIPT_PATH == *"/Contents/MacOS/quarto/bin" ]]; then
    export QUARTO_SHARE_PATH="`cd "$SCRIPT_PATH/../../../Resources/quarto/share";pwd`"
  elif [[ $SCRIPT_PATH == *"/usr/local/bin/quarto" ]]; then
    export QUARTO_SHARE_PATH="`cd "$SCRIPT_PATH/../../share/quarto";pwd`"
  else
    export QUARTO_SHARE_PATH="`cd "$SCRIPT_PATH/../share";pwd`"
  fi

  if [ "$1" == "--version" ]; then
    echo `cat "$QUARTO_SHARE_PATH/version"`
    exit 0
  fi

fi

if [ "$1" == "--paths" ]; then
  echo "$QUARTO_BIN_PATH"
  echo "$QUARTO_SHARE_PATH"
  exit 0
fi

if [ "$(uname)" = "Darwin" ]; then
  export DENO_DOM_PLUGIN=$QUARTO_BIN_PATH/deno_dom/libplugin.dylib
else
  export DENO_DOM_PLUGIN=$QUARTO_BIN_PATH/deno_dom/libplugin.so
fi

QUARTO_DENO_OPTIONS="--unstable --allow-read --allow-write --allow-run --allow-env --allow-net --allow-ffi"

${SCRIPT_PATH}/deno ${QUARTO_ACTION} ${QUARTO_DENO_OPTIONS} ${QUARTO_DENO_EXTRA_OPTIONS} ${QUARTO_IMPORT_ARGMAP} ${QUARTO_TARGET} "$@"
