#!/bin/bash

os_level=`cat /etc/os-release |grep REDHAT_SUPPORT_PRODUCT_VERSION |awk -F= '{print $2}' |sed s/\"//g |awk -F. '{print $1}'`
python38="/opt/rh/rh-python38"
if [[ "$os_level" == 7 ]]
then
  if [[ -d  "$python38" ]]
  then
    ucampurestorage="/opt/rh/rh-python38/root/usr/local/bin/ucampurestorage"
  else
    echo "Python 3.8 is not installed as per the recommendatations"
  fi
else
ucampurestorage="/usr/local/bin/ucampurestorage"
fi

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

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

This script eradicates a Pure storage volume.  I.e. bypasses destroying 
(sending to the "recycle bin").
This will likely return an error when SafeMode is enabled on the Pure storage.

Restrictions for use:
  * The volume must be a Pure storage volume
  * The volume must be unmapped

OPTIONS:
  -h                          Show this message
  -n volume_name              Name of the volume to be eradicated
  -k /path/to/secrets.json    Secrets file containing URL and credentials to
                              access the Pure storage

E.g.
$0 -n TEST123 -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:k:" opt
do
  case $opt in
    h)
      usage
      exit
      ;;
    n)
      name=$OPTARG
     ;;
    k)
      secrets=$OPTARG
     ;;
    ?)
      usage
      exit
      ;;
  esac
done

# check required arguments
if [ -z "$name" ] || [ -z "$secrets" ]
then
   usage
   exit
fi

# Call ucampurestorage package
result=$($ucampurestorage --file $secrets volume eradicate --name $name -nop)
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
