#!/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 finds the binary tarballs packaged up by the nightly
# build and makes RPMs out of them by invoking make-condor-rpm.sh on
# each one.  It assumes that "make public" has already finished
# successfully.  
######################################################################

# The first argument must be the full path to the rpm binary we will be using.
if ( !defined($ARGV[0]) || ! -e $ARGV[0] ) {
	die "No full path to rpm command specified!";
}
if (! -x $ARGV[0]) {
	die "rpm command is not executable!";
}
$rpm_cmd = $ARGV[0];

# First, set up a few variables and make sure we can find the script
# we'll eventually need to call to do the actualy RPM creation.
chdir( ".." ) || die "Can't chdir(..): $!\n";
chomp($root = `pwd`);
print "rpm build commmand: $rpm_cmd\n";
print "root: $root\n";
$make_rpm = "$root/src/condor_scripts/make-condor-rpm.sh";
if( ! -f $make_rpm ) {
    die "Can't find make-condor-rpm.sh script!";
}

# Make a temporary build directory in ../public for RPM generation.
chdir "public" || die "Can't chdir(public): $!\n";
if( ! -d "rpm-build" ) {
    mkdir( "rpm-build", 0777 ) || die "Can't mkdir(rpm-build): $!\n";
}
$build_dir="$root/public/rpm-build";
print "build_dir: $build_dir\n";

# Now, figure out what tarballs we're going to be working on.
opendir PUBLIC, "." || die "Can't opendir(.): $!\n";
$vers_dir = "";
foreach $file ( readdir(PUBLIC) ) {
    if( $file =~ /v\d*\.\d*/ ) {
	$vers_dir = $file;
	break;
    }
}
closedir PUBLIC;
if( ! $vers_dir ) {
    die "Can't find directory of public tarballs to make rpms from!";
}
chdir $vers_dir;
opendir VERS_DIR, "." || die "Can't opendir(public/$vers_dir): $!\n";
@tarballs = ();
foreach $file ( readdir(VERS_DIR) ) {
    if( $file =~ /unstrip/ ) {
        next;
    }
    if( $file =~ /condor-\d*\.\d*\.\d*.*/ ) {
	push @tarballs, $file; 
    }
}
closedir VERS_DIR;
if( $#tarballs < 0 ) {
    die "Can't find any public tarballs in $vers_dir!";
}

# Finally, do the real work by invoking make-condor-rpm.sh
foreach $tarball (@tarballs) {
    print "************************************************************\n";
    print "Making rpms out of $tarball\n";
    print "************************************************************\n";
    open( RPM, "$make_rpm $tarball $rpm_cmd $build_dir|" ) 
	|| die "can't open condor-make-rpm.sh as a pipe: $!\n";
    while( <RPM> ) {
	print;
    } 
    close RPM;
}

print "************************************************************\n";
print "All done, cleaning up\n";
print "************************************************************\n";
`rm -rf $build_dir`;
exit 0;

