#!/bin/bash

#
# NAME
#	xmlDelApp
#
# SYNOPSIS
#	curl http://ip_address/cgi-bin/sdk/xmlDelApp?app=APPNAME&name=resName&type=[background|keyboard]
#
# DESCRIPTION
#	This function removes a resource from an application's resources.xml file
#	It is used by PageBuilder to manage secure resources for an application.
#
# RETURNS
#	An XML formatted result
#	<xmlDelApp>
#	  <result>xxx</result>
#	  <text>Descriptive result message</text>
#	</xmlDelApp>
#
#	0 = OK
#	1 = Application $app doesn't have an app.conf file
#   2 = Application's app.conf doesn't have a <resources> tag
#   3 = Application's $app doesn't have resources.xml file
#	4 = app missing
#	5 = name missing
#	6 = type missing
#   7 = Unable to delete resource element
#
# VERSION
#	1.0	2016-12-13	Initial release
#	1.2 2017-04-10	Functional, returns consistent XML as xmlListApp
#	1.3 2018-01-26  Use xmlstarlet to edit xml
#

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

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

returnXML() {
cat <<EOF
<resources>
    <response>$result</response>
    <text>$text</text>
</resources>
EOF

echo "$application exiting (result=$result, text=$text)" >>$LOG

exit
}

echo . >>$LOG
echo . >>$LOG
echo xmlDelApp >>$LOG
echo . >>$LOG

if [ -z "$QUERY_STRING" ] ; then
  echo "$application: No parameters QUERY_STRING" >>$LOG
else
  echo "$application: QUERY_STRING = '$QUERY_STRING'" >>$LOG
  saveIFS=$IFS
  IFS=\&
  set $QUERY_STRING
  while [ "$1" ] ; do
    left=${1%=*}
    right=${1#*=}
    eval $left=$right
    echo "Parameter '$left' = '$right'" >>$LOG
    shift
  done
  IFS=$saveIFS
fi

echo "Content-type: text/xml"
echo ""
echo '<?xml version="1.0" encoding="iso-8859-1"?>'

#
# Verify that app.cfg exist
#
if [[ ! -e "/home/$app/app.conf" ]] ; then
  result=1
  text="Application $app doesn't have an app.conf file"
  returnXML
fi

#
# Verify that app.cfg contains <resources>...<//resources> element
#
xmlFile=`cat /home/$app/app.conf | sed -n 's:.*<resources>\(.*\)</resources>.*:\1:p'`
if [[ -z "$xmlFile" ]] ; then
  result=2
  text="Application $app app.conf doesn't have a 'resources' tag"
  returnXML
fi

#
# Verify that $xmlFile exist
#
if [[ ! -e "/home/$app/$xmlFile" ]] ; then
  result=3
  text="Application $app doesn't have an $xmlFile file"
  returnXML
fi

#
# Verify that the mandatory parameters "app", "name" and "type" are specified.
#
if [[ -z "$app" ]] ; then
  result=4
  text="Mandatory parameter 'app' missing"
  returnXML
fi
if [[ -z "$name" ]] ; then
  result=5
  text="Mandatory parameter 'name' missing"
  returnXML
fi
if [[ -z "$type" ]] ; then
  result=6
  text="Mandatory parameter 'type' missing"
  returnXML
fi

#RESOURCE_FILENAME=`grep -e "name=\"$name\"" /home/$app/$xmlFile | sed -n 's/.*path="\([^"]*\).*/\1/p'`
RESOURCE_FILENAME=$(xmlstarlet sel -t -m "/resources/resource[@name='$name' and @type='$type']" -v "@path" /home/$app/$xmlFile)

if [[ -z "$RESOURCE_FILENAME" ]] ; then
  echo Resource "$name" associated file $RESOURCE_FILENAME NOT found! Skipping delete. >>$LOG
else
  echo Resource "$name" has associated file $RESOURCE_FILENAME >>$LOG

  #remove app resource file
  echo "Removing /home/$app/resources/$RESOURCE_FILENAME" >>$LOG
  rm -f /home/$app/resources/$RESOURCE_FILENAME
fi

#remove existing resource element
echo "Removing resource element from  /home/$app/$xmlFile" >>$LOG
#sed -i -e "/<resource name=\"$name\"/d" /home/$app/$xmlFile
RETURN_XML=$(xmlstarlet ed -d "/resources/resource[@name='$name' and @type='$type']" /home/$app/$xmlFile) 
if [ $? == 0 ]; then
  echo $RETURN_XML | xmllint --format --pretty 2 - > /home/$app/$xmlFile
else  
  result=7
  text="Unable to delete resource element with attribute name='$name'"
  returnXML
fi

result=0
text="OK"
returnXML

