#!/bin/bash 

ucamdsm="/usr/local/bin/ucamdsm"

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

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

This script deletes the recycled volumes listed in a file OR (exclusive) the recycled 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/2019, the files that are 7 days old are the files
whose names start with '20190113.'.


OPTIONS:
   -h                                  Show this message
   -o 'by_file' or 'by_age'            Option 1 (by_file): deletion based on a file. Option 2 (by_age): deletion
                                       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/recycles/vols/file      File containing the recycled volumes to delete
   -p /path/to/recycled/vols/          Path of the files containing the recycled volumes info
   -n number of days                   Age of files that contain the recycled volumes to be deleted
   -c /path/to/secrets.json            Secret file containing URL and credential to access Dell SC
   -f                                  If specified, user will not be prompted to confirm the volume deletion

E.g.
   $0 -r /path/to/recycles/vols/file -c /path/to/secrets.json

NB. 
1. /path/to/secrets.json must be in the following format:
{
    "dsm_host": "example.com",
    "dsm_user": "user",
    "dsm_password": "password"
}

2. ucamdsm must be installed and located under /usr/local/bin/
3. /path/to/recycles/vols/file should be a file generated by the script "record_recycled_volumes"
4. Lines in /path/to/recycles/vols/file or files under /path/to/recycled/vols/ can be commented with the sign '#' 
EOF
}

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

force=0

while getopts "ho:r:p:n:c:f" opt
do
  case $opt in
    h)
      usage
      exit
      ;;
    o)
      type=$OPTARG
      ;;
    r)
      recycled_vols_file=$OPTARG 
      ;;
    p)
      path_recycled_vols=$OPTARG
      ;;
    n)
      file_age=$OPTARG
      ;;
    c)
      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 "$recycled_vols_file" ]
then
    if [ -f $recycled_vols_file ]
    then
      file_wwns=$recycled_vols_file
    else
      echo "File $recycled_vols_file not found."
      exit 1
    fi
elif [ "$type" == "by_age" ] && [ ! -z "$path_recycled_vols" ] && [ ! -z "$file_age" ]
then
    past_date=`date --date="$file_age day ago" +%Y%m%d`
    if [ -f $path_recycled_vols/$past_date.wcdc ] || [ -f $path_recycled_vols/$past_date.sby ] || [ -f $path_recycled_vols/$past_date.wcdc_overflow ]
    then
      cat "$path_recycled_vols/$past_date.wcdc" "$path_recycled_vols/$past_date.sby" "$path_recycled_vols/$past_date.wcdc_overflow" > "$path_recycled_vols/$past_date.deleted"
      file_wwns="$path_recycled_vols/$past_date.deleted"
    else
        echo "Recycled volumes files under $path_recycled_vols for the date $past_date cannot be found."
        exit 1
    fi
else
    usage
    exit
fi

if [ ! -z "$file_wwns" ]
then    
    if [[ "$force" == 0 ]]
    then
      nb_vols=`grep -c '^[^#]' $file_wwns`
      read -p "Do you really want to delete the $nb_vols recycled volumes listed in the file $file_wwns (Y or y for Yes)? " -n 1 -r
      echo
    else
      REPLY=Y
    fi
    if [[ $REPLY =~ ^[Yy]$ ]]
    then
      result=$($ucamdsm --file $secrets delete_recycled_vols --wwns_file $file_wwns)
    else
      exit
    fi
fi

if [[ $result == *"[Command succeeded - Returns True]"* ]]
then
    echo "Command succeeded."
    exit 0
else
    echo "Command failed. Please check the ucamdsm log file in /var/log/ucamdsm/."
    exit 1
fi