#!/bin/bash

# Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
# Licensed under the Universal Permissive License v 1.0 as shown at
# http://oss.oracle.com/licenses/upl.



# utility assists with configuring iscsi storage on Oracle Cloud
# Infrastructure instances.  See the manual page for more information.


_PY3=/usr/bin/python3.6
_PY_CMD=oci_notify_main.py
s_dir=`${_PY3} -c 'import os.path ; import oci_utils.impl ; print (os.path.dirname(oci_utils.impl.__file__))' 2>/dev/null`

declare COMMAND
declare -A TOPICOCID
declare -A MSGDATA
declare -A MSGTITLE
declare notify_exec="${s_dir}/${_PY_CMD}"

if [ "${1:0:1}" != '-' ]
then
   # execute as is as the command line do not start with an option dash-<something>
   exec ${_PY3} $notify_exec "$@"
   exit 0
fi

PARSED_ARGUMENTS=$(getopt --alternative  --quiet --options c:t:f:h --longoptions config:,title:,file:,help -- "$@")
eval set -- "$PARSED_ARGUMENTS"

while :
do
  case "$1" in
    -c|--config)
      COMMAND='config';  TOPICOCID=${2}; shift 2
    ;;
    -t|--title)
      COMMAND='message'; MSGTITLE=${2};  shift 2
    ;;
    -f|--file)
      COMMAND='message'; MSGDATA=${2};   shift 2
    ;;
    -h|--help)
      COMMAND='--help'; shift
    ;;
    --)
      shift; break
    ;;
    *)
      COMMAND='--help'
    ;;
  esac
done

if [ ${COMMAND} == '--help' ]; then
  exec ${_PY3} ${notify_exec} --help
elif [ -z "${TOPICOCID+x}" ]; then
  # no topic, must be message.
  if [ -z "${MSGTITLE+x}" -o -z "${MSGDATA+x}" ]; then
    exec ${_PY3} ${notify_exec} --help
  else
    exec ${_PY3} ${notify_exec} message --title "${MSGTITLE}" --file "${MSGDATA}"
  fi
else
  # topic is defined, must be config
  exec ${_PY3} ${notify_exec} config "$TOPICOCID"
fi
exit 0
