#!/bin/bash

set -e

# accept argument in the form
github_raw_content () {
  echo "https://raw.githubusercontent.com/$1"
}

option_debug=false
option_portable=false
option_copy=true
option_global=false
run_windows=false
pargs=()
plugins=()
while [[ "$#" -gt 0 ]]; do
  case $1 in
    -debug)
      option_debug=true
      set -x
      ;;
    -portable)
      option_portable=true
      ;;
    -keep)
      option_copy=false
      ;;
    -plugin=*)
      # should be like -plugin=franko/lite-plugins/master/plugins/autowrap.lua
      plugins+=("${1#-plugin=}")
      ;;
    -global)
      option_global=true
      ;;
    -*)
      if [ "${#pargs[@]}" = "0" ]; then
        echo "error: unknown option \"$1\""
        exit 1
      else
        pargs+=("$1")
      fi
      ;;
    *)
      pargs+=("$1")
    ;;
  esac
  shift
done

if [ "${#pargs[@]}" -lt 1 ]; then
  echo "usage: $0 [options] <build-dir>"
  exit 1
fi

if [[ "$OSTYPE" == "msys"* || "$OSTYPE" == "mingw"* ]]; then
  run_windows=true
fi

rundir=".run"
if [[ $option_portable == true ]]; then
  bindir="$rundir"
  datadir="$rundir/data"
else
  bindir="$rundir/bin"
  datadir="$rundir/share/pragtical"
fi

# on macOS we need `brew install coreutils` for grealpath
if command -v grealpath >/dev/null 2>&1; then
  userdir="$(grealpath "$rundir")"
else
  userdir="$(realpath "$rundir")"
fi
builddir="${pargs[0]}"

build_pragtical () {
  if [ -e "$builddir/.muon" ]; then
    echo "running muon samu"
    muon samu -C "$builddir"
  else
    echo "running meson compile"
    meson compile -C "$builddir"
  fi
}

copy_pragtical_build () {
  echo "copying pragtical executable and data"
  rm -fr "$rundir"
  mkdir -p "$bindir" "$datadir"
  if [[ $run_windows == true ]]; then
    cp "$builddir/src/pragtical.exe" "$bindir"
  else
    cp "$builddir/src/pragtical" "$bindir"
  fi
  mkdir -p "$datadir/core"
  for module_name in core compat plugins colors fonts; do
    cp -r "data/$module_name" "$datadir"
  done
  if [ -d "subprojects/widget" ]; then
    cp -a "subprojects/widget" "$datadir"
  fi
  if [ -d "subprojects/colors" ]; then
    cp -a "subprojects/colors/colors" "$datadir"
  fi
  if [ -d "subprojects/plugins" ]; then
    cp -a "subprojects/plugins/plugins/"language_* "$datadir/plugins"
  fi
  if [ -d "$builddir/subprojects/luajit/src" ] && [ -d "subprojects/luajit/src/jit" ]; then
    mkdir -p "$datadir/jit"
    cp -a "subprojects/luajit/src/jit/." "$datadir/jit"
    if [ -f "$builddir/subprojects/luajit/src/vmdef.lua" ]; then
      cp -a "$builddir/subprojects/luajit/src/vmdef.lua" "$datadir/jit"
    fi
  fi
  if [ -d "subprojects/ppm" ]; then
    if [ -d "$builddir/subprojects/ppm" ] && ls "$builddir"/subprojects/ppm/ppm.*; then
      cp -a "subprojects/ppm/plugins/plugin_manager" "$datadir/plugins"
      cp -a "$builddir"/subprojects/ppm/ppm.* "$datadir/plugins/plugin_manager/"
    elif ls subprojects/ppm/ppm.* | grep -v ppm.json; then
      cp -a "subprojects/ppm/plugins/plugin_manager" "$datadir/plugins"
      cp subprojects/ppm/ppm.* "$datadir/plugins/plugin_manager/"
    fi
    if [ -d "$datadir/plugins/plugin_manager" ]; then
      mkdir "$datadir/libraries"
      cp -a "subprojects/ppm/libraries/json.lua" "$datadir/libraries"
    fi
  fi
  # The start.lua file is generated by meson in $builddir but
  # there is already a start.lua file in data/core so the command below
  # should be executed after we copy the data/core directory.
  cp "$builddir/start.lua" "$datadir/core"
  # For ci purposes we also copy datadir to bindir on macOS
  if [[ "$OSTYPE" == "darwin"* ]]; then
    cp -a "$datadir/"* "$bindir"
  fi
}

run_pragtical () {
  if [[ $option_debug == true ]]; then
    echo "LLVM_PROFILE_FILE: $LLVM_PROFILE_FILE"
    echo "SDL_VIDEO_DRIVER: $SDL_VIDEO_DRIVER"
  fi
  if [[ $option_global == true ]]; then
    echo "running \"pragtical ${pargs[@]:1}\""
    exec "$bindir/pragtical" "${pargs[@]:1}"
  else
    echo "running \"pragtical ${pargs[@]:1}\" with local HOME"
    if [[ $run_windows == true ]]; then
      PRAGTICAL_SCALE=1 USERPROFILE="$userdir" ./$bindir/pragtical "${pargs[@]:1}"
    else
      PRAGTICAL_SCALE=1 PRAGTICAL_USERDIR="$userdir" ./$bindir/pragtical "${pargs[@]:1}"
    fi
  fi
}

github_raw_content () {
  echo "https://raw.githubusercontent.com/$1"
}

fetch_plugins () {
  for name in "$@"; do
    local url="$(github_raw_content "$name")"
    local modname="${url##*/}"
    if [ "$modname" == init.lua ]; then
      local m1="${name#*/}"
      modname="${m1%%/*}.lua"
    fi
    echo "installed $name as $modname from $url"
    curl --insecure -L "$url" -o "$datadir/plugins/${modname}"
  done
}

if [[ $option_copy == true ]]; then
  build_pragtical
  copy_pragtical_build
fi
fetch_plugins "${plugins[@]}"
run_pragtical
