-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-transform.sh
executable file
·118 lines (100 loc) · 3.01 KB
/
sync-transform.sh
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env bash
#: A template for a script that synchronises two directories,
#: except that instead of copying files it performs some sort
#: of transformation.
# The directory containing the files to be transformed
SOURCE_DIR="clear"
# The directory to contain the transformed files
TARGET_DIR="encrypted"
# A function which, given the path to a source file (relative to
# SOURCE_DIR), returns the path to the corresponding target file
# (relative to TARGET_DIR).
function target-of() {
echo "${TARGET_DIR}/${1#*/}.copy"
}
# A function which, given the path to a target file (relative to
# TARGET_DIR), returns the path to the corresponding source file
# (relative to SOURCE_DIR).
function source-of() {
echo "${SOURCE_DIR}/"`basename "${1%.copy}"`
}
# A function which, given the full path to a source file and
# target file, performs the tasks necessary to synchronise the
# two.
# Typically this involves transforming the source file to
# produce the target file.
# Note: Don't prepend SOURCE_DIR or TARGET_DIR.
function transform() {
cp ${1} ${2}
}
# A function which, given the path to a source file and target
# file (relative to the current directory), performs the tasks
# necessary to synchronise the two.
# Typically this consists of deleting the target file, but it
# could also involve reverse-transforming the target file to
# regenerate the source file.
# Note: Don't prepend SOURCE_DIR or TARGET_DIR.
function reverse-transform() {
rm ${1}
}
# Invoke the supplied command, or pretend to, according to the
# command line options.
function react() {
message=$1
command=$2
if [ -n "${message}" ]; then
if [ ${DEBUG} ]; then
echo "::: ${message}"
else
:
fi
fi
if [ -n "${command}" ]; then
if [ ${PRETEND} ]; then
echo "I would execute: ${command}"
else
echo "${command}"
`${command}`
fi
fi
}
while getopts "dp" options; do
case "${options}" in
d) DEBUG=1;;
p) PRETEND=1;;
*) echo "Usage: ${0} [-d] args\n" 1>&2
exit 2;;
esac
done
mkdir -p ${TARGET_DIR}
find ${SOURCE_DIR} -type f -print0 | while read -d $'\0' source
do
target=`target-of ${source}`
message=""
command=""
if test -f ${target}; then
if [ ${target} -ot ${source} ]; then
message="${target} is out of date"
command="transform ${source} ${target}"
else
message="${target} is up-to-date"
fi
else
message="${target} does not exist"
command="transform ${source} ${target}"
fi
react "${message}" "${command}"
done
find ${TARGET_DIR} -type f -print0 | while read -d $'\0' target
do
source=`source-of ${target}`
message=""
command=""
if test -f ${source}; then
message="${source} exists"
else
message="${source} does not exist"
command="reverse-transform ${target} ${source}"
fi
react "${message}" "${command}"
done