#!/bin/sh
#
# install_cron -- install crontab entries for enstore, in conjunction
#                 with ecron command
#
# each crontab entry is in its own file with the name of job.cron
# where "job" is the name of identification and should be the same as
# -p job in ecron's option. The convention is: for example, backup job
# is described in backup.cron which contains the following line
#
# * * * * * $ENSTORE_SBIN/ecron -p backup enstore backup
#
# To install it, just say, install backup.
# install_cron will take $ENSTORE_DIR/sbin/backup.cron, substitute
# "$ENSTORE_SBIN" by the current "$ENSTORE_DIR/sbin", then append it
# to the current crontab and install it.

# read current crontab, remove the comments and existing entries of the
# same names

crontab -l 2> /dev/null | grep -v "no crontab for" | grep -v "^#" > /tmp/enstore_cron

for i in $*
do
	grep -v "\-p $i" /tmp/enstore_cron > /tmp/e_cron_tmp
	mv -f /tmp/e_cron_tmp /tmp/enstore_cron
	sed -e "s:\$ENSTORE_SBIN:$ENSTORE_DIR/sbin:" $ENSTORE_DIR/sbin/$i.cron >> /tmp/enstore_cron
done

crontab /tmp/enstore_cron
rm -rf /tmp/enstore_cron
rm -rf /tmp/e_cron_tmp
