#!/bin/bash

# This file uses the method outlined in [1] to pass additional arguments to the
# `julia` executable (in this case: enable colored output). The approach should
# work portably on all UNIX-like operating systems.
#
# NOTE TO WINDOWS USERS: Just invoke `bin/trixi` explicitly with the `julia` executable.
#
# [1]: https://docs.julialang.org/en/v1/manual/faq/#How-do-I-pass-options-to-julia-using-#!/usr/bin/env?-1

#=
# Check if any command line arguments are given - if yes, run in batch
# mode, if not, start an interactive session
if [ "$#" -gt "0" ]; then
  interactive=0
else
  interactive=1
fi

# Get directory of script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
PROJECT_DIR="$DIR/pkg/Trixi2Img"

# If not interactive, just run script as usual. Otherwise load REPL
if [ $interactive -eq 0 ]; then
  exec julia --color=yes --project="$PROJECT_DIR" \
      -e 'include(popfirst!(ARGS))' "${BASH_SOURCE[0]}" "$@"
else
  exec julia --banner=no --project="$PROJECT_DIR" -i \
      -e "println(\"# Note: project directory set to '$PROJECT_DIR'. Changes to packages will only affect current project.\")" \
      -e 'println("# Execute the first line below once at the beginning of an interactive session.")' \
      -e 'println("# Start Trixi2Img by running the second line.\n")' \
      -e 'println("using Revise; push!(Revise.dont_watch_pkgs, :Plots); using Revise; Revise.silence(\"Plots\"); import Trixi2Img")' \
      -e 'println("Trixi2Img.run(datafile=\"file.h5\")")'
fi
=#

# Load Trixi2Img
import Trixi2Img

# Run Trixi2Vtu but handle user interrupts gracefully (Ctrl-c)
@Trixi2Img.interruptable Trixi2Img.run(args=ARGS)
