#!/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 perl script is used by our build environment to build
# "both_tarballs", which are tarballs that use files from both the
# main release directory, and the contrib directory.  Files from the
# contrib directory are temporarily copied into the release
# directory.  If the release directory already contains one of these
# files (e.g. the condor_starter, where there are both "real" and
# contrib versions), the real version is stashed in a temporary
# location, and moved back in place when everything is finished.
# Once all the right files are assembled, the given tarball is
# created, and the release directory is cleaned up: contrib versions
# are deleted, and if they conflicted with a real version, the real
# version is moved back into place, and the directory tree used to
# save real versions is deleted.  
#
# Written on 1/8/99 by Derek Wright <wright@cs.wisc.edu>

# Process args
$opt = shift;
if( $opt =~ /-cmd/ ) {
    $tar_cmd = shift;
} else {
    die "You must specify the tar command to use for root-owned files with '-cmd'\n";
}
$type = shift;
$type =~ s/,//;
$name = shift;
$name =~ s/,//;
while( $_ = shift ) {
    if( s/,// ) { $done = 1 };
    push @files, $_;
    if( $done ) { last };
}
while( $_ = shift ) {
    push @contribfiles, $_;
}
# Make string versions of these lists for later.
$files = join ' ', @files;
$contribfiles = join ' ', @contribfiles;
# Make some strings we use for directory names
$dir = "$type" . "_dir";
$contrib = "$type" . "_contrib";

# Remove existing tarball, if it's there.
unlink "$dir/$name";

# Make a directory tree to save "real" versions of things if we've got
# both real and contrib versions (e.g. condor_starter).
mkdir( "$dir/save", 0777 );
foreach $i ( "bin", "sbin", "lib", "etc" ) {
    mkdir( "$dir/save/$i", 0777 );
}

# Move real versions out of the way, if they exist, and copy in needed
# contrib versions.
foreach $file ( @contribfiles ) {
    if( -f "$dir/$file" ) {
	`mv -f $dir/$file $dir/save/$file`;
    }
    `cp -f $contrib/$file $dir/$file`;
}

# Actually make the tarball
chdir( "$dir" );
`$tar_cmd -cvf $name $files $contribfiles`;
chdir( ".." );

# Do the cleanup
foreach $file ( @contribfiles ) {
    unlink "$dir/$file";
    if( -f "$dir/save/$file" ) {
	`mv -f $dir/save/$file $dir/$file`;
    }
}
`rm -rf $dir/save`;

