#!/bin/bash

###################################################################
# do_unmap_purestorage_volume
# Purpose: Unmap of PureStorage volume 
#
#    Args: $1 -> name of volume to delete
#
#  Output: Log of actions
# Returns: TRUE for success, FALSE for failure
###################################################################
ucampurestorage="/usr/local/bin/ucampurestorage"

echo "Invoked with: $0 $@"
echo "My PID is $$"

###################################################################
# Purpose: usage information
###################################################################
function usage()
{
cat << EOF
usage: $0 options

This script unmaps a purestorage volume.

OPTIONS:
  -h 
  -n name                             Name of the volume.
  -w wwn                              WWN of the volume.
  -p mount point                      mountpoint on the local machine.
  -k /path/to/secrets.json            Secret file containing URL and credential to access Pure Storage.

E.g.
  1. $0 -n Test123 -k /path/to/secrets.json
  2. $0 -w 123455 -k /path/to/secrets.json
  3. $0 -p /t123 -k /path/to/secrets.json

NB. 
1. /path/to/secrets.json must be in the following format:
{
  "client_id": "pure_api_client_id",
  "key_id": "pure_api_key_id",
  "client_name": "pure_api_client_name",
  "storage": "purestorage.cam.ac.uk",
  "user": "pureuser",
  "password": "purepassword",
  "keyfile": "private.pem"
}

2. ucampurestorage must be installed and located under /usr/local/bin/
EOF
}

###################################################################
# Parse the args 
###################################################################

while getopts "hn:w:p:k:" opt
do
  case $opt in
    h)
      usage
      exit
      ;;
    n)
      name=$OPTARG
     ;;
    w)
      wwn=$OPTARG
     ;;
    p)
      mountpoint=$OPTARG
     ;;
    k)
      secrets=$OPTARG
     ;;
    ?)
      usage
      exit
      ;;
  esac
done

# check required arguments
if [ -z "$secrets" ]
then
   usage
   exit
fi
if [[ ("$name" && "$wwn") || ("$mountpoint" && "$wwn") || ("$name" && "$mountpoint") || ("$name" && "$mountpoint" &&"$wwn") || (-z "$name" && -z "$mountpoint" && -z "$wwn")]]
then
   usage
   exit 
fi


# Call ucampurestorage package
result=$($ucampurestorage  --record_config True --file $secrets volume unmap --name $name)
if [[ $result == *"[Command succeeded - Returns True]"* ]]
then
    echo "Command succeeded."
    exit 0
else
  echo "Command failed. Please check the ucampurestorage log file in /var/log/ucampurestorage/."
  exit 1
fi