#!/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 the destroyed ("recycle bin") volumes listed in a file 
OR (exclusive) the destroyed volumes listed in a set of files under a given 
directory that are older than n number of days. The age of files is uniquely
determined by the date used to name them. For example, if today is 2023-01-20, 
the files that are 7 days old are the files whose names start with '20230113.'.
NOTE: This will likely fail and return an error when SafeMode is enabled on 
Pure storage.

OPTIONS:
  -h                                Show this message
  -o 'by_file' or 'by_age'          Option 1 (by_file): eradicated based on a 
                                    file. Option 2 (by_age): eradicated based 
                                    on the age of the files containing the 
                                    recycled volumes. If -o by_file:
                                    -r option should be provided, -p and -n 
                                    options will be ignored. If -o by_age,
                                    -r will be ignored and -p and -n should 
                                    be provided
  -r /path/to/destroyed/vols/file   File containing the names of the destroyed 
                                    volumes to eradicated
  -p /path/to/destroyed/vols/       Path to the files containing the destroyed 
                                    volumes information
  -n number_of_days                 Age of files that contain the destroyed 
                                    volumes to be eradicated
  -k /path/to/secrets.json          Secrets file containing URL and credentials 
                                    to access the Pure storage
  -f                                If specified, user will not be prompted to 
                                    confirm the volume eradication

E.g.
1. $0 -o by_file -r /path/to/destroyed/vols/file -k /path/to/secrets.json
2. $0 -o by_age -p /path/to/destroyed/vols -n 7 -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/
3. /path/to/destroyed/vols/file should be a file generated by the 
   script "pure_record_destroyed_volumes"
4. Lines in /path/to/destroyed/vols/file or files under 
   /path/to/destroyed/vols/ can be commented with the sign '#'
EOF
}

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

force=0

while getopts "ho:r:p:n:k:f" opt
do
  case $opt in
    h)
      usage
      exit
      ;;
    o)
      type=$OPTARG
      ;;
    r)
      destroyed_vols_file=$OPTARG
      ;;
    p)
      destroyed_vols_path=$OPTARG
      ;;
    n)
      file_age=$OPTARG
      ;;
    k)
      secrets=$OPTARG
     ;;
    f)
      force=1
    ;;
    ?)
      usage
      exit
      ;;
  esac
done

# check required arguments
if [ -z "$type" ] || [ -z "$secrets" ] || ([ "$type" != "by_file" ] && [ "$type" != "by_age" ])
then
    usage
    exit
fi

echo $destroyed_vols_file
if [ "$type" == "by_file" ] && [ ! -z "$destroyed_vols_file" ]
then
    if [ -f $destroyed_vols_file ]
    then
      file_destroyed_dev=$destroyed_vols_file
    else
      echo "File $destroyed_vols_file not found."
      exit 1
    fi
elif [ "$type" == "by_age" ] && [ ! -z "$destroyed_vols_path" ] && [ ! -z "$file_age" ]
then
    past_date=`date --date="$file_age day ago" +%Y%m%d`
    if [ -f $destroyed_vols_path/$past_date.pure_destoyed ]
    then
      file_destroyed_dev="$destroyed_vols_path/$past_date.pure_destoyed"
    else
        echo "Destroyed volumes files under $destroyed_vols_path for the date $past_date cannot be found."
        exit 1
    fi
else
    usage
    exit
fi
bad=0
if [ ! -z "$file_destroyed_dev" ]
then
    if [[ "$force" == 0 ]]
    then
      nb_vols=`cat $file_destroyed_dev  |grep ^\( | sed s/\(\//g |sed s/,//g |sed s/\'//g |awk '{print $1}' |awk /./`
      echo $nb_vols
      read -p "Do you really want to delete the $nb_vols recycled volumes listed in the file $file_destroyed_dev (Y or y for Yes)? " -n 1 -r
      echo
    else
      REPLY=Y
    fi
    if [[ $REPLY =~ ^[Yy]$ ]]
    then
      for vol_name in $nb_vols
      do
      result=$($ucampurestorage --file $secrets volume eradicate --name $vol_name -nop)
      if [[ $result == *"[Command succeeded - Returns True]"* ]]
        then
        echo "$vol_name successfully deleted!"
      else
        echo "$vol_name failed deleted!"
        bad = 1
      fi
      done
    else
      exit
    fi
fi

if [[ $bad == 0 ]]
then
    echo "Command succeeded."
    exit 0
else
    echo "Command failed. Please check the ucampurestorage log file in /var/log/ucampurestorage/."
    exit 1
fi
