#! /s/std/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;

# Things we know about
my $Hawkeye = "hawkeye_release";
my $Release = "release_dir";
my @BinDirs = ( "bin", "sbin" );
my @BinList = (
		"condor",
		"condor_config_val",
		"condor_status",
		"condor_version",
		"condor_advertise",
		"condor_collector",
		"condor_fetchlog",
		"condor_master",
		"condor_master_off",
		"condor_off",
		"condor_on",
		"condor_reconfig",
		"condor_restart",
		"condor_startd",
		"condor_updates_stats",
	       );

# Are we in the right home?
die "No '$Release' dir!" if ( ! -d $Release );

# Process command line..
my $DoCopy = 0;
foreach my $Arg ( @ARGV )
{
    if ( $Arg eq "-c" )
    {
	$DoCopy = 1;
    }
}

# Blow away the current Hawkeye dir
if ( -d $Hawkeye )
{
    system( "rm -fr $Hawkeye" );
}
die "Can't create $Hawkeye" if ( ! mkdir( $Hawkeye, 0755 ) );

# Make stdout unbufferred...
$| = 1;

# Rebuild it
foreach my $Bin ( @BinDirs )
{
    my $RelBin = "$Release/$Bin";
    my $HawkBin = "$Hawkeye/$Bin";
    die "Can't create $HawkBin" if ( ! mkdir( $HawkBin, 0755 ) );
    my @Files = split( /\s+/, `/bin/ls -1 $RelBin` );
  FILE:
    foreach my $File ( @Files )
    {
	my $Want = 0;
	foreach my $Bin ( @BinList )
	{
	    $Want++ if ( $File eq $Bin );
	}
	next FILE if ( ! $Want );

	# Create the actual names
	my $CondFile = "$RelBin/$File";
	my $Tmp = $File;
	$Tmp =~ s/condor/hawkeye/;
	my $HawkFile = "$HawkBin/$Tmp";
	print "$CondFile -> $HawkFile:  ";

	# Copy / link 'em
	if ( $DoCopy )
	{
	    if ( system( "cp $CondFile $HawkFile" ) )
	    {
		print "ERROR\n";
		next;
	    }
	    print "Ok\n";
	}
	else
	{
	    if ( ! link( $CondFile, $HawkFile ) )
	    {
		print "ERROR: $!\n";
		next;
	    }
	    print "Ok\n";
	}
    }
}
