-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chk.sh
executable file
·272 lines (262 loc) · 8.01 KB
/
chk.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/bin/bash
#
## @file chk.sh
## @brief Check if the specified directories/libraries need to be sync'd
## @remark Sync with rsync to the USB flash drive backup
## @author Ronald Joe Record (rr at ronrecord dot com)
## @copyright Copyright (c) 2014, Ronald Joe Record, all rights reserved.
## @date Written 15-Feb-2014
## @version 1.0.1
##
## Note: to check other directories use the -s, -a, and -t arguments
## when invoked as chkaplibs it checks directories in my Photosn libs
## when invoked as chkitunes it checks directories in my iTunes library
## when invoked as chkpicdir it checks directories in my Pictures dir
## when invoked as chkmovdir it checks directories in my Movies dir
## when invoked as chkauddir it checks directories in my Audio dir
## when invoked as chkhome it checks directories in $HOME
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# The Software is provided "as is", without warranty of any kind, express or
# implied, including but not limited to the warranties of merchantability,
# fitness for a particular purpose and noninfringement. In no event shall the
# authors or copyright holders be liable for any claim, damages or other
# liability, whether in an action of contract, tort or otherwise, arising from,
# out of or in connection with the Software or the use or other dealings in
# the Software.
#
[ "${MEDROOT}" ] || MEDROOT=/u
[ "${PICROOT}" ] || PICROOT=/u/pictures
[ "${VIDROOT}" ] || VIDROOT=/u/movies
[ "${AUDROOT}" ] || AUDROOT=/Audio
[ "${PHOROOT}" ] || PHOROOT=/Photos
[ "${ITUROOT}" ] || ITUROOT=/iTunes
[ "${MNTROOT}" ] || {
USER=`id -u -n`
MNTROOT=/media/${USER}
}
SOURCEDIR=
USBLIBDIR=
SUFFIX=
NAME="chk"
UPSC="upd"
FORCE=
IGNORE=
APERT_DIR=
ITUNES_DIR=
AUDDIR=
MOVDIR=
PICDIR=
HOMEDIR=
MYHOME=
UPD=
TELL=
USAGE=
## @fn usage()
## @brief Display command line usage options
## @param none
##
## Exit the program after displaying the usage message and example invocations
usage() {
printf "Usage: $NAME [-h] [-n] [-r] [-u] [-a source_dir] [-t dest_dir]\n"
printf " [-A] [-I] [-H] [-L] [-M] [-P] [-f] [-s suffix]\n"
printf " [library name(s) ...]\n"
printf "\nWhere:\n\t-r indicates run rsync to sync libraries when needed\n"
printf "\t-f indicates force a sync regardless of last time-stamp\n"
printf "\t-h indicates use directories from $HOME\n"
printf "\t-A indicates act like invoked as chkauddir\n"
printf "\t-I indicates act like invoked as chkitunes\n"
printf "\t-H indicates act like invoked as chkhome\n"
printf "\t-L indicates act like invoked as chkaplibs\n"
printf "\t-M indicates act like invoked as chkmovdir\n"
printf "\t-P indicates act like invoked as chkpicdir\n"
printf "\t-n indicates tell me what you would do without doing it\n"
printf "\t-u displays this usage message\n"
printf "\t-a source_dir sets the top-level source directory\n"
printf "\t-t dest_dir sets the top-level destination directory\n"
printf "\t-s suffix sets the subdirectory suffix to look for\n"
printf "\tadditional arguments indicate library names to check/sync\n"
printf "\nWhen invoked without arguments $NAME will attempt to check\n"
printf "all the directories in $SOURCEDIR\n"
printf "Only corresponding directories in $USBLIBDIR will be checked\n\n"
exit 1
}
## @fn chklib()
## @brief Check if specified library directory needs to be sync'd
## @param param1 first argument specifies library directory name
chklib() {
lib=$1
[ -d "$USBLIBDIR/$lib" ] || continue
[ -f "$USBLIBDIR/.rsync_$lib" ] || {
if [ "$FORCE" ]
then
echo "Forced sync has been specified - $lib will by synced"
else
echo "$USBLIBDIR/.rsync_$lib does not exist. Directory will by synced"
fi
newer="sync_me"
}
echo "Checking $lib"
if [ "$FORCE" ] || [ "$newer" = "sync_me" ]
then
newer=1
else
newer=`find "$SOURCEDIR/$lib" -newer "$USBLIBDIR/.rsync_$lib" | wc -l`
fi
if [ $newer -eq 0 ]
then
echo "$USBLIBDIR/$lib is current"
else
[ "$FORCE" ] || echo "$USBLIBDIR/$lib is out of date"
[ "$UPD" ] && {
if [ "$TELL" ]
then
echo "Showing what syncing $USBLIBDIR/$lib would do"
else
echo "Syncing $USBLIBDIR/$lib"
fi
if [ "$SUFFIX" ]
then
libnam=`echo $lib | sed -e "s/$SUFFIX//"`
else
libnam="$lib"
fi
$UPSC $IGNORE $TELL $MYHOME $libnam
}
fi
}
while getopts a:s:t:efhAIHPLMrnu flag; do
case $flag in
f)
FORCE=1
;;
e)
IGNORE="-e"
;;
h)
MYHOME="-h"
;;
A)
AUDDIR=1
;;
I)
ITUNES_DIR=1
;;
H)
HOMEDIR=1
;;
L)
APERT_DIR=1
;;
M)
MOVDIR=1
;;
P)
PICDIR=1
;;
r)
UPD=1
;;
n)
TELL="-dn"
;;
a)
SOURCEDIR="$OPTARG"
;;
s)
SUFFIX="$OPTARG"
;;
t)
USBLIBDIR="$OPTARG"
;;
u)
USAGE=1
;;
esac
done
shift $(( OPTIND - 1 ))
NAME=`basename $0`
[ "$APERT_DIR" ] || [ "$NAME" = "chkphotoslibs" ] && {
[ "$SOURCEDIR" ] || SOURCEDIR="${PHOROOT}/Libraries"
[ "$USBLIBDIR" ] || USBLIBDIR="${MNTROOT}/Transcend/Pictures/Libraries"
SUFFIX=".photoslibrary"
UPSC="upd -L"
}
[ "$NAME" = "chkaplibs" ] && {
[ "$SOURCEDIR" ] || SOURCEDIR="${PHOROOT}/Libraries"
[ "$USBLIBDIR" ] || USBLIBDIR="${MNTROOT}/Transcend/Pictures/Libraries"
SUFFIX=".aplibrary"
UPSC="upd -L"
}
[ "$PICDIR" ] || [ "$NAME" = "chkpicdir" ] && {
if [ "$MYHOME" ]
then
[ "$SOURCEDIR" ] || SOURCEDIR="$HOME/Pictures"
else
[ "$SOURCEDIR" ] || SOURCEDIR="${PICROOT}"
fi
[ "$USBLIBDIR" ] || USBLIBDIR="${MNTROOT}/Transcend/Pictures"
UPSC="upd -P"
}
[ "$AUDDIR" ] || [ "$NAME" = "chkauddir" ] && {
if [ "$MYHOME" ]
then
[ "$SOURCEDIR" ] || SOURCEDIR="$HOME/Audio"
else
[ "$SOURCEDIR" ] || SOURCEDIR="${AUDROOT}"
fi
[ "$USBLIBDIR" ] || USBLIBDIR="${MNTROOT}/Transcend/Audio"
UPSC="upd -A"
}
[ "$MOVDIR" ] || [ "$NAME" = "chkmovdir" ] && {
if [ "$MYHOME" ]
then
[ "$SOURCEDIR" ] || SOURCEDIR="$HOME/Movies"
else
[ "$SOURCEDIR" ] || SOURCEDIR="${VIDROOT}"
fi
[ "$USBLIBDIR" ] || USBLIBDIR="${MNTROOT}/Transcend/Movies"
UPSC="upd -M"
}
[ "$HOMEDIR" ] || [ "$NAME" = "chkhome" ] && {
[ "$SOURCEDIR" ] || SOURCEDIR="$HOME"
[ "$USBLIBDIR" ] || USBLIBDIR="${MNTROOT}/Transcend/Home"
UPSC="upd -H"
}
[ "$ITUNES_DIR" ] || [ "$NAME" = "chkitunes" ] && {
[ "$SOURCEDIR" ] || SOURCEDIR="${ITUROOT}"
[ "$USBLIBDIR" ] || USBLIBDIR="${MNTROOT}/Transcend/iTunes"
UPSC="upd -I"
}
# Where to check the sync from
## Defaults to checking current working directory
[ "$SOURCEDIR" ] || SOURCEDIR=`pwd`
# The mounted USB stick or backup directory
[ "$USBLIBDIR" ] || USBLIBDIR="${MNTROOT}/Transcend/`basename $SOURCEDIR`"
[ "$USAGE" ] && usage
[ -d "$SOURCEDIR" ] || {
echo "$SOURCEDIR does not exist or is not a directory. Exiting."
exit 1
}
cd "$SOURCEDIR"
if [ $# -eq 0 ]
then
for lib in *
do
chklib $lib
done
else
for lib in $*
do
chklib $lib$SUFFIX
done
fi