#!/bin/bash

set -euo pipefail

print_usage()
{
    echo "USAGE: mpra-data-access-portal [OPTIONS]"
    echo
    echo "OPTIONS:"
    echo
    echo "--help       This help screen"
    echo "--self-test  Run self-check and tests"
    echo
    echo "Shiny app is started when called without options."
}

run_self_test()
{
    cd $PACKAGE_HOME

    if [[ ! -e data ]]; then
        >&2 echo "ERROR: directory data missing: $PACKAGE_HOME/data"
        exit 1
    fi

    Rscript run_tests.R
}

run_server()
{
    cd $PACKAGE_HOME

    Rscript app.R
}


if [[ $# -gt 1 ]]; then
    >&2 echo "ERROR: Invalid number of arguments: $#"
    >&2 echo
    >&2 print_usage
    exit 1
elif [[ $# -eq 1 ]]; then
    if [[ "$1" == "--help" ]]; then
        print_usage
        exit 0
    elif [[ "$1" == "--self-test" ]]; then
        run_self_test
    else
        >&2 echo "ERROR: Invalid option: $1"
        >&2 echo
        >&2 print_usage
        exit 1
    fi
else
    run_server
fi
