#!/bin/bash

#
# NAME
#	xmlAddApp
#
# SYNOPSIS
#	curl http://ip_address/cgi-bin/sdk/xmlAddApp
#		?app=APPNAME
#		&name=resName
#		&type=[background|keyboard|pan]
#		&path=path/to/file.res
#		&keyId=NN
#		&signature=12345678ABCD
#
# DESCRIPTION
#	This function adds a line to an application's
#	resource XML file.  Normally this is in /home/APPNAME/resources/resources.xml
#	but I shouldn't assume this.
#
#	if /home/APPNAME/app.conf doesn't exist then
#	  create one using a basic set of tags.
#	end if
#	Look in /home/APPNAME/app.conf for the XML tag <resources>,
#	If the tag doesn't exist, then
#	  create a new tag and an empty resources.xml
#	file.
#	open the resources.xml file
#	remove any existing resource line with "name"
#	insert a new resource line for resource called "name"
#
#	FIXME:  Still to be developed!!!
#	================================
#
# RETURNS
#	An XML formatted result
#	<xmlAddApp>
#	  <result>xxx</result>
#	  <text>Descriptive result message</text>
#	</xmlAddApp>
#
#	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 = path missing
#	8 = keyId missing
#	9 = signature missing
#	10 = Unable to delete existing resource element
#	11 = Unable to insert new resource element
#
# VERSION
#	1.0		2016-12-13	Initial release
#	1.1     2017-04-10      Functional, need proper XML returns, not just OK
#	1.2     2017-04-10      Functional, returns consistent XML as xmlListApp
#	1.3     2017-28-06      Only update resources.xml. Expects resource file to be in /home/APP_NAME/resources/
#	1.4     2018-01-26      Use xmlstarlet to edit xml


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

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

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

exit
}

urldecode(){
  echo -e "$(sed 's/+/ /g;s/%/\\x/g;')"
}

echo . >>$LOG
echo . >>$LOG
echo $application ... >>$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", "type", "path", "keyId"
# and "signature" 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

name=$(echo $name | urldecode)

if [[ -z "$type" ]] ; then
  result=6
  text="Mandatory parameter 'type' missing"
  returnXML
fi

if [[ -z "$path" ]] ; then
  result=7
  text="Mandatory parameter 'path' missing"
  returnXML
fi

if [[ -z "$keyId" ]] ; then
  result=8
  text="Mandatory parameter 'keyId' missing"
  returnXML
fi

if [[ -z "$signature" ]] ; then
  result=9
  text="Mandatory parameter 'signature' missing"
  returnXML
fi

RESOURCE_FILENAME=$(basename $path)

#copy resource to app resources
#echo "Copying /home/sdk/files/$path to /home/$app/resources/" >>$LOG
#cp /home/sdk/files/$path /home/$app/resources/$RESOURCE_FILENAME
#chmod 644 /home/$app/resources/$RESOURCE_FILENAME

#if resource is already in $xmlFile, remove existing resource element
RETURN_XML=$(xmlstarlet ed -d "/resources/resource[@name='$name' and @type='$type']" /home/$app/$xmlFile) 
if [ $? == 0 ]; then
  echo $RETURN_XML > /home/$app/$xmlFile 	
else  
  result=10
  text="Unable to delete resource element with attribute name='$name' and type='$type'"
  returnXML
fi

#update $xmlFile with added resource
xmlstarlet ed -P -S -L -s /resources -t elem -n ResourceTMP -v "" \
    -i //ResourceTMP -t attr -n "name" -v "$name" \
    -i //ResourceTMP -t attr -n "path" -v "$RESOURCE_FILENAME" \
    -i //ResourceTMP -t attr -n "type" -v "$type" \
    -i //ResourceTMP -t attr -n "keyId" -v "$keyId" \
    -i //ResourceTMP -t attr -n "signature" -v "$signature" \
	-r //ResourceTMP -v resource \
	/home/$app/$xmlFile > /dev/null 2>&1
if [ $? != 0 ]; then
  result=11
  text="Unable to add resource element with attribute name='$name'"
  returnXML
fi
	
NEW_RESOURCE="<resource name=\"$name\" path=\"$RESOURCE_FILENAME\" type=\"$type\" keyId=\"$keyId\" signature=\"$signature\"/>"
echo "Adding $NEW_RESOURCE element to /home/$app/$xmlFile" >>$LOG


result=0
text="OK"
returnXML


