aboutsummaryrefslogtreecommitdiff
path: root/m/copy-if-changed.sh
blob: d46490f19655a1cbb4ed00a00cee2e2027d8fd62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#! /bin/bash

set -e

src=$1
dst=$2

# Copy tree $src to tree $dst


if test -z "$src" || test -z "$dst" || test "$src" = "$dst"; then
    echo "Usage: $0 <src> <dst>"
    exit 1
fi



if test -f $src; then
    
    # Both $src and $dst must be files.
    
    test -f $dst || ! test -e $dst || exit 3
    
    # Copy file if it differs
    srcfile=$src
    dstfile=$dst
    if cmp -s $srcfile $dstfile; then
        : # unchanged; do nothing
    else
        echo cp $srcfile
        cp $srcfile $dstfile
    fi
    
fi



if test -d $src; then
    
    # Both $src and $dst must be directories.  $dst is created if it
    # does not exist
    
    # All files in the source tree are checked; if they already exist
    # in the destination tree and are identical, they are ignored,
    # otherwise they are copied.  Missing directories are created.
    
    # All files in the destination tree are checked; if they do not
    # exist in the source tree, they are deleted.
    
    test -e $dst || mkdir -p $dst
    test -d $dst || exit 3

    # find inode number to identify the src directory later on
    srcinode=$(ls -id $src | awk '{print $1}')

    # Create all directories
    for dir in $(cd $src && find . -type d); do
        dstdir=$dst/$dir
        if test -d $dstdir; then
            : # directory exists; do nothing
        else
            echo mkdir $dstdir
            mkdir -p $dstdir
        fi
    done
    
    # Delete directories which do not exist
    for dir in $(cd $dst && find . \( -name 'CVS' -o -name '.svn' -o -name '.git' -o -name '.hg' -o -name '_darcs' -o -inum $srcinode \) -prune -o -type d -print); do
        srcdir=$src/$dir
        dstdir=$dst/$dir
        if test -d $srcdir; then
            : # directory exists; do nothing
        else
            echo rm -rf $dstdir
            rm -rf $dstdir
        fi
    done
    
    # Copy files that differ
    for file in $(cd $src && find . -type f); do
        srcfile=$src/$file
        dstfile=$dst/$file
        if cmp -s $srcfile $dstfile; then
            : # unchanged; do nothing
        else
            echo cp $srcfile
            cp $srcfile $dstfile
        fi
    done
    
    # Delete files which do not exist
    for file in $(cd $dst && find . \( -name 'CVS' -o -name '.svn' -o -name '.git' -o -name '.hg' -o -name '_darcs' -o -inum $srcinode \) -prune -o -type f -print); do
        srcfile=$src/$file
        dstfile=$dst/$file
        if test -e $srcfile; then
            : # file exists; do nothing
        else
            echo rm $dstfile
            rm $dstfile
        fi
    done
    
fi