#!/usr/bin/env perl

##**************************************************************
##
## 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.
##
##**************************************************************



# This script performs all steps necessary to take the release.tar
# files for whatever platforms you want and put them onto the web as
# the completed binary release with all the right support files, name,
# etc, etc.  Read the README file in this directory for details on how
# to release a new patch level or revision of Condor.
#
# This should be run in the top-level src directory.
#

# Autoflush the output
$| = 1;

# Deal with any command-line args we are passed:
while( $_ = shift( @ARGV ) ) {
  SWITCH: {	
    if( /-s.*/ ) {
	$sys = shift(@ARGV);
	last SWITCH;
    }
    if( /-v.*/ ) {
	$versarg = shift(@ARGV);
	last SWITCH;
    }
    if( /-t.*/ ) {
        $target = shift(@ARGV);
        last SWITCH;
    }
    if( /-l.*/ ) {
	$libc = shift(@ARGV);
	last SWITCH;
    }
  }
}

# Now, grab the version of this release.
if( $versarg ) {
    $_ = $versarg;
} else {
    # If we weren't told on the command line, try to run ident
    # directly on the condor_master binary.
    if( -f "strip_dir/sbin/condor_master" ) {
        $_=`strip_dir/sbin/condor_master -version`;
    } elsif ( -f "static_dir/sbin/condor_master" ) {
        $_=`static_dir/sbin/condor_master -version`;
    } else {
	die "ERROR: can't find a condor_master, and no -v specified";
    }
}
/.*\$CondorVersion: (\d*)\.(\d*)\.(\d*).*/;
$majorv = $1;
$minorv = $2;
$releasev = $3;
$version = "$majorv.$minorv.$releasev";

print "Extracted $version from Condor**************************************************************\n";
# Things you should probably leave alone:
require 5.002;

######################################################################
# Settings you may want to customize
######################################################################

# These are the modules you actually want to build.
@modules = ( "condor", "ckpt_server", "sdk","eventd", "dagman", "condor-g" );
#@modules = ( "smp_startd", "ckpt_server", "sdk" );
#@modules = ( "condor", "ckpt_server", "mpi" );
#@modules = ( "view_server", "ckpt_server" );
#@modules = ( "condor" );

# List any special files, other than README, DOC, INSTALL, and
# LICENSE.TXT (which are included in all releases) that you want in
# the skeleton for a given module.
%skelfiles = (
  "condor"   => [ "condor_configure", "examples", ],
);

# If any of the files you specify aren't found in the $realskel
# directory you specify, put the relative path to where your file can
# be found here.
%filesource = (
  "condor_configure" => "condor_scripts/condor_configure",
);	   

# If the tar file for this module has a different name than
# "$module.tar", specify that here.
%tarfile = (
  "condor" => "release.tar",
);	   

# English descriptions of the modules, used in the README, etc. 
%module_descriptions = (
  "condor"        => "Main release",
  "view_client"   => "CondorView client contrib module",
  "dagman"        => "DagMan contrib module",
  "ckpt_server"   => "Checkpoint Server contrib module",
  "mpi"           => "MPI contrib module",
  "condor-g"      => "Condor-G technology",
  "sdk"           => "software developer's kit",
  "eventd"        => "Condor Eventd contrib module"
);	   

######################################################################
# The rest of these settings can probably stay just like they are.
######################################################################

# Some paths of interest:
$skel="release_skeleton";
$realskel="condor_release";

%releaselocation = (
  "static"        => "static_dir",
  "dynamic"       => "strip_dir"
);

# Convert from AFS @sys names to our names for the tarballs.
%opsysname = (
  "i386_linux2"   => "linux",
  "i386_linux22"  => "linux",
  "i386_linux3"   => "linux",
  "i386_rh62"     => "linux",
  "i386_rh71"     => "linux",
  "i386_rh72"     => "linux",
  "ia64_linux24"  => "linux"
);

%archname = (
  "i386_linux2"   => "x86",
  "i386_linux22"  => "x86",
  "i386_rh62"     => "x86",
  "i386_rh71"     => "x86",
  "i386_rh72"     => "x86",
  "i386_linux3"   => "x86",
  "ia64_linux24"  => "ia64"
);


%libcname = (
  "i386_linux2"   => "libc5",
  "i386_linux22"  => "glibc21",
  "i386_linux3"   => "glibc20",
  "i386_rh61"   => "glibc21",
  "i386_rh62"   => "glibc21",
  "i386_rh71"   => "glibc22",
  "i386_rh72"   => "glibc22",
  "ia64_linux24" => "glibc22"
);


umask(022);

######################################################################
# Actual work begins...
######################################################################

if( ! $target) {
   $target="/p/condor/public/binaries";
}

# Make sure we know what platform we are... If we weren't told on the
# command-line, run "sys" and use that.
if( ! $sys ) {
    $sys=`sys`;
    chomp($sys);
} 
$arch = $archname{$sys};
$opsys = $opsysname{$sys};
$libc = $libcname{$sys};

`rm -rf $skel`;
mkdir( "$skel", 0777 );

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
$mon++;
$date="$mon/$mday/$year";

foreach $module ( @modules ) {

    $release="$module-$version";
    if( $tarfile{$module} ) {
	$tarfile=$tarfile{$module};
    } else {
	$tarfile="$module.tar";
    }

    # Setup skeleton directory
    print "Setting up skeleton directory for \"$module\"... ";
    mkdir( "$skel/$release", 0777 );

    # You always want the same version of LICENSE.TXT in everything. 
    `cp -f $realskel/LICENSE.TXT $skel/$release`;

    # Now, you always want a README, DOC, and INSTALL
	foreach $file ( "README", "INSTALL", "DOC" ) {
	    `cp -f $realskel/$file $skel/$release/`;
    } 

    # Now, handle any special case files for a given module
    foreach $file ( @{$skelfiles{$module}} ) {
		if( $filesource{$file} ) {
	   	 $src = $filesource{$file};
		} else {
	   	 $src = "$realskel/$file";
		}
		if( $filedest{$file} ) {
	   	 $dest = $filedest{$file};
		} else {
	   	 $dest = $file;
		}
		# under linux, use the GNU fileutils flags
		if( $libc =~ /.*glibc22.*/ ) {	
			`cp -rL $src $skel/$release/$dest`;
		} else {
			`cp -r $src $skel/$release/$dest`;
		}	
    }
    `rm -rf $skel/$release/CVS`;
    `rm -rf $skel/$release/*/CVS`;
    `rm -rf $skel/$release/*/*/CVS`;

    print "done.\n";

    # Now, handle static and/or dynamic versions, whatever exists... 

    foreach $linktype ( "static", "dynamic" ) {
	# Make sure the proper tar file exists
	if( ! (-d $releaselocation{$linktype} && 
	       -f "$releaselocation{$linktype}/$tarfile") ) {
	    print STDERR "There's no $tarfile file for $linktype on this platform, skipping.\n";
	    next;
	}

	# Figure out what our filenames should look like.
	$platform="$opsys-$arch";
	if( $libc ) {
	    $platform = "$platform-$libc";
	}
	if( $linktype =~ /dynamic/ ) {
	    $platform = "$platform-dynamic";
	}

	# Copy the tarfile with the right permissions into the skeleton. 
	`cp $releaselocation{$linktype}/$tarfile $skel/$release/$tarfile`;

	# Create full archive 
	chdir( $skel ) || die "Can't chdir to $skel: $!\n";
	print "Creating full archive: $release.tar\n";
	print `tar -cvf $release.tar --owner=root --group=0 $release`;
	chdir( ".." ) || die "Can't chdir to ..: $!\n";

	# Delete the release.tar we're now done with.
	unlink( "$skel/$release/$tarfile" );

	# Fix ownerships in the full archive.
	`cp $skel/$release.tar $skel/$release-$platform.tar`;

	# Delete the tar file we're now done with.
	unlink( "$skel/$release.tar" );

	# Figure out where we want put this tarball
	if( $module =~ /condor/ ) {
	    $targetdir = "$target/v$majorv.$minorv";
	} else {
	    $targetdir = "$target/contrib";
	}
	$targetfile = "$targetdir/$release-$platform.tar";

	# Make sure there's nothing in the way where we want to put it.
	unlink( "$targetfile", "$targetfile.Z", "$targetfile.gz" );

	# Compress as appropriate, and move to the final location.
	if( $platform =~ /.*linux.*/ ) {
	    print "gzipping $skel/$release-$platform.tar\n";
	    system( "gzip $skel/$release-$platform.tar" );
	    print "Moving $release-$platform.tar.gz into $targetdir ... ";
	    `mv $skel/$release-$platform.tar.gz $targetdir`;
	    print "done.\n";
	} else {
	    print "compressing $skel/$release-$platform.tar\n";
	    system( "compress $skel/$release-$platform.tar" );
	    print "Moving $release-$platform.tar.Z into $targetdir ... ";
	    `mv $skel/$release-$platform.tar.Z $targetdir`;
	    print "done.\n";
	}
    }
}

sub customize {
    local( $source, $target, $module, $tarfile ) = @_;
    local( $description ) = $module_descriptions{$module};

    open( SRC, "<$source" ) || die "\nCan't open $source for reading.\n";
    open( TARGET, ">$target" ) || die "\nCan't open $target for writing.\n";

    while( <SRC> ) {
	s/#module#/$module/;
	if( length($tarfile) < 8 ) {
	    s/#tarfile#/$tarfile\t/;
	} else {
	    s/#tarfile#/$tarfile/;
	}
	s/#description#/$description/;
	s/#date#/$date/;
	print TARGET $_;
    }
    close( SRC );
    close( TARGET );
}
    


