#!/bin/sh

###############################################################################
##
## Copyright 2008 Point Clark Networks.
##
###############################################################################
##
## This program 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.
##
## This program 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, write to the Free Software
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##
###############################################################################
##
## Adds command to /etc/sudoers for webconfig
##
## Usage: addsudo <command> <logtag>
##
## All sudo entries will generate a log entry in /var/log/system
## 
###############################################################################

###############################################################################
# V A L I D A T E 
###############################################################################

if [ -z "$1" ]; then
	echo "Usage: $0 <filename>"
	exit 1
else
	COMMAND="$1"
fi

if [ -n "$2" ]; then
	LOGTAG=$2
else
	LOGTAG="addsudo"
fi

###############################################################################
# M A I N
###############################################################################

if ! grep 'webconfig ALL' /etc/sudoers > /dev/null; then
    echo "Cmnd_Alias CC = " >> /etc/sudoers
    echo "webconfig ALL=NOPASSWD: CC" >> /etc/sudoers
    chmod 0440 /etc/sudoers
fi

LINE=`grep "^Cmnd_Alias CC" /etc/sudoers 2>/dev/null`
CHECK=`echo $LINE, | grep $1,`
if [ -z "$CHECK" ]; then
	/usr/bin/logger -p local6.notice -t installer "$LOGTAG - adding sudoers entry $1"
	ESCAPE=`echo $1 | sed 's/\//\\\\\//g'`
	sed -i -e "s/Cmnd_Alias CC.*=/Cmnd_Alias CC = $ESCAPE,/i" /etc/sudoers
	sed -i -e "s/[[:space:]]*,[[:space:]]*$//i" /etc/sudoers
	chmod 440 /etc/sudoers
fi
