#!/bin/bash

# -----------------------------------------------------------------------------
# 0. Configurations for local

# Set directory of this script.
_DIR_="$(cd "$(dirname "${BASH_SOURCE:-$0}")"; pwd)"

# Devise name which are candidates of MLF_SYS_NAME
DEVNAMES=("AMR" "HPN" "SAS" "SIK")

# -----------------------------------------------------------------------------
# Functions
# Show error dialog about beamline settings and exit process with status 1.
function exit_on_error_for_beamline_settings () {
    message="Illegal beamline code has been specified."
    python3 "${UTIL_PATH}/PySide_DlgSetUtsusemiEnv.py" --alert --message="${message}"
    exit 1
}
# Show error dialog about user settings and exit process with status 1.
function exit_on_error_for_user_settings () {
    message="Environment variables for Utsusemi should be specified."
    python3 "${UTIL_PATH}/PySide_DlgSetUtsusemiEnv.py" --alert --message="${message}"
    exit 1
}
# Check array contains key
#   Usage:
#       contains ${array[@]} key
#   Returns:
#       If array contains key, echos "y" and returns 0.
#       Otherwise array contains key, echos "n" and returns 1.
function contains() {
    local n=$#
    local value=${!n}
    for ((i=1;i < $#;i++)) {
        if [ "${!i}" == "${value}" ]; then
            echo "y"
            return 0
        fi
    }
    echo "n"
    return 1
}
# -----------------------------------------------------------------------------
# 1. Load constants
#   The constants are the file path which stores user configured variables like
#   beam-line code (UTSUSEMI_INST_CODE) or user data directory (UTSUSEMI_DATA_DIR
#   and UTSUSEMI_USR_DIR).
source "${_DIR_}/_consts"


# -----------------------------------------------------------------------------
# 2. Execute first launching script

if [ -f "${_DIR_}/_first_launch" ]; then
	echo FIRST LAUNCH
    .  "${_DIR_}/_first_launch"
    mv -f "${_DIR_}/_first_launch" "${_DIR_}/_first_launch_done"
fi


# -----------------------------------------------------------------------------
# 3. Set up the environment variable for Manyo-Lib and Python packages

source "${_DIR_}/_setEnvManyo"

export PYROOT="${MLFSOFT_PATH}/python"
export PYBIN="${PYROOT}/bin"
export PYLIB="${PYROOT}/lib"
export PYPKG="${PYLIB}/python3.7"

export PYTHONPATH=".:${PYPKG_PATH}:${MANYO_PATH}:${MANYO_PATH}/Manyo:${MANYO_PATH}/Manyo/Utsusemi:${MANYO_PATH}/Manyo/MLF:${UTSU0_PATH}:${UGAO0_PATH}:${UTSU_PATH}:${UGAO_PATH}:${UTIL_PATH}"


export PATH="${PYBIN}:${PATH}"


if [ "$1" == "STEP3" ]; then
    return 0
fi


# -----------------------------------------------------------------------------
# 4. Load user settings (UTSUSEMI_DATA_DIR / UTSUSEMI_USR_DIR)

# Check whether configuration file exists.
if [ ! -f "${CONFFILE_ENV}" ]; then
    # Exit when beamline selector dialog exits with stauts 1.
    exit_on_error_for_user_settings
fi

source "${CONFFILE_ENV}"

export UTSUSEMI_DATA_DIR
export UTSUSEMI_USR_DIR
export UTSUSEMI_DEBUG_MODE
export UTSUSEMI_INST_CODE
export UTSUSEMI_LOG_QUIET
export UTSUSEMI_MULTH

# Check beamline var is within a valid contents.
[ $(contains "${DEVNAMES[@]}" "${UTSUSEMI_INST_CODE}") == "n" ] && \
    exit_on_error_for_beamline_settings

# -----------------------------------------------------------------------------
# 5. Load environment variables

source "${_DIR_}/../mlfsoftrc"

clear
echo "==========================================="
echo "  Welcome to Utsusemi Environment for ${UTSUSEMI_INST_CODE}"
echo "==========================================="
