#!/bin/bash
########################################################################
# MuGLIn - MuGLIn GNU/Linux Installation		                       #
#                                                                      #
# Copyright (C) 2010 Jakob Gurnhofer <jakob.gurnhofer@gmail.com>       #
# Copyricht (C) 2010 Srdjan Markovic <smark2ki@htl.moedling.at>        #
#                                                                      #
# This file is part of MuGLIn source code.                             #
#                                                                      #
# MuGLIn 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 3 of the License, or    #
# (at your option) any later version.                                  #
#                                                                      #
# MuGLIn 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 MuGLIn. If not, see <http://www.gnu.org/licenses/>.       #
########################################################################

. /etc/muglin/base.conf
. /etc/muglin/cli_frontend.conf
. /usr/local/lib/muglin/muglin.d/libmuglin

# Arguments:
# -h help
# -s show
# -a allow
# -d deny
# -c CIDs
# -n name
# -l all

while getopts sadc:n: OPTION; do
	case $OPTION in
		s) setcmd show;;
		a) setcmd allow;;
		d) setcmd deny;;
		c)
			if [ "`echo $OPTARG | egrep '^[[:digit],]$'`" != "" ]; then echo "CIDs can only contain numbers or \",\" to split them, aborting!"; exit -1; fi
			CID=`echo "$CID $OPTARG"`
		;;
		n)
			if [ "`echo $OPTARG | egrep '^\-'`" != "" ]; then echo "Names must not start with \"-\" (maybe missing argument?), aborting!"; exit -1; fi
			NAME=`echo "$NAME $OPTARG"`
		;;
		l) ALL=all;;
		h)
		  echo -e "Manages pending clients.
Options:
-h\tThis help
-s\tShows a client list sorted by their pending state
-a\tGrants one or more clients. Names and/or CIDs must be provided.
-d\tDenies one or more clients. Names and/or CIDs must be provided.
-c\tCID. Multiple CIDs can be separated by \",\".
-n\tNames. Multiple Hostnames can be separated by \",\".
-l\tAll clients with pending status."
		  exit
		;;
		\?)
		  echo "Unknown option \"-$1\". Aborting"
		  exit -1
		;;
		:)
		  echo "Option \"-$OPTARG\" requires an argument. Aborting."
		  exit -1
		;;
	esac
done

cmdName=`echo "$NAME" | sed "s/ /\' OR Name = \'/g"`
#Resolv names to CIDs
if [ "$cmdName" != "" ]; then
	CID=`echo $CID $(echo "SELECT CID FROM Clients WHERE Name = '$cmdName'" | mysql -u$MYS_USER -p$MYS_PASS -D$MYS_DB --skip-column-names -B)`
fi

show_list_with_pending(){
	echo -e "$(echo $SHOW | sed -e 's/ /\\t\\t /g')"
	declare -a list=($(echo "SELECT $(echo $SHOW | sed -e 's/ /,/g') FROM Clients WHERE Pending = $1" | mysql -u$MYS_USER -p$MYS_PASS -D$MYS_DB --skip-column-names -B))
	count=$(echo ${list[@]} | wc -w)
	for ((i=0;i<count;)); do
		echo -e "$i: ${list[$i]}\t\t | ${list[$(($i+1))]}\n";
		i=$(($i+2));
	done
}
	
cmdCID=`echo "$CID" | sed "s/ /\' OR CID = \'/g"`
export cmdCID

set_pending_status(){
	if [ "$ALL" = "all" ]; then
		echo "UPDATE Clients SET Pending = $1 WHERE Pending = 1 OR Pending = 2" | mysql -u$MYS_USER -p"$MYS_PASS" -D$MYS_DB --skip-column-names -B
	else
		echo "UPDATE Clients SET Pending = $1 WHERE CID = $cmdCID" | mysql -u$MYS_USER -p"$MYS_PASS" -D$MYS_DB --skip-column-names -B
	fi
}
	
case $CMD in 
	show)
		echo "Pending Clients:"
		show_list_with_pending 1
		echo "Pending Clients (actually offline):"
		show_list_with_pending 2
		echo "Allowed Clinets:"
		show_list_with_pending 0
		echo "Denied Clients:"
		show_list_with_pending -1
	;;
	allow)
		set_pending_status 0
	;;
	deny)
		set_pending_status -1
	;;
easc
