#! /usr/bin/env sh

# -----------------------------------------------------------------------
#
#    $Id: cronjob,v 1.6 2006/01/08 04:16:13 bobbynations Exp $
# 
# -----------------------------------------------------------------------

#
# make sure that everything gets cleaned up if we get interrupted
#
trap premature_end 1 2 15

SF_USER="bobbynations"

#########################################################################
# DO NOT MODIFY BELOW THIS LINE 
#########################################################################


############################################################################
#
# Standard Function Definitions
#
############################################################################
function clean_up {
    log TRACE "cleaning up after myself"

    if [ -n "${LITTER}" ] 
    then
        log DEBUG "LITTER=[${LITTER}]"

        for file_or_dir in ${LITTER}
        do
            log TRACE  "removing [${file_or_dir}]"
            rm -rf ${file_or_dir}
        done
    fi
}
function throw_away {
    test -z ${LITTER} && LITTER="${1}" || LITTER="${LITTER} ${1}"
    log DEBUG "LITTER=[$LITTER]"
}
function premature_end {
    echo "hold your horses while I clean up after myself"
    clean_up
    exit
}
function show_version {
    echo "${PROGNAME} v$VERSION"
    exit 1
}
function show_help {
    HELP_PROG=${1:-pod2usage}
    log DEBUG "HELP_PROG=[$HELP_PROG]"
    if type ${HELP_PROG} > /dev/null 2>&1
    then
        ${HELP_PROG} ${PROGNAME}
        exit
    else
        echo "unable to find ${HELP_PROG} in the PATH"
        exit 1
    fi
}
function show_usage {
    show_help
}
function show_man {
    show_help pod2text
}
function log {
    level_as_string="$1"
    level_as_nbr=`eval echo "$"$1""`
    [ "${VERBOSITY}" -ge "${level_as_nbr}" ] && echo "[$level_as_string] $2"
}
function set_globals {
    TRUE=0
    FALSE=1
    EXECUTE=${TRUE}
    REVISION='$Revision: 1.6 $'
    VERSION=`echo $REVISION | sed -e 's/[A-Za-z: $]//g'`
    HAWK_VARS_FILE='hawk_vars.cfg'

    #
    # the user can adjust the level of output by specifying multiple 'v' 
    # command line arguments with the level equal to the number given.  
    #
    SILENT=0
    TRACE=1
    DEBUG=2
    REALLYDEBUG=3
    VERBOSITY=${SILENT}
}


############################################################################
#
# Regular Function Definitions
#
############################################################################

function set_os_params {
    OS=`uname -s`;            log DEBUG "OS=[$OS]"
    [ -n "${1}" ] && OS=${1}; log DEBUG "OS=[$OS]"

    unset PS
    case ${OS} in
        [Ll][Ii][Nn][Uu][Xx]) PS='ps auwwx';;
        [Hh][Pp]-[Uu][Xx])    PS='ps -efx';;
    esac
    log DEBUG "PS=[$PS]"
}


############################################################################
#
# Main Program
#
############################################################################

#
# we'll need this from some of our functions ... therefore must save it 
# out before calling any of them otherwise it will get set to the function
# name itself, which would hose things up.
#
PROGNAME=${0}
set_globals

#
# handle any command line switches
#
while getopts 'tnvVhmQ' OPTION
do
    case "$OPTION" in
        t)  performUnitTests;;   
        n)  EXECUTE=${FALSE};;     # Don't actually execute any commands
        v)  let VERBOSITY=VERBOSITY+1;;
        Q)  exec 1>/dev/null;;
        V)  show_version;;         # User asked for version information
        h)  show_usage;;           # User asked for help
        m)  show_man;;             # User asked for lotsa help
        \?) show_usage;;           # Unknown argument ... show usage and exit
    esac
done
shift `expr $OPTIND - 1`

#
# process the requested action
#
set_os_params

log DEBUG "\$@=[$@]"

SF_CVS_SERVER="cvs.sourceforge.net"
SF_CVS_REPOSITORY_PATH="/cvsroot/shunit"

COMPILE_FARM_SERVER="cf-shell.sourceforge.net"
SF_USER_HOME_DIR="/home/users/`expr "${SF_USER}" : '\(.\)'`"
SF_USER_HOME_DIR="${SF_USER_HOME_DIR}/`expr "${SF_USER}" : '\(..\)'`"
SF_USER_HOME_DIR="${SF_USER_HOME_DIR}/${SF_USER}"
REGRESSION_TEST_SCRIPT="test_in_compile_farm"
TEST_RESULTS_FILE="nightly-build.php"

WEB_HOST_SERVER="shell.sourceforge.net"
HTDOCS_DIR="/home/groups/s/sh/shunit/htdocs"

TMP_DIR="`basename ${0}`-$$"

log DEBUG "SF_USER=[$SF_USER]"
log DEBUG "COMPILE_FARM_SERVER=[$COMPILE_FARM_SERVER]"
log DEBUG "SF_USER_HOME_DIR=[$SF_USER_HOME_DIR]"
log DEBUG "REGRESSION_TEST_SCRIPT=[$REGRESSION_TEST_SCRIPT]"
log DEBUG "TEST_RESULTS_FILE=[$TEST_RESULTS_FILE]"
log DEBUG "TMP_DIR=[$TMP_DIR]"

mkdir "${TMP_DIR}"

cd "${TMP_DIR}"

cvs -d":ext:${SF_USER}@${SF_CVS_SERVER}:${SF_CVS_REPOSITORY_PATH}" -Q export -D now ShUnit
scp -r ShUnit ${SF_USER}@${COMPILE_FARM_SERVER}:${SF_USER_HOME_DIR}
ssh ${SF_USER}@${COMPILE_FARM_SERVER} chmod 755 ${SF_USER_HOME_DIR}/ShUnit/*Test
ssh ${SF_USER}@${COMPILE_FARM_SERVER} ${SF_USER_HOME_DIR}/ShUnit/${REGRESSION_TEST_SCRIPT}
scp ${SF_USER}@${COMPILE_FARM_SERVER}:${SF_USER_HOME_DIR}/${TEST_RESULTS_FILE} .
ssh ${SF_USER}@${COMPILE_FARM_SERVER} rm -rf ${SF_USER_HOME_DIR}/ShUnit ${SF_USER_HOME_DIR}/${TEST_RESULTS_FILE}
scp ${TEST_RESULTS_FILE} ${SF_USER}@${WEB_HOST_SERVER}:${HTDOCS_DIR}

cd ..

throw_away "${TMP_DIR}"

#
# clean up after ourselves
#
clean_up

exit


############################################################################
#
# Man page section in Perl's POD format
#
############################################################################

=pod

=head1 NAME 

cronjob - cronjob suitable script that regression tests ShUnit and posts
the results to the shunit.sourceforge.net website.

=head1 SYNOPSIS

% ./cronjob [-h] [-m] [-n] [-v] [-V] [-Q] [-t]

=head1 DESCRIPTION

This script will trigger a fresh checkout and regression test of ShUnit in the 
sourceforge.com compile farm.  It will also post the results to 
http://shunit.sourceforge.net/nightly-build.php. 

=head1 OPTIONS

=over

=item B<-h> 

help: get usage information (this man page, actually)

=item B<-n> 

dry-run: don't actually change anything except log messages to STDOUT

=item B<-Q> 

Be very quiet indeed.  When this option is invoked, STDOUT gets redirected
to /dev/null.

=item B<-v> 

produce verbose output.  I<NOTE:> Using multiple '-v' options will increase 
the verbosity of the the output

=item B<-V> 

show the script's version number and exit

=item B<-m> 

show the script's man page and exit

=back

=head1 ENVIRONMENT

=over

=item SHUNIT_HOME

The directory containing the shUnit unit-testing framework script.  It's needed
if you want to run the unit tests.

=head1 BUGS

But of course not!

=head1 AUTHOR

Bobby Nations [BobbyNations@sourceforge.net]

=cut

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# indent-tabs-mode: nil
# End:
# vim: ai et sw=4 syntax=perl:
