#!/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 replaces an existing target volume with a gold image volume.

Restrictions for use:
  * The target volume must be a Pure storage volume
  * Both the target volume and the gold image volume must be mounted
  * There must be no processes listed by "fuser -c" for either the gold image 
    or the target

WARNING: The data on the target volume will be deleted and replaced with a copy 
of the data on the gold image

OPTIONS:
  -h                                  Show this message
  -g /path/to/goldimage/mountpoint    Mount pint of the gold image volume
  -t /path/to/target/mountpoint       Mount point of the target volume
  -k /path/to/secrets.json            Secrets file containing URL and 
                                      credentials to access the Pure storage
  -u true or false                    Optional, but if True (default) unmount 
                                      the source mountpoint, otherwise don't

E.g.
$0 -g /d16 -t /d17 -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
###################################################################
unmount_src_mp="true"
while getopts "hg:t:k:u:" opt
do
  case $opt in
    h)
      usage
      exit
      ;;
    g)
      g_mount_point=$OPTARG
      ;;
    t)
      t_mount_point=$OPTARG
      ;;
    k)
      secrets=$OPTARG
      ;;
    u)
      unmount_src_mp=$OPTARG
      ;;
    ?)
      usage
      exit
      ;;
  esac
done

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

# Call ucampurestorage package
result=$($ucampurestorage --record_config True --file $secrets volume replace --src_mp $g_mount_point --dst_mp $t_mount_point --unmount_src_mp $unmount_src_mp)
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
