#! /bin/bash

#
# #############################################################################
# ## Alladdin, the all-add-in web server                                     ##
# #############################################################################
#
# @author Laurent GAERTNER <garthh@bagsbug.net>
# @version $Revision$
# @modifiedby $Author$
# @lastmodified $Date$
#
# #############################################################################
# ## alladdin : execute alladdin's script                                    ##
# #############################################################################
#
# LICENSE:
# Alladdin is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Alladdin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

. /etc/alladdin/alladdin.conf

## Sortie si ce n'est pas l'utilisateur root

if [ $LOGNAME != $execUser ]
then
	echo "Only $execUser can invoke this command..."
	echo "Aborded..."
	exit 0
fi

## Sortie si aucune action n'est appelée

if [ -z $1 ]
then
	echo "ERROR: alladdin $*"
	echo "No action called, action aborded"
	echo ""
	echo "usage:   alladdin [action] [module] [parameters]"
	echo "eg:      alladdin create account 1"
	echo ""
	echo "Use ''alladdin help'' to get actions/modules list"
	exit 0
fi

## Affichage de la liste des actions/packages

if [ $1 = "help" ] ; then

	if [ -z $2 ] ; then
		ls -RC $alladdinDir/scripts/* | sed "s/.*\/\(.*\):$/Actions for module ''\1''/m"
		echo ""
		echo "Use ''alladdin help [module]'' to get full module help"
		exit 0
	else
		if [ -z $3 ] ; then
			if [ ! -d $alladdinDir/scripts/$2 ] ; then
				echo "No module ''$2'' present, abord !"
				echo "Use ''alladdin help'' to get actions/modules list"
				exit 2
			fi
			echo $(cat $alladdinDir/descr/$2.en.xml) | sed "s/.*<package id=\"$2\">\([^<]*\)<\/package> <description>\([^<]*\)<\/description>.*/Module name: \1\nDescription: \2/m"
			echo ""
			echo "Available actions for module ''$2'':"
			ls -C $alladdinDir/scripts/$2
			echo ""
			echo "Use ''alladdin help $2 [action]'' to get full action help"
			exit 0
		else
			if [ ! -f $alladdinDir/scripts/$2/$3 ] ; then
				echo "No action ''$3'' for module ''$2'', abord !"
				echo "Use ''alladdin help'' to get actions/modules list"
				exit 2
			fi
			echo "Action ''$3'' for module ''$2''"
			echo $(cat $alladdinDir/descr/$2.en.xml) | sed "s/.*<action id=\"$3\"> <title>\([^<]*\)<\/title> <description>\([^<]*\)<\/description>.*/Action name: \1\nDescription: \2/m"
			echo ""
			echo "usage: alladdin $3 $2 [account_id]"
			exit 0
		fi
	fi
fi

## Sortie si aucun module n'est appelé

if [ -z $2 ]
then
	echo "ERROR : alladdin $*"
	echo "No module called, action aborded"
	echo ""
	echo "usage:   alladdin [action] [module] [parameters]"
	echo "eg:      alladdin create account 1"
	echo ""
	echo "Use ''alladdin help'' to get actions/modules list"
	exit 0
fi

## Exécution du script

if [ ! -d $alladdinDir/scripts/$2 ] ; then
	echo "No module ''$2'' present, abord !"
	echo "Use ''alladdin help'' to get actions/modules list"
	exit 2
fi

if [ ! -f $alladdinDir/scripts/$2/$1 ] ; then
	echo "No action ''$1'' for module ''$2'', abord !"
	echo "Use ''alladdin help'' to get actions/modules list"
	exit 2
fi
			
/bin/bash $alladdinDir/scripts/$2/$1 $3
