#!/bin/sh
# $Log: controlsync.in,v $
# Revision 1.1  2019-05-29 00:47:57+05:30  Cprogrammer
# Initial revision
#
#
#
# utility to administer control files
#
# $Id: controlsync.in,v 1.1 2019-05-29 00:47:57+05:30 Cprogrammer Exp mbhangui $
#
usage ()
{
	echo "Wrong Usage"
	echo "$0 [options]"
	echo "-h host (relay host)"
	echo "-D domain"
	echo "-A line (Add line to file)"
	echo "-R line (Remove line from file)"
	echo "-b      (batch mode)"
	echo "-v      (Display)"
	echo "-u      adminUsername"
	echo "-P      adminPasswd"
	echo "-p      adminPort"
	echo "-f      filename"
	echo "-a      (Add Relay)"
	echo "-d      (Delete Relay)"
	
}

args=$*
batch_mode=0
options=""
while getopts adbvh:D:R:A:u:P:p:f: opt
do
	case $opt in
		h) host="$OPTARG";;
		D) domain="$OPTARG";;
		A) operation=0;options="$options -u $OPTARG";addline="$OPTARG";;
		R) operation=0;options="$options -d $OPTARG";delline="$OPTARG";;
		b) batch_mode=1;;
		v) operation=0;options="$options -s";;
		u) adminuser="$OPTARG";;
		P) adminpass="$OPTARG";;
		p) adminport="$OPTARG";;
		f) filename="$OPTARG";;
		a) operation=1;host="$OPTARG";;
		d) operation=2;host="$OPTARG";;
		?) usage $0;exit 1;;
	esac
done

if [ " $operation" = " " ] ; then
	echo "Must use either of -A, -R, -a, -d"
	usage
	exit 1
fi
case $operation in
	0)
	if [ " $host" = " " ] ; then
		echo "missing -h option"
		usage
		exit 1
	fi
	if [ " $adminuser" = " " ] ; then
		echo "missing -u option"
		usage
		exit 1
	fi
	if [ " $adminpass" = " " ] ; then
		echo "missing -P option"
		usage
		exit 1
	fi
	if [ " $adminport" = " " ] ; then
		echo "missing -p option"
		usage
		exit 1
	fi
	if [ " $filename" = " " ] ; then
		echo "missing -f option"
		usage
		exit 1
	fi
	basefile=`/bin/basename $filename`
	if [ " $batch_mode" -eq " 1" ] ; then
		if [ " $CONTROLDIR" = " " ] ; then
			cat /etc/indimail/control/$basefile | while read line
			do
				/usr/sbin/adminclient -h $host -p $adminport -u $adminuser -P $adminpass -c "/usr/libexec/indimail/updatefile -u $line $filename"
				ret=$?
				if [ $ret -ne 0 ] ; then
					exit 1
				fi
			done
		else
			slash=`echo $CONTROLDIR | cut -c1`
			if [ ! " $slash" = " /" ] ; then
				cd /var/indimail
			fi
			cat $CONTROLDIR/$basefile | while read line
			do
				/usr/sbin/adminclient -h $host -p $adminport -u $adminuser -P $adminpass -c "/usr/libexec/indimail/updatefile -u $line -s $filename"
				ret=$?
				if [ $ret -ne 0 ] ; then
					exit 1
				fi
			done
		fi
		exit $ret
	else
		exec /usr/sbin/adminclient -h $host -p $adminport -u $adminuser -P $adminpass -c "/usr/libexec/indimail/updatefile $options $filename"
	fi
	;;
	1|2)
	if [ " $host" = " " ] ; then
		echo "missing -h option"
		usage
		exit 1
	fi
	if [ " $domain" = " " ] ; then
		echo "missing -h option"
		usage
		exit 1
	fi
	if [ $operation -eq 1 ] ; then
		exec /usr/libexec/indimail/updatefile -u $domain:$host /etc/indimail/control/relayhosts
	else
		exec /usr/libexec/indimail/updatefile -d $domain:$host /etc/indimail/control/relayhosts
	fi
	;;
esac
exit $ret
