#!/bin/bash

# A sample script for calls to the CLI. This one creates a Jet Stream Template.
#
# VERY IMPORTANT: In order for this to work, you need to go through the steps here:
# https://help.delphix.com/cd/current/content/CLI_cookbook_configuring_key_based_SSH_authentication_for_automation.htm
# After this, you will not need to use a username and password to log into the Delphix engine. If you do not
# set up the SSH authentication, you will have to manually enter the password.
#

##### Constants

# Describes a Delphix software revision.
VERSION="1.11.10"  # Default Values. These can be overwritten with optional arguments.
engine="ars-6010.dlpxdc.co"
username="admin"

# Examples
# Create template with mandatory params
# Create template with optional params: Notes and Description
# ./createDBTemplate.sh -n <sourceName> -N "<templateNotes>" -D "<AnyDescription>" <templateName> <containerName>
# NOTE: This script is for adding one source per template. It does not add properties for template, container, or source.

##### Functions

# Help Menu
function usage {
    echo "Usage: createDBTemplate.sh [[-h] | options...] <template_name> <source_container>"
    echo "Create a Jet Stream Dat Template."
    echo ""
    echo "Positional arguments"
    echo "  <template_name>"
    echo "  <source_container>"
    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                Server user. Password needs to be manually provided at runtime, otherwise revert to default"
    echo "  -n                Source name to display on JS template screen"
    echo "  -N                Template notes, if any. Type: String"
    echo "  -D                Source description, if any. Type: String"
}

# Create Our Session, including establishing the API version.
function create_session {
    echo "Creating session..."
    SSH_CMD="ssh ${username}@${engine}"
    ${SSH_CMD} "version $VERSION"
    check_result
}

# 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"
        echo $result
        exit 1
    fi
}

function create_template {
    paramString="selfservice template create;set name=$templateName;"
    if [[ -n $templatenotes ]]; then
        paramString="$paramString set notes=\"$templatenotes\";"
    fi
    paramString="$paramString edit dataSources;add;"
    paramString="$paramString set container=$sourceContainer;set source.name=$sourcename;set source.priority=1;"
    if [[ -n $sourcedesc ]]; then
        paramString="$paramString set source.description=\"$sourcedesc\";"
    fi
    paramString="$paramString commit;"
    # echo $paramString
    echo "Creating Data Template..."
    result=$(${SSH_CMD} $paramString)
    check_result
    echo "New JetStream template $templateName successfully created"
}

##### Main

while getopts "u:d:n:N:D:h" flag; do
    case "$flag" in
        u ) username=${OPTARG%:*} ;;
        d ) engine=$OPTARG ;;
        n ) sourcename=$OPTARG ;;
        N ) templatenotes=$OPTARG ;;
        D ) sourcedesc=$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 are 2 positional arguments
if [ $# -ne 2 ]; then
    usage
    exit 1
fi

# Get the two positional arguments
templateName=$1
shift
sourceContainer=$1

create_session
create_template
