#!/bin/bash
###############################################################################
# IBM (C) Copyright 2017 Eclipse Public License                               #
# http://www.eclipse.org/org/documents/epl-v10.html                           #
###############################################################################
# COMPONENT: offlinediskanddetach                                             #
#                                                                             #
# Offline a disk and detaches it                                              #
#                                                                             #
# Uses routines in zhcpshellutils                                             #
###############################################################################


source /opt/zhcp/lib/zhcpshellutils
version="1.0"

###############################################################################
### FUNCTIONS #################################################################
###############################################################################

function printCMDUsage {
  : SOURCE: ${BASH_SOURCE}
  : STACK:  ${FUNCNAME[@]}
  # @Description:
  #   Prints usage help text using a list generated by previous requests made
  #   to parse the command-line argument list.
  # @Override in zhcpshellutils
  # @Code:
  printf "USAGE: $CMDNAME [OPTIONS] USERID VDEV\n"

  printf "${optionHelp}\n"

} #printCMDUsage{}

###############################################################################

function printCMDDescription {
  : SOURCE: ${BASH_SOURCE}
  : STACK:  ${FUNCNAME[@]}
  # @Description:
  #   Prints a short description of this command.
  # @Overrides:
  #   printCMDDescription{} in "xcatshellutils".
  # @Code:
  printf "Looks up the userid and vdev in table, offlines the local linked "
  printf "disk and detaches the disk.\n"
} #printCMDDescription{}

###############################################################################

function parseArgs {
  : SOURCE: ${BASH_SOURCE}
  : STACK:  ${FUNCNAME[@]}
  # @Description:
  #   Parses and checks command-line arguments.
  # @Code:
  # Apply any defaults before handling operands

  # Non-local variables in this function are intentionally non-local.
  isOption -h --help '     Print this help message.'   && printHelp='true'
  isOption -V --version '  Print the version number of this script.'   && printVersion='true'

  # Handle options that provide info but don't deal with locks or disks
  if [[ $printVersion ]]; then
    printf "Version: $version\n\n"
  fi

  if [[ $printHelp ]]; then
    printHelp
  fi

  if [[ $printHelp || $printVersion  ]]; then
    exit 0
  fi

  # Get input parms we need
  getPositionalArg 1 userID
  getPositionalArg 2 vdev

  if [[ -z "$userID" ]]; then
      printf 'ERROR: Missing userid parameter.'
      printCMDUsage
      exit 1
  fi

  if [[ -z "$vdev" ]]; then
      printf 'ERROR: Missing virtual device address parameter.'
      printCMDUsage
      exit 1
  fi

  local badOptions=$(getBadOptions)
  if [[ $badOptions ]]; then
    printf "ERROR: ${badOptions}"
    printCMDUsage
    exit 1
  fi

} #parseArgs{}


###############################################################################

function offlineDetachDisk {
  : SOURCE: ${BASH_SOURCE}
  : STACK:  ${FUNCNAME[@]}
  # @Description:
  #   Calls zhcpshellutils to offline the disk and detach it
  #
  # @Returns:
  #   0 if the offline and detach was successful.
  #   1..4 if there was an error with disconnectDisk.
  #
  # @Parameters:
  #   set by parseArgs: userID, vdev
  #
  # @Code:
  #

  local rc
  local output
  local linkedAsVdev
  local deviceName
  output=$(disconnectDisk $userID $vdev 0)
  rc=$?

  if [[ $rc -gt 0 ]]; then
      printError "Failed to disconnect disk: ${userID}:${vdev} rc: ${rc}"
      printf "${output}\n"
      exit $rc
  fi

  printf "Success: Userid ${userID} vdev ${vdev} unlinked\n"

} #offlineDetachDisk{}

###############################################################################
### START EXECUTION ###########################################################
###############################################################################
timestamp=$(date -u --rfc-3339=ns | sed 's/ /-/;s/\.\(...\).*/.\1/')
inform "offlinediskanddetach ${args} start time: ${timestamp}"

parseArgs
offlineDetachDisk

timestamp=$(date -u --rfc-3339=ns | sed 's/ /-/;s/\.\(...\).*/.\1/')
inform "offlinediskanddetach exit time: ${timestamp}"
exit 0
###############################################################################
### END OF SCRIPT #############################################################
###############################################################################
