#! /usr/bin/env sh

#
# -----------------------------------------------------------------------
#
#    Copyright (c) FEDERAL EXPRESS CORPORATION 2001
#    This document contains information proprietary and
#    confidential to Federal Express Corporation and may
#    not be copied or reproduced without permission.
#
# -----------------------------------------------------------------------
# 
#    $Id: test_in_compile_farm,v 1.5 2006/01/08 20:33:26 bobbynations Exp $
# 
# OWNER:  
#
#    Business Integration Services
# 
# -----------------------------------------------------------------------

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


############################################################################
#
# Standard Function Definitions
#
############################################################################
clean_up() {
    echo "cleaning up after myself"

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

        for file_or_dir in ${LITTER}
        do
            log TRACE  "removing [${file_or_dir}]"
            rm -rf ${file_or_dir}
        done
    fi
}
premature_end() {
    echo "hold your horses while I clean up after myself"
    clean_up
    exit
}
show_version() {
    echo "${PROGNAME} v$VERSION"
    exit 1
}
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
}
show_usage() {
    show_help
}
show_man() {
    show_help pod2text
}
log() {
    level_as_string="$1"
    level_as_nbr=`eval echo "$"$1""`
    [ "${VERBOSITY}" -ge "${level_as_nbr}" ] && echo "[$level_as_string] $2"
}
set_globals() {
    TRUE=0
    FALSE=1
    EXECUTE=${TRUE}
    REVISION='$Revision: 1.5 $'
    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
#
############################################################################

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 'tnvVhmf:' OPTION
do
    case "$OPTION" in
        t)  performUnitTests;;   
        f)  HAWK_VARS_FILE="${OPTARG}";;
        n)  EXECUTE=${FALSE};;     # Don't actually execute any commands
        v)  let VERBOSITY=VERBOSITY+1;;
        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 "\$@=[$@]"

#
# loop through each compile farm server and run the test scripts there
# 

today=`date +'%a %b %d, %Y'`
web_page=./nightly-build.php

cat > ${web_page} <<END_OF_HEADER
<?php include "top.html" ?>

<div id="main-copy">
  <div class="rowOfBoxes">
    <div class="twoThirds noBorderOnLeft">
END_OF_HEADER

#
# taken from /etc/compilefarm-hosts originally.  not all of them work
# though, so I whittled out the ones that didn't
#
#      ppc-osx1, ppc-osx2, shell, x86-freebsd1, openpower-linux1
#
hosts="alpha-linux1 amd64-linux1 amd64-linux2           \
       openpower-linux1 sparc-solaris1 sparc-solaris2   \
       x86-linux1 x86-linux2 x86-netbsd1 x86-openbsd1   \
       x86-solaris1"

for cfserver in $hosts
do
    log DEBUG "cfserver=[${cfserver}]"
    test $EXECUTE -eq $TRUE && arch=`ssh bobbynations@${cfserver} uname -ms` 2>/dev/null
    log DEBUG "arch=[${arch}]"

    echo "      <h2>${cfserver} ($arch)</h2>"   >> ${web_page}
    echo "      <div class='machine-result'>"   >> ${web_page}

    test $EXECUTE -eq $TRUE && \
        ssh bobbynations@${cfserver} \
            /home/users/b/bo/bobbynations/ShUnit/shUnitTest >> ${web_page} 2>&1

    test $EXECUTE -eq $TRUE && \
        ssh bobbynations@${cfserver} \
            /home/users/b/bo/bobbynations/ShUnit/shUnitPlusTest >> ${web_page} 2>&1

    echo "    </div>" >>  ${web_page}

done

cat >> ${web_page} <<END_OF_FOOTER
  </div>

  <div class="oneThird"><?php include "news.html"?></div>
</div>

<?php
  \$filename = preg_replace( '/.*\//', '', \$_SERVER['SCRIPT_NAME'] );
  if (file_exists(\$filename)) {
    \$last_mod_date = date ("F d, Y", filemtime(\$filename) );
  }
  include "bottom.html"
?>
END_OF_FOOTER

#
# clean up after ourselves
#
clean_up

exit


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

=pod

=head1 NAME 

initTIBCO - start/stop script for all TIBCO processes on a box.

=head1 SYNOPSIS

% ./initTIBCO [-h] [-m] [-n] [-v] [-V] [-t] start|stop

=head1 DESCRIPTION

This script is used to start and stop the TIBCO processes on a machine
and is suitable for inclusion in the system rc scripts (i.e. /etc/init.d).i

When stopping the processes, it will check the hawk rulebases found in the 
autoconfig directory and bring down the administrator or the rvhma adapter
if it finds the correct hawk rulebases there.  It will also read through the
hawk_vars.cfg file and bring down the engines configured therein.

=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<-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 DIAGNOSTICS


=head1 BUGS

But of course not!

=head1 AUTHOR

Business Integration Services (http://www.bis.fedex.com)


=cut

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