#! /usr/bin/perl -w
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use strict;

# Update the module include path
BEGIN
{
    my $Dir = $0;
    if ( $Dir =~ /(.*)\/.*/ )
    {
	push @INC, "$1";
    }
}
use HawkeyePublish;
use HawkeyeLib;

# Prototypes
sub Init( );
sub RunIt( );

# Setup the hawkeye stuff
my $Hawkeye;
my $Hash;
my @Params = "";
my $CmdLine = join( " ", $0, @ARGV );
print STDERR "$CmdLine\n";

# Do it
Init();
RunIt();

sub Init()
{
    HawkeyeLib::DoConfig( );

    $Hawkeye = HawkeyePublish->new;
    $Hawkeye->Quiet( 1 );
    $Hawkeye->AutoIndexSet( 1 );

    # Read the config info..
    $Params[0] = HawkeyeLib::ReadConfig( "_Param0", "true" );
    $Params[1] = HawkeyeLib::ReadConfig( "_Param1", "xyzzy" );
    $Params[2] = HawkeyeLib::ReadConfig( "_Param2", 1 );
    $Params[3] = HawkeyeLib::ReadConfig( "_Param3", "/bin/echo");
    $Params[4] = HawkeyeLib::ReadConfig( "_Param4", 0 );

    # Finally, parse the command line...
    foreach my $Arg ( @ARGV )
    {
	# Cluster on/off
	if ( $Arg =~ /-param(\d)=(.*)/ )
	{
	    $Params[$1] = $2;
	}
	else
	{
	    print STDERR "Unknown option '$Arg'\n";
	    print STDERR "Usage: test [name [options]]\n";
	    print STDERR
		"  [name]         \tModules logical name\n" .
		"  [-paramN=X]    \tSet parameter N to X\n" ;
	    exit 1;
	}
    }
}

# Do the real work here...
sub RunIt()
{

    print STDERR "Test module message to stderr\n";

    # Start things off
    $Hash = HawkeyeHash->new( \$Hawkeye, "" );

    my $TestFailFile = "/tmp/hawkeye_test_fail";
    if ( -f $TestFailFile )
    {
	print STDERR "Simulating failure: $TestFailFile exists\n";
	$Hash->Add( "Status", "Failed" );
	$Hash->Store( );
	$Hawkeye->Publish( );
    }

    $Hash->Add( "Status", "OK" );
    $Hash->Add( "CmdLine", HawkeyePublish::TypeString, $CmdLine );
    $Hash->Add( "ModuleName", HawkeyePublish::TypeString,
		HawkeyeLib::GetModuleName() );

    my $StartdNameEnv = "STARTD_CRON_NAME";
    my $StartdName = "Hawkeye";
    my $ConfigVal = "hawkeye_config_val";
    my $InterfaceVersionEnv = "HAWKEYE_INTERFACE_VERSION";
    my $InterfaceVersion = 0;
    if ( exists $ENV{$StartdNameEnv} )
    {
	$StartdName = $ENV{$StartdNameEnv};
	my $NameUc = uc( $StartdName );
	$InterfaceVersionEnv = $NameUc . "_INTERFACE_VERSION";
	$ConfigVal = "condor_config_val" if ( $NameUc ne "HAWKEYE" );
    }
    $InterfaceVersion = $ENV{$InterfaceVersionEnv}
	if ( exists $ENV{$InterfaceVersionEnv} );

    $Hash->Add( "StartdName", HawkeyePublish::TypeString, $StartdName );
    $Hash->Add( "InterfaceVersion", HawkeyePublish::TypeNumber,
		$InterfaceVersion );
    $Hash->Add( "StartdName", HawkeyePublish::TypeString, $StartdName );

    my $cvp = `/usr/bin/which $ConfigVal`; chomp $cvp;
    $cvp = $ConfigVal if ( $cvp eq "" );
    $Hash->Add( "ConfigValProgram", HawkeyePublish::TypeString, $cvp );
    foreach my $Env ( keys %ENV )
    {
	$Hash->Add( "Env_" . $Env, HawkeyePublish::TypeAuto, $ENV{$Env} );
    }

    foreach my $Num ( 0 .. $#Params )
    {
	$Hash->Add( "Param$Num", HawkeyePublish::TypeAuto, $Params[$Num] );
    }

    # Simle test values
    $Hash->Add( "zero", HawkeyePublish::TypeNumber, "0" );
    $Hash->Add( "one", HawkeyePublish::TypeNumber, "1" );
    $Hash->Add( "True", HawkeyePublish::TypeBool, "True" );
    $Hash->Add( "False", HawkeyePublish::TypeBool, "False" );
    $Hash->Add( "string", HawkeyePublish::TypeString, "string #1" );
    $Hash->Add( "string2", HawkeyePublish::TypeAuto, "string #2" );
    $Hash->Add( "string3", HawkeyePublish::TypeAuto, "3 3" );
    $Hash->Add( "string4", HawkeyePublish::TypeAuto, "4 strings" );
    $Hash->Add( "string5", HawkeyePublish::TypeAuto, "5strings" );
    $Hash->Add( "pi", HawkeyePublish::TypeAuto, "3.1415926" );
    $Hash->Add( "IP", HawkeyePublish::TypeAuto, "192.168.1.1" );
    $Hash->Add( "three", HawkeyePublish::TypeAuto, "3" );
    $Hash->Add( "point_two_percent", HawkeyePublish::TypeAuto, ".2%" );
    $Hash->Add( "three_percent", HawkeyePublish::TypeAuto, "3%" );
    $Hash->Add( "four_percent", HawkeyePublish::TypeAuto, "4.0%" );
    $Hash->Add( "five_percent", HawkeyePublish::TypePercent, .05 );

    # Ok, summary is done...
    $Hash->Store( );
    $Hawkeye->Publish( );

    print STDERR "Test module done\n";
}
