#!/usr/bin/env bash
# Script to setup devon-ide environment. Will actually be sourced,
# hash bang only for filetype detection and editor syntax support

function doLoadProperties() {
  if ! [ -f "${1}" ]
  then
    return
  fi
  local key
  local value
  local export_key
  while IFS='=' read -r key value
  do
    if [ -n "${key}" ] && [ "${key:0:1}" != "#" ]
    then
      export_key=""
      if [ "${value:0:1}" = "~" ]
      then
        value=${HOME}${value:1}
      fi
      if [ "${key:0:7}" = "export " ]
      then
        export_key="true"
        key="${key:7}"
      fi
      if [ -z "${value}" ]
      then
        unset "${key}"
      else
        if [ "${value:0:1}" != "(" ]
        then
          value=\"${value}\"
        fi
        eval "${key}=${value}"
        if [ "${export_key}" = "true" ]
        then
          # shellcheck disable=SC2086
          export ${key?}
        fi
      fi
    fi
  done < <(tr -d '\r' < "${1}")
}

# determine proper home directory
DEVON_HOME_DIR=~
if [ "${OSTYPE}" = "cygwin" ]
then
  DEVON_HOME_DIR="$(cygpath "${USERPROFILE}")"
fi
# load and setup configuration
doLoadProperties "${DEVON_IDE_HOME}/scripts/devon.properties"
doLoadProperties "${DEVON_HOME_DIR}/devon.properties"
doLoadProperties "${DEVON_IDE_HOME}/devon.properties"
doLoadProperties "${SETTINGS_PATH}/devon.properties"
if [ -z "${WORKSPACE}" ]
then
  WORKSPACE=main
fi
export WORKSPACE
export WORKSPACE_PATH=${DEVON_IDE_HOME}/workspaces/${WORKSPACE}
if [ ! -d "${WORKSPACE_PATH}" ]
then
  if [ "${WORKSPACE}" = "main" ]
  then
    echo "Creating main workspace directory"
    mkdir -p "${WORKSPACE_PATH}"
  else
    echo "WARNING: Worksapce ${WORKSPACE} does not exist"
  fi
fi
doLoadProperties "${WORKSPACE_PATH}/devon.properties"

if [ -z "${DEVON_OLD_PATH}" ]
then
  export DEVON_OLD_PATH="${PATH}"
else
  if [[ "${DEVON_OLD_PATH}" == *"C:\\WINDOWS"* ]]
  then
    # https://github.com/devonfw/devon-ide/issues/49
    DEVON_OLD_PATH=
    IFS=':'
    set -f
    for dir in ${PATH}
    do
      if [[ "${dir}" != "${DEVON_IDE_HOME}"* ]]
      then
        if [ -z "${DEVON_OLD_PATH}" ]
        then
          DEVON_OLD_PATH="${dir}"
        else
          DEVON_OLD_PATH="${DEVON_OLD_PATH}:${dir}"
        fi
      fi
    done
    set +f
    unset IFS
    export DEVON_OLD_PATH
  fi
  PATH="${DEVON_OLD_PATH}"
fi
# Setup path
SOFTWARE_PATH=${DEVON_IDE_HOME}/software
for SOFTWARE_FOLDER in "${SOFTWARE_PATH}"/*
do
  if [ -d "${SOFTWARE_FOLDER}/bin" ]
  then
    PATH="${SOFTWARE_FOLDER}/bin:${PATH}"
  else
    PATH="${SOFTWARE_FOLDER}:${PATH}"
  fi
  # Load custom configuration of software
  if [ -e "${SOFTWARE_FOLDER}/ide-config" ]
  then
    # shellcheck disable=SC1090
    source "${SOFTWARE_FOLDER}/ide-config"
  fi
done

# node.js support
if [ -d "${DEVON_IDE_HOME}/software/nodejs" ]
then
  PATH="${PATH}:$(npm bin -g 2> /dev/null)"
fi

# load user settings late so variables like M2_REPO can be overriden
doLoadProperties "${DEVON_IDE_HOME}/conf/devon.properties"

unset -f doLoadProperties

export PATH
