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

# Version info, dirs, etc
my $HawkeyeVersion = "";
my $HawkeyeCpu = "";
my $HawkeyeOs = "";
my $HawkeyeLib = "";
my $TempNum = $$;
my $HawkeyeRelease = "hawkeye_release";
my @BinDirs = ( "bin", "sbin" );
my $Modules = "hawkeye_modules";
my $ModulesTarget = "modules";
my $Install = "install";
my $Examples = "condor_examples";
my $CondorRel = "condor_release";
my $Scripts = "condor_scripts";
my $ReleaseDir = "../Hawkeye-Release";
my $TestMode = 0;

# Script version, etc
my $Version = "0.3.1";
my $Usage = "$0 [-t|-T] version os cpu lib\n".
  "Example: $0 1.2.3 linux x86 glibc23-dynamic\n";

# Env variable overrides the dirctory from the file...
if ( exists $ENV{HAWKEYE_RELEASE_DIR} )
{
    $ReleaseDir = $ENV{HAWKEYE_RELEASE_DIR};
}

# Prototypes
sub CopyAll( $$$$ );

# Process command line..
my @RelOpts;
foreach my $Arg ( @ARGV )
{
    # Option (no options supported for now)
    if ( $Arg =~ /^-/ )
    {
      SWITCH:
	{
	    $Arg =~ /^-t$/ && do { $TestMode = 1; last SWITCH; };
	    $Arg =~ /^-T$/ && do { $TestMode = 2; last SWITCH; };
	    $Arg =~ /^-n=(\d+)$/ && do { $TempNum = $1; last SWITCH; };
	    print STDERR "$Usage"; exit 1;
	}
    }
    else
    {
	push @RelOpts, $Arg;
    }
}

# Temp directory to use
my $Temp = "../tmp-$TempNum";

# Pull the info from the commandline
 if ( $#RelOpts != 3 )
{
    print STDERR "$Usage"; exit 1;
}
$HawkeyeVersion = shift( @RelOpts );
$HawkeyeOs = shift( @RelOpts );
$HawkeyeCpu = shift( @RelOpts );
$HawkeyeLib = shift( @RelOpts );

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

# Temp better not exist!
die "$Temp exists" if ( -d $Temp );

# Check modules dir
die "No '$Modules' dir!" if ( ! -d $Modules );
die "'$Modules' is empty!" if ( ! -f "$Modules/df" );

my $ReleaseTarball;
{
    my $TarBall = sprintf( "Hawkeye-%s-%s-%s-%s.tar.gz",
			   $HawkeyeVersion, $HawkeyeOs,
			   $HawkeyeCpu, $HawkeyeLib );
    $ReleaseTarball = "$ReleaseDir/$TarBall";
}
die "$ReleaseTarball exists!" if ( -f $ReleaseTarball );

# Make the hawkeye release dir
print "Creating the Hawkeye directory..\n";
die "Can't make hawkeye!" if ( system( "./condor_scripts/make_hawkeye" ) );

# Make the temp directory...
mkdir( $Temp, 0755 ) || die "Can't create $Temp";
mkdir "$Temp/$Install" || die "Can't create $Temp/$Install";

# Let the user know what's going on
print "\n == Filling temp release directory ==\n\n";

# Start populating it..
foreach my $Bin ( @BinDirs )
{
    CopyAll( "$HawkeyeRelease/$Bin", "$Temp/$Bin", "*", undef );
}

# Copy the modules
CopyAll( $Modules, "$Temp/$ModulesTarget", "test", undef );
CopyAll( $Modules, "$Temp/$ModulesTarget", "*pm", undef );

# Copy the installation tools & rename them
my @Scripts;
CopyAll( $Scripts, "$Temp/sbin", "hawkeye_install_*", \@Scripts );
foreach my $File ( @Scripts )
{
    rename( "$Temp/sbin/$File", "$Temp/sbin/$File.pl" );
}

# Copy the module installer, configs, etc.
CopyAll( $Examples, "$Temp/$Install", "*hawkeye*", undef );

# Rename 'em to .example'
foreach my $File ( split ( /\s+/, `echo $Temp/$Install/*` ) )
{
    if ( -f $File )
    {
	rename ( $File, "$File.example" );
    }
}

# Test Mode
do { print "\n-- TestMode -> $Temp; done --\n"; exit; } if ( $TestMode == 1 );

# Create the release directory
mkdir( $ReleaseDir, 0755 ) if ( ! -d $ReleaseDir );

# Directory to create the final tarball in...
my $AssembleDir = $Temp . "/" . "hawkeye-" . $HawkeyeVersion;

# Finally, make the damn tarball
my $BinTarBall = "release.tar";
{
    my $TarBallPath = "$ReleaseDir/$BinTarBall";
    print "\nCreating tarball '$TarBallPath'...  ";
    system( "tar -C $Temp -cf $TarBallPath ." );
    print "Done\n";
    system( "ls -l $TarBallPath" );

    # Blow away tmp, and move the newly created tarball there
    system( "rm -r $Temp" );
    mkdir( "$Temp", 0755 ) || die "Can't create $Temp";
    mkdir( "$AssembleDir", 0755 ) || die "Can't create $AssembleDir";
    system( "mv $TarBallPath $AssembleDir" );
}

# Assemble the final tarball pieces

# Copy in the last minute pieces..
CopyAll( $CondorRel, $AssembleDir, "*hawkeye*", undef );

# Create the install script
{

    # Copy the installation script
    system( "cp $Scripts/hawkeye_install $AssembleDir/hawkeye_install.pl" );
    my $Install = "$AssembleDir/hawkeye_install";
    open( OUT, ">$Install" ) || die "Can't write to '$Install'\n";
    print OUT "#! /bin/sh\n";
    print OUT "\$0.pl \"$BinTarBall\" \$*\n";
    close OUT;
    chmod( 0755, $Install );
}

# Test Mode
do { print "\n-- TestMode -> $Temp; done --\n"; exit; } if ( $TestMode == 2 );

# Now, make the release tarball...
{
    print "\nCreating release tarball '$ReleaseTarball'...  ";
    system( "tar --gzip -C $Temp -cf $ReleaseTarball ." );
    print "Done\n";
    system( "ls -l $ReleaseTarball" );
    system( "tar --gzip -tvf $ReleaseTarball" );
}

# Clean up the temp dir
print "Cleaning up the $Temp directory...\n";
system( "rm -r $Temp" );
exit 0;

# Stupid subroutine to copy files
sub CopyAll( $$$$ )
{
    my ( $SrcDir, $DestDir, $FilePat, $List ) = @_;
    print "Copy/Link '$FilePat' from '$SrcDir' to '$DestDir':\n";
    if ( ! -d $DestDir )
    {
	mkdir( "$DestDir", 0755 ) || die "Can't create $DestDir";
    }
    my @FileList = split( /\s+/, `cd $SrcDir; /bin/ls -1 $FilePat` );
    foreach my $File ( @FileList )
    {
	next if ( ! -f "$SrcDir/$File" );
	print "   $SrcDir/$File -> $DestDir:  ";
	push( @{$List}, $File ) if ( defined $List );

	if ( ( -l $SrcDir ) || ( -l "$SrcDir/$File" ) )
	{
	    system( "/bin/cp $SrcDir/$File $DestDir" );
	    chmod( 0755, "$DestDir/$File" ) if ( -x "$SrcDir/$File" );
	    print "Copy done\n";
	}
	elsif ( -f "$SrcDir/$File" )
	{
	    link "$SrcDir/$File", "$DestDir/$File" || die "link: $!";
	    print "Link done\n";
	}
	else
	{
	    print "\n";
	    die "Don't know how to handle '$SrcDir/$File'\n";
	}
    }
    system "ls -li $DestDir";
}
