#!/bin/bash

#
# NAME
#	xmlListApp
#
# SYNOPSIS
#	curl http://ip_address/cgi-bin/sdk/xmlListApp
#
# DESCRIPTION
#	This function parses and returns the complete list of
#	secure resources that will be loaded from the various
#	applications to MQX by parsing each /home/APPNAME/app.conf
#	file to see if application APPNAME loads secure resources.
#
# RETURNS
#<resources>
#<default>
#    <resource path="btn-large.png" type="Background" keyId="10" name="btn-large"/>
#    <resource path="btn-small.png" type="Background" keyId="10" name="btn-small"/>
#    <resource path="btn-key-injection.png" type="Background" keyId="10" name="btn-ki"/>
#    <resource path="E.png" type="Background" keyId="10" name="pwd-e"/>
#    <resource path="Q.png" type="Background" keyId="10" name="pwd-q"/>
#    <resource path="U.png" type="Background" keyId="10" name="pwd-u"/>
#    <resource path="I.png" type="Background" keyId="10" name="pwd-i"/>
#    <resource path="N.png" type="Background" keyId="10" name="pwd-n"/>
#    <resource path="O.png" type="Background" keyId="10" name="pwd-o"/>
#    <resource path="X.png" type="Background" keyId="10" name="pwd-x"/>
#    <resource path="date-time.png" type="Background" keyId="10" id="100"/>
#    <resource path="date-time-prompt.ini" type="SecurePrompt" keyId="10" id="100"/>
#    <resource path="date-time-prompt-text.ini" type="PromptText" keyId="10" id="100"/>
#    <resource path="transparent_bgd.png" type="Background" keyId="10" name="transparent_bgd"/>
#</default>
#<emv>
#    <resource path="Signature.bmp" type="Background" keyId="10" name="SigBg" />
#    <resource path="buttonCancelPushed.png" type="Background" keyId="10" name="btnCancelPushed"/>
#    <resource path="buttonOkPushed.png" type="Background" keyId="10" name="btnOkPushed"/>
#    <resource path="buttonBypassPushed.png" type="Background" keyId="10" name="btnBypassPushed"/>
#    <resource path="blue300x60.png" type="Background" keyId="10" name="blue300x60"/>
#    <resource path="gray300x60.png" type="Background" keyId="10" name="gray300x60"/>
#</emv>
#<sdkdemo>
#    <resource path="checkbox2.png" type="Background" keyId="10" name="sdk_checkbox2"/>
#    <resource path="demo1.png"     type="Background" keyId="10" name="sdk_background"/>
#    <resource path="EnterPIN.png"  type="PINMessage" keyId="10" id="250"/>
#    <resource path="menu-btn.png"  type="Background" keyId="10" name="sdk_menu-btn"/>
#    <resource path="radiobox2.png" type="Background" keyId="10" name="sdk_radiobox2"/>
#    <resource path="sign.png"      type="Background" keyId="10" name="sdk_sign"/>
#    <resource path="secprompts0.ini" type="SecurePrompt" keyId="10" id="260" />
#    <resource path="sectitles0.ini" type="PromptText" keyId="10" id="270" />
#</sdkdemo>
#</resources>
#
# VERSION
#	1.0	2016-12-13	Initial release
#

LOG=/tmp/sdk.log
application="sdk/resource/xmlListApp"

echo . >>$LOG
echo . >>$LOG
echo $application ... >>$LOG
echo . >>$LOG

echo "Content-type: text/xml"
echo ""
echo '<?xml version="1.0" encoding="iso-8859-1"?>'
echo '<resources>'
cd /home
for app in * ; do
  if [[ -d "$app" ]] ; then
    if [[ -e "/home/$app/app.conf" ]] ; then
      xmlFile=`cat /home/$app/app.conf | sed -n 's:.*<resources>\(.*\)</resources>.*:\1:p'`
      if [[ "$xmlFile" ]] ; then
        echo "  <$app>"
        #
        # Make sure the XML file is formatted the way I want it with each XML tag
        # on a single line by itself for easier processing
        #
        tr -d '\n' </home/$app/$xmlFile | sed "s/>/>\n/g" | grep -Ev "<!--" | grep "<resource "
        echo "  </$app>"
      fi
    fi
  fi
done
echo '</resources>'
