#! /bin/sh
##
## $Id: rancid-run.in 2096 2009-06-17 21:49:46Z heas $
##
## rancid 2.3.3
## Copyright (c) 1997-2008 by Terrapin Communications, Inc.
## All rights reserved.
##
## This code is derived from software contributed to and maintained by
## Terrapin Communications, Inc. by Henry Kilmer, John Heasley, Andrew Partan,
## Pete Whiting, Austin Schutz, and Andrew Fort.
##
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions
## are met:
## 1. Redistributions of source code must retain the above copyright
##    notice, this list of conditions and the following disclaimer.
## 2. Redistributions in binary form must reproduce the above copyright
##    notice, this list of conditions and the following disclaimer in the
##    documentation and/or other materials provided with the distribution.
## 3. All advertising materials mentioning features or use of this software
##    must display the following acknowledgement:
##        This product includes software developed by Terrapin Communications,
##        Inc. and its contributors for RANCID.
## 4. Neither the name of Terrapin Communications, Inc. nor the names of its
##    contributors may be used to endorse or promote products derived from
##    this software without specific prior written permission.
## 5. It is requested that non-binding fixes and modifications be contributed
##    back to Terrapin Communications, Inc.
##
## THIS SOFTWARE IS PROVIDED BY Terrapin Communications, INC. AND CONTRIBUTORS
## ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COMPANY OR CONTRIBUTORS
## BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
## POSSIBILITY OF SUCH DAMAGE.
# 
# Run rancid for each of the rancid groups defined by $LIST_OF_GROUPS in
# /etc/rancid.conf or those specified on the command-line.
#

# Default ENVFILE, overrideable with -f flag.
ENVFILE="/etc/rancid.conf"

TMPDIR=${TMPDIR:=/tmp}; export TMPDIR

# control_rancid argv
CR_ARGV=""; export CR_ARGV

# print a usage message to stderr
pr_usage() {
    echo "usage: $0 [-V] [-f config_file] [-r device_name] [-m mail rcpt] [group [group ...]]" >&2;
}

# command-line options
# -V
# -f <config file name>
# -m <mailto address>
# -r <device name>
if [ $# -ge 1 ] ; then

    while [ 1 ] ; do
	case $1 in
	-V)
	    echo "rancid 2.3.3"
	    exit 0
	    ;;
	-f)
	    shift
	    # next arg is the alternate config file name
	    ENVFILE="$1"
	    if [ -z $ENVFILE ]; then
		pr_usage
		exit 1
	    fi
	    shift
	    ;;
	-m)
	    shift
	    # next arg is the mailto name
	    CR_ARGV="$CR_ARGV -m $1"; export CR_ARGV
	    shift
	    ;;
	-r)
	    shift
	    # next arg is the device name
	    CR_ARGV="$CR_ARGV -r $1"; export CR_ARGV
	    shift
	    ;;
	--)
	    shift; break;
	    ;;
	-h)
	    pr_usage
	    exit
	    ;;
	-*)
	    echo "unknown option: $1" >&2
	    pr_usage
	    exit 1
	    ;;
	*)
	    break;
	    ;;
	esac
    done
fi

. $ENVFILE


if [ $# -ge 1 ] ; then
    LIST_OF_GROUPS="$*"; export LIST_OF_GROUPS
elif [ "$LIST_OF_GROUPS" = "" ] ; then
    echo "LIST_OF_GROUPS is empty in $ENVFILE"
    exit 1
fi

if [ ! -d $LOGDIR ] ; then
    mkdir $LOGDIR || (echo "Could not create log directory: $LOGDIR"; exit 1)
fi

for GROUP in $LIST_OF_GROUPS
do

    LOCKFILE=$TMPDIR/.$GROUP.run.lock

    (
	echo starting: `date`
	echo

	if [ -f $LOCKFILE ]
	then
	    echo hourly config diffs failed: $LOCKFILE exists
	    ls -l $LOCKFILE

	    # Send email if the lock file is old.
	    if [ "X$LOCKTIME" = "X" ] ; then
		LOCKTIME=4
	    fi
	    perl -e "\$t = (stat(\"$LOCKFILE\"))[9]; print \"OLD\\n\" if (time() - \$t >= $LOCKTIME*60*60);" > $TMPDIR/.$GROUP.old
	    if [ -s $TMPDIR/.$GROUP.old ]
	    then
		(
		  echo "To: rancid-admin-${GROUP}${MAILDOMAIN}"
		  echo "Subject: rancid hung - $GROUP"
		  echo "Precedence: bulk"
		  echo ""

		  cat <<END
rancid $GROUP hung on `hostname`?  Old lockfile still exists:
`ls -l $LOCKFILE`
END
		) | sendmail -t
	    fi
	    rm -f $TMPDIR/.$GROUP.old
	else
	    trap 'rm -fr $LOCKFILE;exit 1' 1 2 3 6 10 15
	    touch $LOCKFILE
	    if [ $? -eq 0 ] ; then
		control_rancid $CR_ARGV $GROUP
		rm -f $LOCKFILE
	    fi
	    trap ''  1 2 3 6 10 15
	fi

	echo
	echo ending: `date`
    ) >$LOGDIR/$GROUP.`date +%Y%m%d.%H%M%S` 2>&1
done
