#!/bin/bash

echo -e '\n';
echo "########################################################################################"
echo "This example demonstrates how to create a trading partner (FTP) using the server API."
echo "########################################################################################"

### Read from the user
echo -e '\n';
echo "Enter protocol:"
read proto
echo "Enter server REST host:"
read jscapehost
echo "Enter server REST port:"
read jscapemanagmentport
echo "Enter username:"
read username
echo "Enter password:"
read password
echo "Enter domain name:"
read domain
echo "Enter trading partner name:"
read tpName
echo "Enter trading partner host (FTP):"
read tpHost
echo "Enter trading partner port (FTP):"
read tpPort
echo "Enter trading partner username (FTP):"
read tpUsername
echo "Enter trading partner password (FTP):"
read tpPassword


### make a cookie file to store the session id and get it through get_session_id()
cookie_file=`mktemp /tmp/cookie.XXXXXXXXXX`

get_session_id() {
   grep JSESSIONID $cookie_file | awk '{printf "%s=%s",$6,$7}'
}


## get CSRF token
csrftoken=`curl -s -k -c "${cookie_file}" -H "Content-type: application/json" $proto://$jscapehost:$jscapemanagmentport/rest/management/csrf-token`

### get session cookie
sid=$(get_session_id ${cookie_file})


## Login to MFT Server
loginResponse=$(curl -s -k -b "$sid" -c "$cookie_file" -w "%{http_code}" -o /dev/null -H "accept: */*" -H "x-js-at: $csrftoken" -H "X-Requested-With: XMLHttpRequest" -H "Content-type: application/json" -X POST -d "{\"username\":\"$username\", \"password\":\"$password\"}" $proto://$jscapehost:$jscapemanagmentport/rest/management/login)

## check server reachable
if [[ "$loginResponse" -eq 000 ]]; then 
echo -e '\n';
echo "Could not connect to the server..";  
else

if [[ "$loginResponse" -eq 204 ]]; then 
echo -e '\n';
echo "Login success. Proceeding to create partner ..";  

### get session cookie again
sid=$(get_session_id ${cookie_file})

## get CSRF token again
csrftoken=`curl -s -k -b "$sid" -H "Content-type: application/json" $proto://$jscapehost:$jscapemanagmentport/rest/management/csrf-token`

## Create trigger
dateinMilliseconds=$(date +%s)
addTradingPartnerResponse=$(curl -s -o /dev/null -k -b "$sid" -w "%{http_code}" -o /dev/null -H "x-js-at: $csrftoken" -H "X-Requested-With: XMLHttpRequest" -H "accept: application/json" -H "Content-type: application/json" -X POST -d "{\"name\":\"$tpName\",\"company\":null,\"email\":\"\",\"proxyData\":null,\"protocol\":\"FTP\",\"url\":\"\",\"host\":\"$tpHost\",\"port\":$tpPort,\"tcpNoDelayEnabled\":true,\"connectionType\":null,\"username\":\"$tpUsername\",\"password\":\"$tpPassword\",\"domain\":null,\"keyProvider\":null,\"hostKeyProvider\":null,\"encryptionRequired\":false,\"aftpData\":null,\"agentData\":null,\"amazonS3Data\":null,\"amazonAwsData\":null,\"as2Data\":null,\"ftpData\":{\"postLoginCommand\":\"\",\"passiveTransferModeRequired\":false,\"epsvCommandAllowed\":false},\"imapData\":null,\"oftpData\":null,\"pop3Data\":null,\"sftpData\":null,\"smtpData\":null,\"splunkHttpData\":null,\"s3CompatibleData\":{\"endPoint\":\"\",\"region\":null},\"tags\":[],\"creationDate\":$dateinMilliseconds,\"version\":1,\"timeout\":30000,\"boxData\":null,\"dropboxData\":null,\"googleStorageData\":null,\"googleCloudData\":null,\"googleDriveData\":null,\"ibmCloudData\":null,\"microsoftAzureFileStorageData\":null,\"microsoftAzureBlobStorageData\":null,\"microsoftAzureDataLakeData\":null,\"microsoftAzureDataLake2Data\":null,\"microsoftOneDriveData\":null}" $proto://$jscapehost:$jscapemanagmentport/rest/management/trading-partners/$domain)

## Already exists check
if [[ "$addTradingPartnerResponse" -eq 409 ]]; then 
echo -e '\n'; 
echo "Trading partner already exists .. Try other name"  
fi

## success creation check
if [[ "$addTradingPartnerResponse" -eq 201 ]]; then 
echo -e '\n'; 
echo "Trading partner created successfully.."  
fi
## failed creation check
if [[ "$addTradingPartnerResponse" -eq 404 ]]; then 
echo -e '\n'; 
echo "Response Code : 404 Trading partner creation failed, Recheck the parameters.."  
fi
else
echo -e '\n';   
echo "Login failed. Recheck rest user and password";  
fi
fi


### logout
curl -s -k -b "$sid" -H "x-js-at: $csrftoken" -H "X-Requested-With: XMLHttpRequest" -H "Content-type: application/json" -X POST $proto://$jscapehost:$jscapemanagmentport/rest/management/logout

### delete cookie file
rm $cookie_file