#!/bin/bash

# A sample script for calls to the API. This one shares Bookmark across containers in same template.

##### Constants

# Describes a Delphix software revision.
# Please change version are per your Delphix Engine CLI, if different.
VERSION="1.11.10"


##### Default Values. These can be overwriten with optional arguments.
engine="ars-dlpx-6010-3.dlpxdc.co"
username="admin"
password="delphix"

##examples##
# Share Bookmark
#./shareBookmark.sh -a share JS_BOOKMARK-75
# Unshare Bookmark
#./shareBookmark.sh -a unshare JS_BOOKMARK-75

##### Functions

# Help Menu
function usage {
	echo "Usage: shareBookmark.sh [[-h] | options...] <bookmarkName>"
	echo "Share/Unshare JetStream bookmark"
	echo ""
	echo "Positional arguments"
	echo "bookmarkName. Format: JS_BOOKMARK-<n>"
	echo ""
	echo "Optional Arguments:"
	echo "  -h                Show this message and exit"
	echo "  -d                Delphix engine IP address or host name, otherwise revert to default"
	echo "  -u USER:PASSWORD  Server user and password, otherwise revert to default"
	echo "  -a                action to perform on bookmark. Type:String. Values:share/unshare"
}

# Create Our Session, including establishing the API version.
function create_session
{
	# Pulling the version into parts. The {} are necessary for string manipulation.
	# Strip out longest match following "."  This leaves only the major version.
	major=${VERSION%%.*}
	# Strip out the shortest match preceding "." This leaves minor.micro.
	minorMicro=${VERSION#*.}
	# Strip out the shortest match followint "." This leaves the minor version.
	minor=${minorMicro%.*}
	# Strip out the longest match preceding "." This leaves the micro version.
	micro=${VERSION##*.}

	# Quick note about the <<-. If the redirection operator << is followed by a - (dash), all leading TAB from the document data will be 
	# ignored. This is useful to have optical nice code also when using here-documents. Otherwise you must have the EOF be on a line by itself, 
	# no parens, no tabs or anything.

	echo "creating session..."
	result=$(curl -s -S -X POST -k --data @- http://${engine}/resources/json/delphix/session \
		-c ~/cookies.txt -H "Content-Type: application/json" <<-EOF
	{
		"type": "APISession",
		"version": {
			"type": "APIVersion",
			"major": $major,
			"minor": $minor,
			"micro": $micro
		}
	}
	EOF)

	check_result
}

# Authenticate the DE for the provided user.
function authenticate_de
{
	echo "authenticating delphix engine..."
	result=$(curl -s -S -X POST -k --data @- http://${engine}/resources/json/delphix/login \
		-b ~/cookies.txt -c ~/cookies.txt -H "Content-Type: application/json" <<-EOF
	{
		"type": "LoginRequest",
		"username": "${username}",
		"password": "${password}"
	}
	EOF)	

	check_result
}

function bookmark_action
{
	# Change share mode of bookmark
	
	if [[ $action = "share" ]]
	then  
		result=$(curl -s -X POST -k --data @- http://${engine}/resources/json/delphix/jetstream/bookmark/${bookmarkName}/$action \
	   -b ~/cookies.txt -H "Content-Type: application/json" <<-EOF
	   {}
	   EOF)

	check_result
	
    echo "Bookmark ${bookmarkName} is now in shared mode"

    elif [[ $action = "unshare" ]]
	then
		result=$(curl -s -X POST -k --data @- http://${engine}/resources/json/delphix/jetstream/bookmark/${bookmarkName}/$action \
	   -b ~/cookies.txt -H "Content-Type: application/json" <<-EOF
	   {}
	   EOF)

	check_result
	
    echo "Bookmark ${bookmarkName} is now in not-share mode"		            

	fi

}

# Check the result of the curl. If there are problems, inform the user then exit.
function check_result
{
	exitStatus=$?
	if [ $exitStatus -ne 0 ]
	then
	    echo "command failed with exit status $exitStatus"
	    exit 1
	elif [[ $result != *"OKResult"* ]]
	then
		echo ""
		echo $result
		exit 1
	fi
}

##### Main

while getopts "u:d:a:h" flag; do
	case "$flag" in
    	u )             username=${OPTARG%:*}
						password=${OPTARG##*:}
						;;
		d )             engine=$OPTARG
						;;
		a )         	action=$OPTARG
		                ;;              
		h )             usage
						exit
						;;
		* )             usage
						exit 1
	esac

done


# Shift the parameters so we only have the positional arguments left
shift $((OPTIND-1))

# Check that there is 1 positional arguments
if [ $# != 1 ]
then
	usage
	exit 1
fi

# Get the one positional arguments
bookmarkName=$1

create_session
authenticate_de
bookmark_action


