Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | if [ $# -lt 2 ]; then |
| 4 | cat - <<EOF |
| 5 | $0 FROM-DIR TO-DIR ENVIRONMENT |
| 6 | |
| 7 | Copies files from one directory to another with possible |
| 8 | transformations. |
| 9 | |
| 10 | Files named FILE.spp will be transformed via the spp preprocessor |
| 11 | subject to environment definitions. Source FILE.copyimgspp results in |
| 12 | destination file FILE in the corresponding destination directory. |
| 13 | |
| 14 | Files named FILE.copyimgsh are run as shell scripts in (i.e. via chdir) |
| 15 | the corresponding destination directory (and not copied). |
| 16 | |
| 17 | First regular files are copied. Then transformations are preformed. |
| 18 | Finally, shell scripts are run. |
| 19 | EOF |
| 20 | exit 1; |
| 21 | fi |
| 22 | |
| 23 | FROM_DIR=$1 |
| 24 | TO_DIR=$2 |
| 25 | |
| 26 | FILTER=" -and -not -name '*~'"; |
| 27 | FILTER="${FILTER} -and -not -name '.*~'"; |
| 28 | FILTER="$FILTER -and -not -path '*/.git*'"; |
| 29 | FILTER="$FILTER -and -not -path '*/.svn*'"; |
| 30 | FILTER="$FILTER -and -not -path '*/.CVS*'"; |
| 31 | |
| 32 | FROM_FILES=`(cd $FROM_DIR; eval "find . -not -type d $FILTER")`; |
| 33 | FROM_DIRS=`(cd $FROM_DIR; eval "find . -type d $FILTER")`; |
| 34 | |
| 35 | COPY_FILES= |
| 36 | SPP_FILES= |
| 37 | SH_FILES= |
| 38 | for f in $FROM_FILES; do |
| 39 | case $f in |
| 40 | *.copyimgspp) SPP_FILES="$SPP_FILES $f" ;; |
| 41 | *.copyimgsh) SH_FILES="$SH_FILES $f" ;; |
| 42 | *) COPY_FILES="$COPY_FILES $f";; |
| 43 | esac |
| 44 | done |
| 45 | |
| 46 | # Make destination directories. |
| 47 | mkdir -p $TO_DIR; |
| 48 | if [ "$FROM_DIRS" != "" ]; then |
| 49 | for d in $FROM_DIRS; do |
| 50 | mkdir -p $TO_DIR/$d; |
| 51 | done |
| 52 | fi |
| 53 | |
| 54 | # Copy files |
| 55 | if [ "$COPY_FILES" != "" ]; then |
| 56 | tar -cf - -C $FROM_DIR $COPY_FILES | tar --preserve-permissions -xf - -C $TO_DIR; |
| 57 | fi |
| 58 | |
| 59 | # Use spp to transform any spp files |
| 60 | if [ "$SPP_FILES" != "" ]; then |
| 61 | for f in $SPP_FILES; do |
| 62 | d=`dirname $f`; |
| 63 | b=`basename $f .copyimgspp`; |
| 64 | mkdir -p $TO_DIR/$d; |
| 65 | t=$TO_DIR/$d/$b; |
| 66 | spp -o $TO_DIR/$d/$b $FROM_DIR/$f || exit 1; |
| 67 | done; |
| 68 | fi |
| 69 | |
| 70 | # Now that all files have been copied/created we run any shell scripts |
| 71 | ABS_FROM_DIR=`(cd $FROM_DIR; pwd)`; |
| 72 | if [ "$SH_FILES" != "" ]; then |
| 73 | # Allow directory to define some functions |
| 74 | if [ -f $FROM_DIR/copyimgsh-functions.sh ]; then |
| 75 | . $FROM_DIR/copyimgsh-functions.sh ; |
| 76 | fi ; |
| 77 | for f in $SH_FILES; do |
| 78 | d=`dirname $f`; |
| 79 | b=`basename $f`; |
| 80 | mkdir -p $TO_DIR/$d; |
| 81 | (cd $TO_DIR/$d; . $ABS_FROM_DIR/$d/$b) || exit 1; |
| 82 | done; |
| 83 | fi; |