eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;

# create_ace_build,v 4.15 2000/05/02 18:53:04 levine Exp
#
# Creates an ACE build tree in directory "build/<build name>" below the current
# directory, which must be an ACE "top level" directory (such as
# $ACE_ROOT).  The build tree directory structure mirrors that of the ACE
# top level directory structure, except that instead of containing any plain
# files, it contains only symlinks to the files in the ACE top level structure.
#
# This program has a similar purpose to "clone", but in addition to
# only creating symlinks (clone creates hard links, by default), this
# script:
# 1) uses relative rather than absolute symlinks,
# 2) tries not to put junk files into the build tree,
# 3) only creates a new tree in a build/ directory below the current,
#    top level ACE directory (it's a feature :-), but it does enforce
#    consistency).
#
# This program can be re-run on a build tree at any time in order to
# update it.  It will add symlinks for newly added files, and remove
# any that are no longer valid.
#
# If the <build name> starts with "build/", that part will be removed
# from it.
#
# The first three lines above let this script run without specifying the
# full path to perl, as long as it is in the user's PATH.
# Taken from perlrun man page.

$usage = "usage: $0 -? | [-a] [-d <directory mode>] [-v] <build name>\n";
$directory_mode = 0777;   #### Will be modified by umask, also.
$verbose = 0;

$source='.';
$absolute= 0;

$perl_version = $] + 0;
if ($perl_version >= 5) {
  #### Use an eval so that this script will compile with perl4.
  eval <<'PERL5_CWD'
  require Cwd;
  sub cwd {
    Cwd::getcwd ();
  }
PERL5_CWD
} else {
  sub cwd {
    local ($pwd);

    chop ($pwd = `pwd`);
    $pwd;
  }
}

####
#### Process command line args.
####
while ($#ARGV >= 0  &&  $ARGV[0] =~ /^-/) {
  if ($ARGV[0] eq '-v') {
    $verbose = 1;
  } elsif ($ARGV[0] eq '-d') {
    if ($ARGV[1] =~ /^\d+$/) {
      $directory_mode = eval ($ARGV[1]); shift;
    } else {
      warn "$0:  must provide argument for -d option\n";
      die $usage;
    }
  } elsif ($ARGV[0] eq '-a') {
    $source = &cwd ();
    $absolute = 1;
  } elsif ($ARGV[0] eq '-?') {
    print "$usage";
    exit;
  } else {
    warn "$0:  unknown option $ARGV[0]\n";
    die $usage;
  }
  shift;
}

die $usage unless $#ARGV == 0;
$build = $ARGV[0];
$build =~ s%^build/%%;        #### remove leading "build/", if any
$build = "build/$build";

if (-e '/usr/bin/find') {
  $find = '/usr/bin/find';
} elsif (-e '/bin/find') {
  $find = '/bin/find';
} else {
  $find = '/find';
}

####
#### Check that we're in an ACE "top level" directory.
####
(-d 'ace' && -d 'include')  ||
  die "$0:  must be in an ACE top level (ACE_ROOT) directory!\n";

####
#### Create build directories, if needed.
####
-d 'build'  ||  mkdir ('build', $directory_mode);
-d "$build"  ||  mkdir ("$build", $directory_mode);

####
#### Get all ACE plain file and directory names.
####
@files = (`$find . -name CVS -prune \\\
        -o -name build -prune -o \\\
    -name '.*obj' -prune -o -name Templates.DB -prune -o \\\
    \\( ! -type l ! -name core ! -name '*.state' ! -name '*.so' \\\
    ! -name '*.[oa]' ! -name '*~' ! -name '.' ! -name '.#*' \\\
    ! -name '*.log' \\) \\\
    -print`);

####
#### Create directories and symlinks to files.
####
foreach $file (@files) {
  chop $file;         #### remove trailing newline (from find command above)
  $file =~ s%^./%%g;  #### excise leading ./ directory component

  if (-d $file) {
     unless (-d "$build/$file") {
       print "mkdir $build/$file, $directory_mode\n" if $verbose;
       mkdir ("$build/$file", $directory_mode);
     }
  } else {
    unless (-e "$build/$file") {
      if (!$absolute) {
        $up = '../..';
        while ($file =~ m%/%g) {
          $up .= '/..';
        }

        print "symlink $up/$file $build/$file\n" if $verbose;
        symlink ("$up/$file", "$build/$file")  ||
          warn "$0: symlink to $build/$file failed\n";
      } else {
        $path = $source . '/' . $file;
        print "symlink $path $build/$file\n" if $verbose;
        symlink ("$path", "$build/$file")  ||
        warn "$0: symlink to $build/$file failed\n";
      }
    }
  }
}

####
#### Find all the symlinks in the build directory, and remove ones
#### that are no longer actually linked to a file.
####
open (LINKS, "$find $build -type l |") ||
  die "$0: cannot find symlinks in $build\n";

while (<LINKS>) {
  chop;
  local @s = stat $_;
  if ($#s == -1) {
    print "Removing $_ \n" if $verbose;
    unlink $_  ||  warn "$0: unlink of $_ failed\n";
  }
}
close (LINKS) ||
  die "$0: cannot close symlinks pipe\n";

####
#### Done: print message.
####
print "\nCompleted creation of $build/.\n";

unless (-e "$build/ace/config.h"  &&
        -e "$build/include/makeinclude/platform_macros.GNU") {
  print "Be sure to setup $build/ace/config.h and\n" .
        "$build/include/makeinclude/platform_macros.GNU symlinks.\n";
}

#### EOF
