#!/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 make
# a soft-link directory tree.  The first arg is the name of the
# directory we wish to create, and the 2nd arg is the root of the
# source tree we want to make the links to.  
#
# Written on 7/2/99 by Derek Wright <wright@cs.wisc.edu>

# Process args
$dir = shift;
$root = shift;

# First, remove anything we've got here already
`rm -rf $dir`;

# Let the world know what we're doing, and immediately flush output in
# case of errors.
$| = 1;
print "Creating soft-link directory tree for $dir... ";

# Now, call our function, which we might call recursively, if it's
# actually a directory tree we're creating.
make_dir( $dir, $root );

print "done.\n";
exit(0);

sub make_dir {
    my( $dir, $root ) = @_;
    my( $file, $DIR );

    # First, create the dir:
    mkdir( $dir, 0777 ) || die "\nCan't make directory $dir: $!\n";

    # Now, cd into it:
    chdir $dir;

    opendir( DIR, "$root/$dir" );
    foreach $file ( readdir(DIR) ) {
	# Skip over ".", "..", and "CVS", but allow other dot files
	next if( $file eq "." );
	next if( $file eq ".." );
	next if( $file =~ /^CVS/ );
	next if( $file =~ /~$/ );	# skip emacs backup files
	if( -d "$root/$dir/$file" ) {
	    # It's a directory we're about to create, so just call
	    # ourself recursively to do the job.
	    make_dir( "$file", "$root/$dir");
	} else {
	    # Regular file, make the soft link.
	    symlink( "$root/$dir/$file", $file ) || 
		die "\nCan't link $file to $root/$dir/$file: $!\n";
	}
    }
    # Clean-up
    closedir( DIR );
    chdir "..";
}

