#!/bin/bash
###############################################################################
# Copyright 2017 IBM Corp.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
###############################################################################
# COMPONENT: offlinediskanddetach                                             #
#                                                                             #
# Offline a disk and detaches it                                              #
#                                                                             #
# Uses routines in zthinshellutils                                             #
###############################################################################


source /opt/zthin/lib/zthinshellutils
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 zthinshellutils
  # @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 zthinshellutils 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 #############################################################
###############################################################################
