#!/bin/bash
#######################################################################################
#
# This scripts reads in arguments from a config file and/or the command line to be
# passed into fetch-single-dependency.sh.
#
# The arguments may be stored in a config file at ${HOME}/nxm_dependency_config.txt,
# each separated by a pipe "|".
# However, these arguments may also be overridden on the command line.
#
# This script takes in user credentials, along with the Nexus url, repository, and a
# list of option trees as a comma-separated list. For each option tree listed, that
# option tree's dependencies are read from ${NMROOT}/nxm/<opt>/jars/<opt>_dependencies.txt,
# and each dependency gets pulled.
#
# Note:
#   Original script in:
#   http://blog.sonatype.com/2011/01/downloading-artifacts-from-nexus-with-bash/#.VPW0c1PF_v9
#   Adapted for Nexus 3
#
# Note:
#   Original script altered by Zachary Matthews
#
# Since: NextMidas 4.1.4
#
#######################################################################################

NMROOT_TMP=`realpath ${0}`
for i in {1..2}
do
  NMROOT_TMP=`dirname ${NMROOT_TMP}`
done
NMROOT=${NMROOT_TMP}

usage()
{
cat <<EOF
usage: ${0} options
This script will fetch an artifact from a Nexus server using the Nexus REST redirect service.
OPTIONS:
   -h    Show this message
   -d    Print debug information
   -m    Automatically call script to move dependencies after they've been fetched
   -r    Repository
   -k    SSL Key
   -s    SSL Certificate
   -u    Username
   -p    Password
   -n    Nexus Base URL
   -t    List of NeXtMidas option trees
   -l    Alternative locations for external option trees (i.e. opt1,dir1,opt2,dir2)
EOF
}

# Read in Complete Set of parameters
DEBUG=
MOVE_DEPENDENCIES=false
REPO=
SSL_KEY=
SSL_CRT=
USERNAME=
PASSWORD=
NEXUS_URL=
NXM_OPTS_LIST=all
NXM_OPTS=
ALT_LOCATIONS_LIST=

# Check for config file
NEXUS_FETCH_INPUT=
NXM_DEPENDENCIES_CONFIG=${HOME}/nxm_dependencies_config.txt
if [[ -f ${NXM_DEPENDENCIES_CONFIG} ]] ; then
  echo ""
  while IFS= read -r line; do
    OIFS=${IFS}
    IFS="|"
    LINEARG=( ${line} )
    IFS=${OIFS}
    case "${LINEARG[0]}" in
      repo)      REPO=${LINEARG[1]}     ; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -r ${REPO}"     ;;
      ssl_key)   SSL_KEY=${LINEARG[1]}  ; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -k ${SSL_KEY}"  ;;
      ssl_crt)   SSL_CRT=${LINEARG[1]}  ; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -s ${SSL_CRT}"  ;;
      username)  USERNAME=${LINEARG[1]} ; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -u ${USERNAME}" ;;
      password)  PASSWORD=${LINEARG[1]} ; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -p ${PASSWORD}" ;;
      nexus_url) NEXUS_URL=${LINEARG[1]}; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -n ${NEXUS_URL}";;
      opt_trees) NXM_OPTS_LIST=${LINEARG[1]};;
      alt_locations) ALT_LOCATIONS_LIST=${LINEARG[1]};;
    esac
  done < ${NXM_DEPENDENCIES_CONFIG}
fi

# if command line args are set, override settings from config file
while getopts "hdmr:k:s:u:p:n:t:l:" OPTION
do
  case ${OPTION} in
    h) usage; exit 0;;
    d) DEBUG=true;;
    m) MOVE_DEPENDENCIES=true;;
    r) REPO=${OPTARG}     ; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -r ${REPO}"     ;;
    k) SSL_KEY=${OPTARG}  ; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -k ${SSL_KEY}"  ;;
    s) SSL_CRT=${OPTARG}  ; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -s ${SSL_CRT}"  ;;
    u) USERNAME=${OPTARG} ; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -u ${USERNAME}" ;;
    p) PASSWORD=${OPTARG} ; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -p ${PASSWORD}" ;;
    n) NEXUS_URL=${OPTARG}; NEXUS_FETCH_INPUT="${NEXUS_FETCH_INPUT} -n ${NEXUS_URL}";;
    t) NXM_OPTS_LIST=${OPTARG};;
    l) ALT_LOCATIONS_LIST=${OPTARG};;
    ?) echo "Illegal argument ${OPTION}=${OPTARG}" >&2; usage; exit 1;;
  esac
done

if [[ "${NXM_OPTS_LIST}" == "all" ]] ; then
  echo ""
  echo "NXM_OPTS_LIST=all, setting it to sys,database..."
  NXM_OPTS_LIST=sys,database
fi

# print out debug information
if [[ ! "${DEBUG}" == "" ]] ; then
  echo ""
  echo "******* DEBUG INFORMATION *******"
  echo "MOVE_DEPENDENCIES:  ${MOVE_DEPENDENCIES}"
  echo "REPO:               ${REPO}"
  echo "SSL_KEY:            ${SSL_KEY}"
  echo "SSL_CRT:            ${SSL_CRT}"
  echo "USERNAME:           ${USERNAME}"
  echo "PASSWORD:           ${PASSWORD}"
  echo "NEXUS_URL:          ${NEXUS_URL}"
  echo "NXM_OPTS_LIST:      ${NXM_OPTS_LIST}"
  echo "ALT_LOCATIONS_LIST: ${ALT_LOCATIONS_LIST}"
  echo "*********************************"
fi

# loop through list of option trees and pull dependencies for each option tree
echo ""
pulljars_parent_dir=`realpath ${0}` ; pulljars_parent_dir=`dirname ${pulljars_parent_dir}`
OIFS=${IFS}
IFS=","
NXM_OPTS=( ${NXM_OPTS_LIST} )
if [[ "${ALT_LOCATIONS_LIST}" != "" ]] ; then
  ALT_LOCATIONS=( ${ALT_LOCATIONS_LIST} )
fi
IFS=${OIFS}
for opt in "${NXM_OPTS[@]}"
do
  OPT_DEPENDENCIES=${NMROOT}/nxm/${opt}/jars/${opt}_dependencies.txt
  for idx in "${!ALT_LOCATIONS[@]}"
  do
    if [[ "${ALT_LOCATIONS[${idx}]}" == "${opt}" ]] ; then
      OPT_DEPENDENCIES=${ALT_LOCATIONS[${idx+1}]}/${opt}_dependencies.txt
      break
    fi
  done

  while IFS= read -r gav; do
    ${pulljars_parent_dir}/fetch-single-dependency.sh ${NEXUS_FETCH_INPUT} -a ${gav} -t ${opt}
  done < ${OPT_DEPENDENCIES}
done

if [[ "${MOVE_DEPENDENCIES}" == "true" ]] ; then
  if [[ "${ALT_LOCATIONS_LIST}" != "" ]] ; then
    ${pulljars_parent_dir}/move-fetched-dependencies.sh -l ${ALT_LOCATIONS_LIST}
  else
    ${pulljars_parent_dir}/move-fetched-dependencies.sh
  fi
fi
