#!/bin/bash
# this script to execute logon command
if [ ! -d "${HOME}/LSFClient" ]; then
	mkdir -p "${HOME}/LSFClient"
fi
echo "Log on to IBM Spectrum LSF Application Center"
echo -n "User account: "
read user
read -p "Enter password: " -s password
echo -e ""
echo "Specify the URL to connect to IBM Spectrum LSF Application Center. Format: "
echo "http://host_name:port_number/platform or https://host_name:port_number/platform"
echo -n "URL: "
read server
curl --cacert "${0%/*}/cacert.pem"  -s -H "Accept: application/xml" -H "Content-Type: application/xml" -X POST -d "<User><name>${user}</name><pass>${password}</pass></User>" "${server%/}/ws/logon" -o "${HOME}/.pacpass"
result=`awk -F '<token>|</token>' '{print $2}' "${HOME}/.pacpass"`
#echo "logon result = ${result}"
if [ -n "${result}" ]; then
    echo ${user}>"${HOME}/LSFClient/user"
    echo ${server}>"${HOME}/LSFClient/server"
    echo ${result}>"${HOME}/.pacpass"
    echo "You have successfully logged on to IBM Spectrum LSF Application Center."
else
    echo "Unable to log on to IBM Spectrum LSF Application Center. Check that the user name, password, and URL are correct."
fi
