#!/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
. /usr/local/lib/muglin/muglin.d/libmuglin
SQLFLT="" # sql filter
SQLSET="" # used for UPDATE ... SET

while getopts sei:n:u:h: ARG; do
  case $ARG in
     s)
	setcmd show
     ;;
     e)
	setcmd edit
     ;;
     i)
	if [[ "`echo $OPTARG | grep -e '^[[:digit:]]*$'`" == "" ]]; then echo "Argument for -i is missing or not numeric, aborting!"; exit -1; fi
	if [[ "$SQLFLT" == "" ]]; then
	  SQLFLT="WHERE CID = '$OPTARG'"
	else
	  echo "Name or CID already provided. Aborting"
	  exit -1
	fi
     ;;
# WTF?!? There is no other name than the hostname for clients
     n)
	if [[ "$(echo $OPTARG | grep -e '^[[:alnum]_]*$')" == "" ]]; then echo "Argument for -n contains invalid characters, aborting!"; exit -1; fi
	if [[ "$SQLFLT" == "" ]]; then
	  SQLFLT="WHERE Name= '$OPTARG'"
	else
	  echo "Name or CID already provided. Aborting"
	  exit -1
	fi
     ;;
     u)
	uacfile="$(readlink -f $OPTARG)"
	if [[ ! -e "$uacfile" ]]; then echo "File \"$uacfile\" not found, aborting!"; exit -1; fi
	UACONF=$uacfile
     ;;
     h)
	if [[ "$(echo $OPTARG | grep -e '^[[:alnum]-]*$')" == "" ]]; then echo "Argument for -h contains invalid characters, aborting!"; exit -1; fi
	HOSTNAME=$OPTARG
     ;;
     :)
	echo "Option -$OPTARG requires an argument. Aborting"
	exit -1
     ;;
     \?)
	echo "Invalid option: -$OPTARG"
	exit -1
     ;;
  esac
done

case $CMD in
  show)
	# very simple for now, but may be expanded to add more features
	mysql -u$MYS_USER -p$MYS_PASS -D$MYS_DB -B -e"SELECT MAC,Hostname,Unattend,IP,Status FROM Clients"
  ;;
  edit)
	if [[ "$HOSTNAME" != "" ]]; then
	  SQLSET="Hostname = $HOSTNAME"
	fi
	if [[ "$HOSTNAME" != "" && "$UACONF" != "" ]]; then
	  SQLSET="$SQLSET, "
	fi
	if [[ "$UACONF" != "" ]]; then
	  SQLSET="$SQLSET Unattend = '$UACONF'"
	fi
	mysql -u $MYS_USER -p$MYS_PASS -D$MYS_DB -B -e"UPDATE Clients SET $SQLSET $SQLFLT';"	
  ;;
  *)
	echo "Missing command. Aborting"
	exit -1
  ;;
esac
