#!/bin/bash

set -o errexit

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

option_portable=off
option_copy=on
pargs=()
plugins=()
while [[ "$#" -gt 0 ]]; do
  case $1 in
    -portable)
    option_portable=on
    ;;
    -keep)
    option_copy=off
    ;;
    -plugin=*)
    # should be like -plugin=franko/lite-plugins/master/plugins/autowrap.lua
    plugins+=("${1#-plugin=}")
    ;;
    -global)
    option_global=on
    ;;
    -*)
    echo "error: unknown option \"$1\""
    exit 1
    ;;
    *)
    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=yes
fi

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

userdir="$(realpath "$rundir")"
builddir="${pargs[0]}"

build_pragtical () {
  echo "running meson compile"
  meson compile -C "$builddir"
}

copy_pragtical_build () {
  echo "copying pragtical executable and data"
  rm -fr "$rundir"
  mkdir -p "$bindir" "$datadir"
  if [ ! -z ${run_windows+x} ]; 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 widget; 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 "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.*; 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"
}

run_pragtical () {
  if [ ! -z ${option_global+x} ]; then
    echo "running \"pragtical ${pargs[@]:1}\""
    exec "$bindir/pragtical" "${pargs[@]:1}"
  else
    echo "running \"pragtical ${pargs[@]:1}\" with local HOME"
    if [ ! -z ${run_windows+x} ]; then
      USERPROFILE="$userdir" exec "$bindir/pragtical" "${pargs[@]:1}"
    else
      HOME="$userdir" exec "$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 == on ]; then
  build_pragtical
  copy_pragtical_build
fi
fetch_plugins "${plugins[@]}"
run_pragtical
