#!/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 maps and mounts a pre-existing Pure storage volume and optionally 
formats it.

Restrictions/notes for use:
  * The mount point to be used must already exist
  * Formatting the volume is done with gdisk to create an ext4 partition

OPTIONS:
  -h                          Show this message
  -n volume_name              Name of the volume to be mapped
  -p mount_point              Mount point for the volume (must already exist)
  -x [0,1]                    Flag for new volume mounting to format before 
                              mount (0=do not format, 1=format)
                              CAUTION: Do not use "-x 1" when mapping/mounting 
                              cloned volumes or you will lose data!
  -k /path/to/secrets.json    Secrets file containing URL and credentials to 
                              access the Pure storage

E.g.
For a new volume: $0 -n TEST121 -p /t1 -x 1 -k /path/to/secrets.json
For clone volume: $0 -n TEST121 -p /t1 -x 0 -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:p:x:k:" opt
do
  case $opt in
    h)
      usage
      exit
      ;;
    n)
      name=$OPTARG
     ;;
    p)
      mount=$OPTARG
     ;;
    x)
      new=$OPTARG
     ;;
    k)
      secrets=$OPTARG
     ;;
    ?)
      usage
      exit
      ;;
  esac
done

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

# Call ucampurestorage package
if [[ $new == 1 ]]
then
    result=$($ucampurestorage --record_config True --file $secrets volume map --name $name --mp $mount -new )
elif [[ $new == 0 ]]
then
    result=$($ucampurestorage --record_config True --file $secrets volume map --name $name --mp $mount )
else
   usage
   exit 1
fi

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
