#!/usr/bin/env bash
# shellcheck source=scripts/functions
source "$(dirname "${0}")"/../functions
cd "${DEVON_IDE_HOME}" || exit 255

function doSetup() {
  if [ -n "${1}" ]
  then
    doDevonCommand java setup
  fi
  if [ -n "${1}" ] || [ ! -d "${IDEA_HOME}" ]
  then
    software_version="${INTELLIJ_VERSION:-2019.2}"
    if doIsMacOs
    then
      software_type="dmg"
    elif doIsWindows
    then
      software_type="win.zip"
    else
      software_type="tar.gz"
    fi
    #mirror="https://mirror.math.princeton.edu"
    #mirror="https://ftp.osuosl.org"
    download_url="https://download-cf.jetbrains.com/idea/ideaIC-${software_version}.${software_type}"
    doInstall "${IDEA_HOME}" "${download_url}" "intellij" "${software_version}"
    if [ "${?}" = 0 ] && doIsMacOs
    then
      echo "Doing workarounds for MacOS quirks..."
      mv "${DEVON_IDE_HOME}/software/intellij/IntelliJ IDEA CE.app" "${DEVON_IDE_HOME}/software/intellij/IntelliJ.app"
      echo -e "#!/usr/bin/env bash\n'${DEVON_IDE_HOME}/software/intellij/IntelliJ.app/Contents/MacOS/idea' \$@" > "${IDEA_HOME}/idea"
      chmod a+x "${IDEA_HOME}/idea"
    fi
  fi
  if [ -n "${1}" ]
  then
    doRunCommand "command -v idea" "verify installation of IntelliJ"
    if [ -e "${IDEA_HOME}/product-info.json" ]
    then
      doRunCommand "cat '${IDEA_HOME}/product-info.json'"
    else
      doRunCommand "cat '${IDEA_HOME}/IntelliJ.app/Contents/Resources/product-info.json'"
    fi
  fi
}

function doConfigureIntellij() {
  local mode="${1}"
  if [ ! -d "${WORKSPACE_PATH}/.idea" ]
  then
    if [ -z "${mode}" ]
    then
      mode="-u"
    elif [ "${mode}" != "-u" ]
    then
      doFail "Workspace ${WORKSPACE} is not initialized.\nReverse merge is not possible."
    fi
  fi
  if [ -n "${mode}" ]
  then
    doConfigureWorkspace "${SETTINGS_PATH}/intellij/workspace" "${WORKSPACE_PATH}" ${mode}
  fi
}

function doStartIntellij() {
  doConfigureIntellij "-u"
  echo "launching IntelliJ..."
  if doIsMacOs
  then
    open "${IDEA_HOME}/idea" --args "${@}"
  else
    "${IDEA_HOME}/idea" "${@}" &
  fi
}

# CLI
if [ "${1}" = "-h" ] || [ "${1}" = "help" ]
then
  echo "Manage IntelliJ IDE and workspace."
  echo
  echo "Arguments:"
  echo " --all              if provided as first arg then to command will be invoked for each workspace"
  echo " setup              setup IntelliJ (install or update)"
  echo " run | start        launch IntelliJ IDE (default if no argument is given)"
  echo " ws-up[date]        update IntelliJ workspace"
  echo " ws-re[verse]       reverse merge changes from workspace into settings"
  echo " ws-reverse-add     reverse merge adding new properties"
  echo " create-script      create intellij-${WORKSPACE} script if not already exists"
  exit
fi
IDEA_HOME="${DEVON_IDE_HOME}/software/intellij"
if [ -z "${1}" ]
then
  doSetup
  doStartIntellij
fi
if [ "${1}" = "--all" ]
then
  shift
  doDevonCommandAllWorkspaces intellij "${@}"
fi
while [ -n "${1}" ]
do
  if [ "${1}" = "run" ] || [ "${1}" = "start" ]
  then
    shift
    doStartIntellij "${@}"
  elif [ "${1}" = "ws-up" ] || [ "${1}" = "ws-update" ]
  then
    doConfigureEclipse "-u"
  elif [ "${1}" = "ws-re" ] || [ "${1}" = "ws-reverse" ]
  then
    doConfigureEclipse "-i"
  elif [ "${1}" = "ws-reverse-add" ]
  then
    doConfigureEclipse "-x"
  elif [ "${1}" = "setup" ]
  then
    doSetup setup
    doCreateIdeScript intellij
  elif [ "${1}" = "create-script" ]
  then
    doCreateIdeScript intellij
  else
    doFail "Unknown argument: ${1}"
  fi
  shift
done
