#!/bin/sh

# Copyright (c) 2017, 2021 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.


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

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

declare -A IQNS
declare -A OCIDS
declare -A COMPS
declare COMMAND
declare cmd_line=""
declare -i post_operation_show=0

# former implementation of oci-iscsi-config was mixing options and sub-commands.
# oci-iscsi-config-main.py now expect proper command line like '<sub-command> <options>'
# translate the command line accordingly (as expected by new implemetation)
# if first token do not start by '-' we consider that command line is following the new format

if [ "${1:0:1}" != '-' ]
then
   # execute as is as the command line do not start with an option dash-<something>
   exec ${_PY3} ${s_dir}/oci-iscsi-config-main.py $@
fi

PARSED_ARGUMENTS=$(getopt -a  --quiet -o sC:iAd:a:c:yf --long show,compartment:,interactive,all,detach:,attach:,create-volume:,destroy-volume:,volume-name:,username:,password:,help,yes,attach-volume,force -- "$@")
eval set -- "$PARSED_ARGUMENTS"
while :
do
    case "$1" in
        -s | --show)          if [ -z ${COMMAND} ]
                              then
                                COMMAND="show"
                                cmd_line="${cmd_line} --compat"
                              else
                                # display information after operation. Used to be an option
                                # change thiso to call ourself again with the show command
                                post_operation_show=1
                              fi
                              shift
                              ;;
        -C | --compartment)   COMMAND="show"     ; COMPS[${COMMAND}]+="$2,"         ; shift 2 ;;
        -i | --interactive)   cmd_line="${cmd_line} --interactive"                  ; shift   ;;
        -A | --all)           cmd_line="${cmd_line} --all"                          ; shift   ;;
        -f | --force)         cmd_line="${cmd_line} --force"                        ; shift   ;;
        -y | --yes)           cmd_line="${cmd_line} --yes"                          ; shift   ;;
        --attach-volume)      cmd_line="${cmd_line} --attach-volume"                ; shift   ;;
        -d | --detach)        COMMAND="detach"   ; IQNS[${COMMAND}]+="$2,"          ; shift 2 ;;
        -a | --attach)        COMMAND="attach"   ; IQNS[${COMMAND}]+="$2,"          ; shift 2 ;;
        -c | --create-volume) COMMAND="create"   ; cmd_line="${cmd_line} --size=$2" ; shift 2 ;;
        --destroy-volume)     COMMAND="destroy"  ; OCIDS[${COMMAND}]+="$2,"         ; shift 2 ;;
        --volume-name)        cmd_line="${cmd_line} --volume-name=$2"               ; shift 2 ;;
        --username)           cmd_line="${cmd_line} --username=$2"                  ; shift 2 ;;
        --password)           cmd_line="${cmd_line} --password=$2"                  ; shift 2 ;;
        --help)
                              if [ -z ${COMMAND} ]
                              then
                                COMMAND="usage"
                              else
                                # not displaying the overall usage but command specific one.
                                md_line="${cmd_line} --help"
                              fi
                              break
                              ;;
        # end of parsing , everything getopt did not understood is after that , we do not care in our case
        --) shift; break ;;
        *)                    COMMAND="usage"                                       ; break   ;;
    esac
done

if [ -z "${COMMAND}" ]
then
    COMMAND="sync"
    cmd_line=""
fi

if [ ${IQNS[${COMMAND}]+_} ] &&  [ -n ${IQNS[${COMMAND}]} ]
then
    cmd_line="${cmd_line} --iqns="
    for i in "${IQNS[${COMMAND}]}"; do cmd_line="${cmd_line}$i,"; done
fi
if [ ${OCIDS[${COMMAND}]+_} ] &&  [ -n ${OCIDS[${COMMAND}]} ]
then
    cmd_line="${cmd_line} --ocids="
    for i in "${OCIDS[${COMMAND}]}"; do cmd_line="${cmd_line}$i,"; done
fi
if [ ${COMPS[${COMMAND}]+_} ] &&  [ -n ${COMPS[${COMMAND}]} ]
then
    cmd_line="${cmd_line} --compartments="
    for i in "${COMPS[${COMMAND}]}"; do cmd_line="${cmd_line}$i,"; done
fi

# apply defaults to have the same behavior as before
if [ ${COMMAND} == "create" ]
then
  cmd_line="${cmd_line} --attach-volume"
fi
if [ $post_operation_show -eq 1  ]
then
    ${_PY3} ${s_dir}/oci-iscsi-config-main.py $COMMAND ${cmd_line}
    if [ $? -eq 0 ]
    then
        exec ${_PY3} ${s_dir}/oci-iscsi-config-main.py show --compat
    fi
else
  exec ${_PY3} ${s_dir}/oci-iscsi-config-main.py $COMMAND ${cmd_line}
fi