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

# $1: optional setup
function doSetup() {
  if command -v ng &> /dev/null
  then
    if [ -n "${1}" ]
    then
      echo "angluar-cli (ng) is already installed at $(command -v ng)"
    fi
  else
    doDevonCommand npm -q setup
    local npm_command="${DEVON_IDE_HOME}/software/node/npm"
    if [ ! -x "${npm_command}" ]
    then
      npm_command="${DEVON_IDE_HOME}/software/node/bin/npm"
    fi
    doRunCommand "'${npm_command}' install -g @angular/cli@latest --unsafe" "install angular-cli"
  fi
}

#CLI
if [ "${1}" = "-h" ] || [ "${1}" = "help" ]
then
  echo "Setup or run angular-cli (ng)."
  echo
  echo "Arguments:"
  echo "setup                 setup angular-cli (install and verify)"
  echo "«args»                call maven with the specified arguments"
  echo
  echo "Options:"
  exit
fi

if [ -z "${1}" ] || [ "${1}" = "setup" ]
then
  doSetup setup
elif [ "${1}" = "create" ]
then
  shift
  doSetup
  ng new "${@}"
else
  doSetup
  ng "${@}"
fi
