#!/bin/bash
# vi: set textwidth=0 :
# e.g. scripts/rerun-responses.sh --id 48009000 --date 2011-08-01 --ssh "ssh -i ~/.ssh/id_production apas@emperor"

function help()
{
      printf "Update an exam request\n"
      printf "$0 --examrequestid 1 --studentid 1 --writedate 2012-01-01T01:01:01 --invigilatorid 1 --inventoryid 1 --requeststatus BLAH --requeststatusdate 2012-01-01T01:01:01 --userIDWhoUpdated me [ -h|--help ] \n";
}

i=0
OPTS=`getopt -o e:s:w:i:n:r:d:u: -l examrequestid:,studentid:,writedate:,invigilatorid:,inventoryid:,requeststatus:,requeststatusdate:,useridwhoupdated:,help -- "$@"`
eval set -- "$OPTS"
while true ; do
  case "$1" in
    --examrequestid) examRequestID=$2; shift 2 ;;
    --studentid) studentID=$2; shift 2 ;;
    --writedate) examWriteDate=$2; shift 2 ;;
    --invigilatorid) invigilatorID=$2; shift 2 ;;
    --inventoryid) inventoryID=$2; shift 2 ;;
    --requeststatus) requestStatus=$2; shift 2 ;;
    --requeststatusdate) requestStatusDate=$2; shift 2 ;;
    --useridwhoupdated) userID=$2; shift 2 ;;
    -h|--help)
      help;
      exit 0;;
    --) shift ; break ;;
    *) echo "Internal error!" ; exit 1 ;;
  esac
done

#if [ "$nation" != "true" ] && [ "$id" != "true" ] || [ "$nation" == "true" ] && [ "$id" == "true" ]; then
#  help
#  exit 99;
#fi;

cat > /tmp/soap-request.txt <<_SOAP_REQUEST_
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
                  xmlns:upd="http://update.request.exam.athabascau.ca">
   <soapenv:Header/>
   <soapenv:Body>
      <upd:update>
         <upd:studentID>$studentID</upd:studentID>
         <upd:examWriteDate>$examWriteDate</upd:examWriteDate>
         <upd:invigilatorID>$invigilatorID</upd:invigilatorID>
         <upd:inventoryID>$inventoryID</upd:inventoryID>
         <upd:requestStatus>$requestStatus</upd:requestStatus>
         <upd:requestStatusDate>$requestStatusDate</upd:requestStatusDate>
         <upd:userIDWhoUpdated>$userID</upd:userIDWhoUpdated>
      </upd:update>
   </soapenv:Body>
</soapenv:Envelope>

_SOAP_REQUEST_
 
cat /tmp/soap-request.txt 
#exit 0;

curl -H "Content-Type: text/xml; charset=utf-8" \
     -H "SOAPAction: http://update.request.exam.athabascau.ca" \
     -d @/tmp/soap-request.txt \
     -k -X POST -u cmd:cmd123 http://localhost:8080/exam-ws/services/ExamRequestUpdateService> /tmp/response.xml 2>/tmp/curl.stderr
if [ $? -ne 0 ]; then
  cat /tmp/curl.stderr
  exit 1;
fi

