#!/bin/bash 

# ucampurestorage="/usr/local/bin/ucampurestorage"
ucampurestorage="/root/test/pypi_test/venv/bin/ucampurestorage"

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

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

This script eradicated the destroyed 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 20/01/2023, the files that are 7 days old are the files
whose names start with '20230113.'.


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 destroyed volumes to eradicated
   -p /path/to/destroyed/vols/          Path of the files containing the destroyed volumes info
   -n number of days                   Age of files that contain the destroyed volumes to be eradicated
   -k /path/to/secrets.json            Secret file containing URL and credential to access Pure Storage
   -f                                  If specified, user will not be prompted to confirm the volume deletieradicatedon

E.g.
   1. $0 -o by_file -r /path/to/destroyed/vols/file -n 7 -k /path/to/secrets.json
   2. $0 -o by_file -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 "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

if [ "$type" == "by_file" ] && [ ! -z "$destroyed_vols_file" ]
then
    if [ -f $destroyed_vols_file ]
    then
      file_desroyed_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 /./`
      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)
      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