#!/usr/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/bin/cdat/smit/cdat_add_sched_coll.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)69    1.2  src/bos/usr/bin/cdat/smit/cdat_add_sched_coll.sh, cdat, bos720 7/15/11 04:01:14
# This command adds or changes the specified collect in the cdat user cronfile
#

PATH=/bin:/sbin:/usr/bin:/usr/sbin
CAT=cdat.cat
REP=0

usage() {
	echo "Usage: $0 [-a|-c] [-f <node file>] [-n <node list>] [-u <user>] \\
	-i <collect name> [-m '<comment>'] [-g] -t <type> [-o <parameters>] \\
	<minute> <hour> <day of month> <month> <weekday>"
}

if [ $# -lt 11 -o $# -gt 21 ]
then
	# should not happen under smit
	usage
	exit 1
fi
if [ "$1" != "-a" -a "$1" != "-c" ]
then
	# should not happen under smit
	usage
	exit 1
fi

if [ "$1" = "-c" ]
then
	CHG="y"
fi
shift

# this part of code is similar to the smit cdat_collect_data cmd_to_exec code
if [ "$1" != "-f" -a "$1" != "-n" ]
then
	/usr/bin/dspmsg -s 11 cdat.cat 4 \
	"A value must be specified in the entry field
for either \"Node filename\" or \"Remote nodes\".\n"
	exit 1
fi
if [ "$1" = "-f" -a "$3" = "-n" ]
then
	# no -f option if -n option
	shift
	shift
fi
if [ "$1" = "-f" ]
then
	# add -f for each file if multiple files
	LIST="-f $(echo "$2" | sed -e 's/,/ -f /g')"
	shift
	shift
fi
if [ "$1" = "-n" ]
then
	# add -n for each node if multiple nodes (separated by a comma)
	LIST="-n $(echo "$2" | sed -e 's/,/ -n /g')"
	shift
	shift
fi
while (($# > 9))
do
	[ "$1" = "-i" ] && COLLECT="$2"
	LIST="$LIST '$1'"
	shift
done
if [ $# -eq 9 -a "$1" = "-t" -a "$3" = "-o" ]
then
	Tx="$2"
	Px="$4"
	while [ -n "`echo $Tx | grep ,`" ]
	do
		# add -t for each type if multiple types
		[ -z "`echo $Px | grep ,`" ] && Px="$Px,"
		T1=`echo "$Tx" | cut -d, -f1`
		Tx=`echo "$Tx" | cut -d, -f2-`
		P1=`echo "$Px" | cut -d, -f1`
		Px=`echo "$Px" | cut -d, -f2-`
		if [ -n "$P1" ]
		then
			LIST="$LIST -t '$T1,$P1'"
		else
			LIST="$LIST -t '$T1'"
		fi
	done
	if [ -n "$Px" ]
	then
		LIST="$LIST -t '$Tx,$Px'"
	else
		LIST="$LIST -t '$Tx'"
	fi
	shift
	shift
	shift
	shift
else
	while (($# > 5))
	do
		if [ "$1" = "-t" ]
		then
			# add -t for each type if multiple types
			LIST="$LIST -t $(echo "$2" | sed -e 's/,/ -t /g')"
			shift
			shift
		else
			[ "$1" = "-i" ] && COLLECT="$2"
			LIST="$LIST '$1'"
			shift
		fi
	done
fi

# end of parameter list treatment

if [ -z "$COLLECT" ]
then
	# should not happen under smit
	usage
	exit 1
fi

# only allow digit, '-', '*' and ',' in crontab time fields
echo $1$2$3$4$5 | grep "^[0-9\*,\-]*$" >/dev/null
if [ $? -ne 0 ]
then
	dspmsg -s 11 $CAT 9 "Bad syntax in crontab time fields\n"
	exit 1
fi

MUSER=`/usr/lib/cdat/smit/cdat_init_values | sed -n 2p | sed -ne 's/:.*$//p'`
if [ "$CHG" != "y" ]
then
	# check if it already exits in cronfile
	/usr/lib/cdat/smit/cdat_list_sched_coll | \
		grep "^$COLLECT " >/dev/null
	if [ $? -eq 0 ]
	then
		dspmsg -s 11 $CAT 7 "Cannot add collect, already existing\n"
		exit 2
	fi

	crontab -l $MUSER > /tmp/cdat_cronfile$$
	[ $? -ne 0 ] && exit 4
	echo "$1 $2 $3 $4 $5 /usr/bin/cdat collect -q $LIST" >> \
		/tmp/cdat_cronfile$$
	[ $? -ne 0 ] && REP=3
else
	crontab -l $MUSER > /tmp/cdat_cronfile$$
	[ $? -ne 0 ] && exit 4
	# change LIST to avoid next sed error with | use as separator
	LIST="$(echo "$LIST" | sed -e 's:|:\\|:g')"
	cat /tmp/cdat_cronfile$$ 2>/tmp/cdat_error$$ | sed -e \
	"s|^.*/usr/bin/cdat[ 	]*collect[ 	].*[ 	'\"]-i[ 	'\"]*${COLLECT}[ 	'\"].*$|$1 $2 $3 $4 $5 /usr/bin/cdat collect -q $LIST|" 2>>/tmp/cdat_error$$ 1>/tmp/cdat_cronfila$$
	if [ "`wc /tmp/cdat_error$$ | awk '{print $1}'`" -ne 0 ]
	then
		cat /tmp/cdat_error$$ >&2
		rm -f /tmp/cdat_error$$ /tmp/cdat_cronfil?$$
		exit 3
	fi
	rm -f /tmp/cdat_error$$
	mv /tmp/cdat_cronfila$$ /tmp/cdat_cronfile$$
	[ $? -ne 0 ] && REP=3
fi
if [ "$REP" -ne 0 ]
then
	rm -f /tmp/cdat_cronfil?$$
	exit $REP
fi
if [ "$USER" != "$MUSER" ]
then
	su "$MUSER" -c crontab /tmp/cdat_cronfile$$
	[ $? -ne 0 ] && REP=10
else
	crontab /tmp/cdat_cronfile$$
	[ $? -ne 0 ] && REP=5
fi

rm /tmp/cdat_cronfile$$
exit $REP
