blob: d693dfe5e507e1893a31f08da9c6dd3370270140 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001#!/bin/sh
2
Dave Barach8d0f2f02018-03-12 09:31:36 -04003# Copyright (c) 2015 Cisco and/or its affiliates.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at:
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Ed Warnickecb9cada2015-12-08 15:45:58 -070017if [ $# -lt 2 ]; then
18 cat - <<EOF
19$0 FROM-DIR TO-DIR ENVIRONMENT
20
21Copies files from one directory to another with possible
22transformations.
23
24Files named FILE.spp will be transformed via the spp preprocessor
25subject to environment definitions. Source FILE.copyimgspp results in
26destination file FILE in the corresponding destination directory.
27
28Files named FILE.copyimgsh are run as shell scripts in (i.e. via chdir)
29the corresponding destination directory (and not copied).
30
31First regular files are copied. Then transformations are preformed.
32Finally, shell scripts are run.
33EOF
34 exit 1;
35fi
36
37FROM_DIR=$1
38TO_DIR=$2
39
40FILTER=" -and -not -name '*~'";
41FILTER="${FILTER} -and -not -name '.*~'";
42FILTER="$FILTER -and -not -path '*/.git*'";
43FILTER="$FILTER -and -not -path '*/.svn*'";
44FILTER="$FILTER -and -not -path '*/.CVS*'";
45
46FROM_FILES=`(cd $FROM_DIR; eval "find . -not -type d $FILTER")`;
47 FROM_DIRS=`(cd $FROM_DIR; eval "find . -type d $FILTER")`;
48
49COPY_FILES=
50SPP_FILES=
51SH_FILES=
52for f in $FROM_FILES; do
53 case $f in
54 *.copyimgspp) SPP_FILES="$SPP_FILES $f" ;;
55 *.copyimgsh) SH_FILES="$SH_FILES $f" ;;
56 *) COPY_FILES="$COPY_FILES $f";;
57 esac
58done
59
60# Make destination directories.
61mkdir -p $TO_DIR;
62if [ "$FROM_DIRS" != "" ]; then
63 for d in $FROM_DIRS; do
64 mkdir -p $TO_DIR/$d;
65 done
66fi
67
68# Copy files
69if [ "$COPY_FILES" != "" ]; then
70 tar -cf - -C $FROM_DIR $COPY_FILES | tar --preserve-permissions -xf - -C $TO_DIR;
71fi
72
73# Use spp to transform any spp files
74if [ "$SPP_FILES" != "" ]; then
75 for f in $SPP_FILES; do
76 d=`dirname $f`;
77 b=`basename $f .copyimgspp`;
78 mkdir -p $TO_DIR/$d;
79 t=$TO_DIR/$d/$b;
80 spp -o $TO_DIR/$d/$b $FROM_DIR/$f || exit 1;
81 done;
82fi
83
84# Now that all files have been copied/created we run any shell scripts
85ABS_FROM_DIR=`(cd $FROM_DIR; pwd)`;
86if [ "$SH_FILES" != "" ]; then
87 # Allow directory to define some functions
88 if [ -f $FROM_DIR/copyimgsh-functions.sh ]; then
89 . $FROM_DIR/copyimgsh-functions.sh ;
90 fi ;
91 for f in $SH_FILES; do
92 d=`dirname $f`;
93 b=`basename $f`;
94 mkdir -p $TO_DIR/$d;
95 (cd $TO_DIR/$d; . $ABS_FROM_DIR/$d/$b) || exit 1;
96 done;
97fi;