#! /bin/bash

#
# #############################################################################
# ## Alladdin, the all-add-in web server                                     ##
# #############################################################################
#
# @author Laurent GAERTNER <garthh@bagsbug.net>
# @version $Revision$
# @modifiedby $Author$
# @lastmodified $Date$
#
# #############################################################################
# ## wishes: add task to alladdin's task list                                ##
# #############################################################################
#
# 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
	choice="-h"
else
	choice=$1
fi

case $choice in

## Aide en ligne
'-h'|'--help')
	echo "usage:   wish [command] [parameters]"
	echo "eg:      wish -a create account 1"
	echo ""
	echo "Use ''alladdin help'' to get actions/modules list"
        echo ""
        echo "Commands:"
        echo " -a --add       add an action to task list"
        echo "                wish -a [action] [module] [account_id]"
        echo " -r --remove    remove an action from task list"
        echo "                wish -r [action_id]"
        echo " -l --list      list all actions in task list"
#        echo " -L --list-all  list all task, pending or done"
        echo " -p --purge     purge all tasks"
        echo " -e --exec      execute all tasks without crontab"
        echo ""
	exit 0
;;

## Ajout d'un élément à la liste de tâches
'-a'|'--add')
	if [ -z $2 ] ; then
		echo "ERROR: no action specified"
		echo "use ''wish --help''"
		exit 0
	fi
	if [ -z $3 ] ; then
		echo "ERROR: no module specified"
		echo "use ''wish --help''"
		exit 0
	fi
	if [ -z $4 ] ; then
		echo "WARNING: no account specified"
		echo "Command may not execute properly"
		echo "Check ''alladdin help $3 $2''"
		echo ""
	fi
	if [ ! -d $alladdinDir/scripts/$3 ] ; then
		echo "ERROR: module ''$3'' not available"
		echo "use ''alladdin help'' to get actions/modules list"
		exit 0
	fi
	if [ ! -f $alladdinDir/scripts/$3/$2 ] ; then
		echo "ERROR: action ''$2'' not available"
		echo "use ''alladdin help $3'' to get actions list for module $3"
		exit 0
	fi
	
	sql="INSERT INTO task_pending (action_script, package, acid) VALUES ('$2', '$3', '$4')"
	mysql -u $sqlUser -p$sqlPass -h $sqlHost -P$sqlPort $sqlDB -e "$sql"
	
	echo "Action ''$2'' for module ''$3'' added"
;;

## Supprimer un élément de la liste de tâches
'-r'|'--remove')
	if [ -z $2 ] ; then
		echo "ERROR: no task selected"
		echo "use ''wish --help''"
		exit 0
	fi
	
	sql="SELECT ptid FROM task_pending WHERE ptid=$2"
	ptid=$(mysql -u $sqlUser -p$sqlPass -h $sqlHost -P$sqlPort $sqlDB -e "$sql" -s -N)

	if [ -z $ptid ] ; then
		echo "Task ''$2'' not found !"
		exit 0
	fi
	
	sql="DELETE FROM task_pending WHERE ptid=$2"
	mysql -u $sqlUser -p$sqlPass -h $sqlHost -P$sqlPort $sqlDB -e "$sql"
	
	echo "Task ''$2'' deleted"
;;

## Lister les tâches
'-l'|'--list'|'-L'|'--list-all')
	sql="SELECT * FROM task_pending ORDER BY ptid"
	
	## Génération d'un nom de fichier temporaire
	tmpfile=$(randomstring -l 12 -c dlu)

	mysql -u $sqlUser -p$sqlPass -h $sqlHost -P$sqlPort $sqlDB -e "$sql" -s -N > /tmp/$tmpfile
	
	echo "#ID Task (action, module, acid)"
	while read line
	    do
	 	id=$(echo $line | awk '{print $1}')
		mdl=$(echo $line | awk '{print $2}')
		act=$(echo $line | awk '{print $3}')
		prm=$(echo $line | awk '{print $4}')
		aci=$(echo $line | awk '{print $5}')
		dtp=$(echo $line | awk '{print $6}')
		
		nbcharNum=$(echo -n $id | wc -m)

		if [ $nbcharNum = 1 ]
		then
			echo -n -e "  \033[1;33m$id\033[0;0m"
		fi

		if [ $nbcharNum = 2 ]
		then
			echo -n -e " \033[1;33m$id\033[0;0m"
		fi

		if [ $nbcharNum = 3 ]
		then
			echo -n -e "\033[1;33m$id\033[0;0m"
		fi
		
		echo " $act $mdl $aci"	
	done < /tmp/$tmpfile
	
	rm -rf /tmp/$tmpfile
;;


## Purger les tâches en attente
'-p'|'--purge')
	sql="TRUNCATE TABLE task_pending"
	mysql -u $sqlUser -p$sqlPass -h $sqlHost -P$sqlPort $sqlDB -e "$sql"
	echo "All tasks deleted"
;;

## Forcer l'exécution des tâches
'-e'|'--exec')
	tail -f /var/log/alladdin.log
    bash $alladdinDir/cron/djinn
;;

esac
exit 0