#!/bin/bash

## This example demonstrates how to create a FTP network storage using the server API.
echo -e '\n';
echo "###########################################################################"
echo "This example demonstrates how to list a directory using the client REST API"
echo "###########################################################################"


### Read from the user
echo -e '\n';
echo "Enter protocol:"
read proto
echo "Enter server host:"
read jscapehost
echo "Enter server port:"
read jscapeclientport
echo "Enter username:"
read username
echo "Enter password:"
read password
echo "Enter domain name:"
read domain
echo "Enter directory to list:"
read remotePath


### 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}'
}


## Login to MFT Server
loginResponse=$(curl -s -k -c "$cookie_file" -w "%{http_code}" -o /dev/null -H "Content-type: application/json" -X POST -d "{\"domain\":\"$domain\",\"username\":\"$username\", \"password\":\"$password\"}" $proto://$jscapehost:$jscapeclientport/rest/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 list the directory ..";  

### get session cookie
sid="`get_session_id`"

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

## listDir
listDirResponse=$(curl -s -o listdir.txt -k -b "$sid" -w "%{http_code}" -H "x-js-at: $csrftoken" -H "X-Requested-With: XMLHttpRequest" -H "accept: */*" -H "Content-type: application/json" -X GET $proto://$jscapehost:$jscapeclientport/rest/files/list/?path=$remotePath)

## Completion check
if [[ "$listDirResponse" -eq 200 ]]; then 
echo -e '\n'; 
echo "Directory sucessfully listed .."  
echo -e '\n';
cat listdir.txt
else
echo "Directory listing failed check the path.."     
fi
else
if [[ "$loginResponse" -eq 500 ]]; then
echo -e '\n';
echo "Response code 500 Invalid credentials.."; 
else  
echo -e '\n';   
echo "Login failed. Re-check protocol,host,port,user and password"; 
fi 
fi
fi

## clean the file list
>listdir.txt

### logout
curl -s -k -b "`get_session_id`" -X POST $proto://$jscapehost:$jscapeclientport/rest/logout

### delete cookie file
rm $cookie_file