1#!/usr/bin/perl -w
2#
3# Licensed to the Apache Software Foundation (ASF) under one or more
4# contributor license agreements.  See the NOTICE file distributed with
5# this work for additional information regarding copyright ownership.
6# The ASF licenses this file to You under the Apache License, Version 2.0
7# (the "License"); you may not use this file except in compliance with
8# the License.  You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18use strict;
19use File::Basename;
20use File::Copy;
21use File::Find;
22use File::Path qw(mkpath);
23
24require 5.010;
25
26my $srcdir;
27my $destdir;
28
29sub process_file {
30    return if $_ =~ /^\./;
31
32    my $rel_to_srcdir = substr($File::Find::name, length($srcdir));
33    my $destfile = "$destdir$rel_to_srcdir";
34
35    if (-d $File::Find::name) {
36        # If the directory is empty, it won't get created.
37        # Otherwise it will get created when copying a file.
38    }
39    else {
40        if (-f $destfile) {
41            # Preserve it.
42        }
43        else {
44            # Create it.
45            my $dir = dirname($destfile);
46            if (! -e $dir) {
47                mkpath($dir) or die "Failed to create directory $dir: $!";
48            }
49            copy($File::Find::name, $destfile) or die "Copy $File::Find::name->$destfile failed: $!";
50        }
51    }
52}
53
54$srcdir = shift;
55$destdir = shift;
56if (scalar(@ARGV) > 0) {
57    my $mode = shift;
58    if ($mode eq "ifdestmissing") {
59        # Normally the check for possible overwrite is performed on a
60        # file-by-file basis.  If "ifdestmissing" is specified and the
61        # destination directory exists, bail out.
62        if (-d $destdir) {
63            print "[PRESERVING EXISTING SUBDIR $destdir]\n";
64            exit(0);
65        }
66    }
67    else {
68        die "bad mode $mode";
69    }
70}
71find(\&process_file, ($srcdir));
72