-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync
More file actions
executable file
·851 lines (703 loc) · 26.2 KB
/
sync
File metadata and controls
executable file
·851 lines (703 loc) · 26.2 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
#!/bin/sh
VERSION=0.0.11
SOURCE="https://raw.githubusercontent.com/gwf/sync/master/sync"
CLIENT=$(hostname -s)
NO_DELETES=0
DO_DELETES=1
VERBOSE=1
# TODO:
# use a randomly generated GUID on initial link as the identifier for this
# client, so that we can have multiple clients from the same user and
# machine.
# Eliminate all .DS_Store files from local sandbox as an initial step.
# Make wrapper that checks version and updates if necessary.
###############################################################################
function help {
local prog=$(basename "$0")
local prefix="error:"
local line=
for line in "$@"; do
echo "$prefix $line" 1>&2
prefix=$(echo "$prefix" | tr -C ' \n' ' ')
done
if [ $# -gt 0 ]; then echo "" 1>&2; fi
cat 1>&2 <<- EOF
usage: $prog link LOCAL SERVER:REMOTE
$prog unlink LOCAL
$prog update LOCAL
EOF
exit 0
}
###############################################################################
function error {
local prefix="error:"
local line=
for line in "$@"; do
echo "$prefix" "$line" 1>&2
prefix=" "
done
exit 1
}
###############################################################################
function log {
local prefix=$(date "+%Y/%m/%d %H:%M:%S [$$]")
local line=
for line in "$@"; do
if [ -d "$BASE/.sync" ]; then
echo "$prefix $line" >> "$BASE/.sync/log"
fi
if [ "$VERBOSE" -eq 1 ]; then
echo "$prefix $line"
fi
done
}
###############################################################################
function realpath {
local path="${1%/}"
if [ "$path" = "${path##*/}" ]; then
echo "$(cd $path; pwd)"
else
echo "$(cd ${path%/*}; pwd)/${path##*/}"
fi
}
###############################################################################
# First argument is a relative or absolute path the the local sandbox
function load_config {
LOCAL=$(realpath "$1")
HOME=$(dirname "$LOCAL")
BASE=$(basename "$LOCAL")
if [ ! -d "$HOME" ]; then
error "Home directory $HOME does not exist."
fi
if [ -f "$LOCAL/.sync/config" ]; then
source "$LOCAL/.sync/config"
if [ -z "$REMOTE" -o -z "$SERVER" ]; then
error "configuration file in $BASE is corrupt."
fi
else
error "$BASE is missing a config file."
fi
}
###############################################################################
function rsh {
ssh -qn -oConnectTimeout=1 -oBatchMode=yes "$SERVER" $@
}
###############################################################################
function network_checks {
log "performing networking checks"
# Check that the remote end point is up and reachable
ping -c 1 "$SERVER" > /dev/null 2>&1
if [ $? != 0 ]; then
error "remote host $SERVER is unreachable"
fi
# Check that the user can ssh into the server
rsh true > /dev/null 2>&1
if [ $? != 0 ]; then
error "unable to ssh to $SERVER." \
"You may need to setup public ssh keys on $SERVER."
fi
}
###############################################################################
function sanity_checks {
log "performing sanity checks"
# Check that the base directory exists
if [ ! -d "$LOCAL" ]; then
error "sync directory $LOCAL does not exist."
fi
# Check all files within $BASE are owned by the user running the script.
local whoami=$(whoami)
local notowned=$(find $BASE -not -user $whoami -print \
| wc -l | tr -d ' ')
if [ "$notowned" != "0" ]; then
error "$notowned file(s) within $BASE not owned by $whoami" \
"Consider running: 'sudo chown -R $whoami $BASE'."
fi
# Check that all files within $BASE are unlocked.
local locked=$(find $BASE -flags uchg -print | wc -l | tr -d ' ')
if [ "$locked" != "0" ]; then
local cmd="'find $BASE -flags uchg -exec chflags nouchg {} \;'"
error "$locked file(s) within $BASE locked" \
"Consider running: $cmd."
fi
network_checks
# Check that the remote base directory exists.
rsh test -d "$REMOTE/.sync" > /dev/null 2>&1
if [ $? != 0 ]; then
error "$REMOTE directory does not exist on $SERVER."
fi
}
###############################################################################
function acquire_lock {
log "acquiring lock"
local lock="$REMOTE/.sync/lock"
rsh "test \! -f $lock && touch $lock"
if [ $? != 0 ]; then
error "Unable to acquire server lock. Please try again later."
fi
}
###############################################################################
function release_lock {
log "releasing lock"
local lock="$REMOTE/.sync/lock"
rsh "rm $lock > /dev/null 2>&1"
if [ $? != 0 ]; then
error "Unable to release server lock." \
"This will undoubtedly cause problems later."
fi
}
###############################################################################
###############################################################################
###############################################################################
function list_ancestors {
while read path; do
local dirpath=$(dirname "$path")
while [ "$dirpath" != "$path" ]; do
echo $dirpath
path="$dirpath"
dirpath=$(dirname "$path")
done
done
}
###############################################################################
function find_local_updates {
log "finding local updates"
local lvnum=$1
local prev="$BASE/.sync/versions/$lvnum"
# List all file names in the local sandbox. This will help us identify
# additions relative to the last sync. We don't need to note
# directories because they will implicitly be included by the files they
# contain.
( cd $BASE; find . -type f -not -path './.sync/*' -print ) \
| sed 's/^\.//g' \
> "$BASE/.sync/new"
# List all file names from the last local version that was synced to the
# remote. This will help us find deletions. We include non-files so
# that we can test for their removal as well.
( cd $prev; find . -true -print ) \
| sed 's/^\.//g' \
> "$BASE/.sync/old"
# Combine the two lists, sort, and strip out duplicates.
cat "$BASE/.sync/old" "$BASE/.sync/new" | sort -u > "$BASE/.sync/all"
rm -f "$BASE/.sync/additions"; touch "$BASE/.sync/additions"
rm -f "$BASE/.sync/deletions"; touch "$BASE/.sync/deletions"
rm -f "$BASE/.sync/dirs"; touch "$BASE/.sync/dirs"
# Iterate over all names, and use differences between the previous
# version and the local sandbox as a way of identifying individual
# updates.
cat "$BASE/.sync/all" | (
while read fname; do
if [ -d "$prev/$fname" ]; then
# This is a directory found in the previous sync ...
if [ \! -d "$BASE/$fname" ]; then
# ... that appears to be deleted because it was is not
# in the local sandbox.
echo "$fname" >> "$BASE/.sync/dirs"
fi
continue
elif [ "$BASE/$fname" -ef "$prev/$fname" ]; then
# Skip any named file in which the two versions are
# hardlinks to the same file.
continue
elif [ -f "$BASE/$fname" ]; then
# The file exists in the sandbox, but is different from the
# last synced version, so it's a new addition.
echo "$fname" >> "$BASE/.sync/additions"
else
# The file exists in the last synced version but is not in
# the sandbox, so it was deleted.
echo "$fname" >> "$BASE/.sync/deletions"
fi
done
)
# The parent directories of all files added must also be in the
# inclussions list, otherwise, rsync will ignore the children.
cp "$BASE/.sync/additions" "$BASE/.sync/inclusions"
cat "$BASE/.sync/additions" | list_ancestors | sort -u \
>> "$BASE/.sync/inclusions"
# Reorder the directory list from longest name to shortest name, which
# gaurantees that will consider all children before their parents.
cat "$BASE/.sync/dirs" \
| awk '{ print length($0), $0 }' \
| sort -rn \
| sed 's/^[0-9]* //g' \
> "$BASE/.sync/dirs.tmp"
mv "$BASE/.sync/dirs.tmp" "$BASE/.sync/dirs"
# Return a result to indicate if there are any changes
test -s "$BASE/.sync/additions" -o \
-s "$BASE/.sync/deletions" -o \
-s "$BASE/.sync/dirs"
return $?
}
###############################################################################
function pull_remote_version {
local lvnum=$1
local rvnum=$2
# If versions $lvnum and $rvnum on the remote server have hardlinks on
# the files that they have in common, then pulling over both versions
# simultaneously (and with the -H flag) will insure that moves and
# renames will be handled efficiently.
if [ "$lvnum" -ne "$rvnum" ]; then
log "pulling remote version $rvnum via local version $lvnum"
rsync -aH \
"$SERVER:$REMOTE/.sync/versions/{$lvnum,$rvnum}" \
"$BASE/.sync/versions"
else
log "skipping pull because versions match"
fi
}
###############################################################################
function apply_remote_updates {
local lvnum=$1
local rvnum=$2
local nvnum=$3
if [ "$rvnum" -ne "$lvnum" ]; then
# The most recent local and remote versions are different. We've
# already noted the local changes between the sandbox and the more
# recent local version, so it's okay to rename the last local
# version to the next version.
log "moving version $lvnum to $nvnum"
mv "$BASE/.sync/versions/$lvnum" "$BASE/.sync/versions/$nvnum"
fi
# We still need to apply the last remote version's updates on top of the
# next version. If the condition above was true, then the next command
# will "patch" the new version to include the remote's most recent
# updates. But if the condition above is false, then the command above
# effectively does a copy with hardlinks preserved.
log "applying remote updates from version $rvnum to $nvnum"
# In the future, I may want to log the line below if we did the move
# above (but otherwise not).
rsync -a --delete \
--link-dest=../../.. \
--link-dest="../$rvnum" \
"$BASE/.sync/versions/$rvnum/" \
"$BASE/.sync/versions/$nvnum"
}
###############################################################################
function apply_local_updates {
local nvnum=$1
local deletes=$2
local line=
local dir=
# Now that the next version has the last local and remote syncs
# reconciled, we now need to apply the local sandbox updates to the next
# version.
log "applying local additions to $nvnum"
rsync -a \
--exclude=.sync \
--include-from="$BASE/.sync/inclusions" \
--filter='-! */' \
--exclude='*/' \
--link-dest='../../..' \
--log-file="$BASE/.sync/log" \
"$BASE/" \
"$BASE/.sync/versions/$nvnum"
if [ "$deletes" -eq "$DO_DELETES" ]; then
log "applying local file deletions to $nvnum"
cat "$BASE/.sync/deletions" | (
cd "$BASE/.sync/versions/$nvnum"
while read line; do
log "deleting .$line"
/bin/rm -f ."$line"
done
)
log "applying local directory deletions to $nvnum"
cat "$BASE/.sync/dirs" | (
cd "$BASE/.sync/versions/$nvnum"
while read dir; do
if [ $(find . -maxdepth 1 -print | wc -l) -gt 1 ]; then
log "deleting directory .$dir"
/bin/rmdir ."$dir"
fi
done
)
fi
}
###############################################################################
function update_local_sandbox {
local nvnum=$1
# lines like the rsync one below need to be routed through the loggin
# mechanism so that they appear on the command line
log "updating local sandbox via version $nvnum"
rsync -av --delete \
--exclude=.sync \
--link-dest=".sync/versions/$nvnum" \
"$BASE/.sync/versions/$nvnum/" "$BASE" \
>> "$BASE/.sync/log"
}
###############################################################################
function push_new_remote {
local rvnum=$1
local nvnum=$2
log "pushing new remote version $nvnum via version $rvnum"
rsync -aH \
--delete-after \
"$BASE/.sync/versions/$rvnum" \
"$BASE/.sync/versions/$nvnum" \
"$SERVER:$REMOTE/.sync/versions"
log "updating remote sandbox via version $nvnum"
rsh rsync -a --delete \
--exclude=.sync \
--link-dest=".sync/versions/$nvnum" \
"$REMOTE/.sync/versions/$nvnum/" "$REMOTE" \
>> "$BASE/.sync/log"
}
###############################################################################
function cleanup_remote {
local nvnum=$1
log "cleaning up remote"
# Update the remote softlink which indicates which version this client
# needs. Keep only those versions referenced by one of the clients;
# remove all other versions.
ssh -q -oConnectTimeout=1 "$SERVER" <<- EOF
cd "$REMOTE/.sync"
/bin/rm -f "clients/$CLIENT"
ln -s "../versions/$nvnum" "clients/$CLIENT"
used=\$(find clients -type l -exec readlink '{}' \; \
| xargs -r -n 1 basename)
filter='^('\$(echo \$used | tr ' ' '|')')$'
unused=\$(ls versions | egrep -v "\$filter" | sort -n)
cd versions
/bin/rm -rf \$unused
EOF
}
###############################################################################
function cleanup_local {
local nvnum=$1
# Remove every version except the most current.
log "cleaning up local"
find "$BASE/.sync/versions" \
-type d -depth 1 -not -name "$nvnum" -print \
| xargs rm -rf
}
###############################################################################
function update {
local deletes=$1
log "----------------------------------------"
log "starting..."
# First, confirm that a sync is possible, in principle.
sanity_checks
# Get the most recent local and remote versions.
local lvnum=$(ls "$BASE/.sync/versions" | sort -rn | head -1)
local rvnum=$(rsh ls "$REMOTE/.sync/versions" | sort -rn | head -1)
# Until we've confirmed a local change, assume the next version is the
# most recent remote version.
local nvnum=$rvnum
# Check for local updates
find_local_updates $lvnum
local local_changes=$?
# If there are local changes, or the local and remotes are on a
# different versions, then we need to do some real work.
if [ "$local_changes" -eq 0 -o "$lvnum" -ne "$rvnum" ]; then
acquire_lock
pull_remote_version $lvnum $rvnum
if [ "$local_changes" -eq 0 ]; then
# Bump version number to account for local changes.
nvnum=$(($rvnum + 1))
apply_remote_updates $lvnum $rvnum $nvnum
apply_local_updates $nvnum $deletes
push_new_remote $rvnum $nvnum
fi
cleanup_remote $nvnum
update_local_sandbox $nvnum
cleanup_local $nvnum
release_lock
else
log "no changes on either side."
fi
log "done."
}
###############################################################################
###############################################################################
###############################################################################
# Inspect the local and remote machines to see if they are in a good state.
function status {
load_config "$1"
sanity_checks
rsh test -d "$REMOTE/.sync"
local remote_stat=$?
test -d "$BASE/.sync"
local local_stat=$?
rsync -aiunO --delete --exclude=.sync/ "$BASE/" "$SERVER:$REMOTE" \
> /tmp/sync.$$
local push_add=$(grep '^[<>]' /tmp/sync.$$ | wc -l | tr -d ' ')
local push_del=$(grep '^*deleting' /tmp/sync.$$ | wc -l | tr -d ' ')
rsync -aiunO --delete --exclude=.sync/ "$SERVER:$REMOTE/" "$BASE" \
> /tmp/sync.$$
local pull_add=$(grep '^[<>]' /tmp/sync.$$ | wc -l | tr -d ' ')
local pull_del=$(grep '^*deleting' /tmp/sync.$$ | wc -l | tr -d ' ')
if [ $local_stat -eq 0 ]; then
if [ $remote_stat -eq 0 ]; then
echo Local and remote syncs appear ready.
else
echo Local sync appears ready but remote is not.
fi
else
if [ $remote_stat -eq 0 ]; then
echo Remote sync appears ready but local is not.
else
echo Neither local nor remote sync are ready.
fi
fi
echo Push would update $push_add files and delete $push_del files.
echo Pull would update $pull_add files and delete $pull_del files.
}
###############################################################################
###############################################################################
###############################################################################
function init_local_sandbox {
local lvnum=$1
# Wipe out all meta data
rm -rf "$BASE/.sync"
mkdir -p "$BASE/.sync/versions"
log "initializing local sandbox"
# Add config file to store SERVER and REMOTE
cat <<- EOF > "$BASE/.sync/config"
SERVER="$SERVER"
REMOTE="$REMOTE"
EOF
#
rsync -a --delete \
--exclude=.sync \
--log-file="$BASE/.sync/log" \
--link-dest=../../.. \
"$BASE/" "$BASE/.sync/versions/$lvnum"
log "local sync initialized"
}
###############################################################################
function init_remote_sandbox {
# TODO: rework to be a single rsh
log "initializing remote sandbox"
rsh rm -rf "$REMOTE/.sync"
rsh mkdir -p "$REMOTE/.sync/versions"
rsh mkdir -p "$REMOTE/.sync/clients"
rsh rsync -aq --delete \
--exclude=.sync \
--link-dest=../../.. \
"$REMOTE/" "$REMOTE/.sync/versions/0"
log "remote sync initialized"
}
###############################################################################
function parse_init_args {
# Warning: this is one big side effect on globals.
# Get the canonical form of the local directory name.
if [ ! -d "$1" ]; then
if mkdir "$1" > /dev/null 2>&1; then
LOCAL=$(realpath "$1")
rmdir "$1"
else
error "unable to create local sync directory, $1."
fi
else
LOCAL=$(realpath "$1")
fi
HOME=$(dirname "$LOCAL")
BASE=$(basename "$LOCAL")
# Split apart the SERVER:REMOTE argument and clean it up.
SERVER="${2%:*}"
REMOTE="${2#*:}"
if [ "$SERVER:$REMOTE" != "$2" ]; then
help "malformed SERVER:REMOTE arguments"
fi
REMOTE="${REMOTE%/}"
log "client location: $LOCAL"
log "server location: $SERVER:$REMOTE"
}
###############################################################################
function probe_init_state {
# Probe local and remote to find the state of each:
local local_state=missing
if [ -d "$LOCAL" ]; then
local_state=exists
if [ -d "$LOCAL/.sync/versions" ]; then
local_state=ready
fi
fi
local remote_state=missing
if rsh test -d "$REMOTE"; then
remote_state=exists
if rsh test -d "$REMOTE/.sync/versions"; then
remote_state=ready
fi
fi
echo "$local_state-$remote_state"
}
###############################################################################
function link {
local client="$1"
local server="$2"
local rvnum=
parse_init_args "$client" "$server"
network_checks
local state=$(probe_init_state)
local client_state=${state%-*}
local server_state=${state#*-}
cd "$HOME"
log "linking $HOME/$BASE to $SERVER:$REMOTE ..."
log "client state: $client_state"
log "server state: $server_state"
case "$state" in
"missing-missing")
help "Neither $client nor $server exist." \
"At least one side must pre-exist to establish a link."
;;
"ready-missing" | "ready-exists")
help "$client is already linked while $server is unlinked." \
"You should unlink $client first and then relink."
;;
"ready-ready")
help "$client and $server are already linked."
;;
"missing-exists" | "missing-ready")
if [ "$server_state" = "exists" ]; then
init_remote_sandbox
fi
rvnum=$(rsh ls "$REMOTE/.sync/versions" | sort -rn | head -1)
log "pulling initial remote version from server"
# TODO: tty logging
rsync -OHauq --exclude=.sync/ "$SERVER:$REMOTE/" "$BASE"
init_local_sandbox "$rvnum"
log "finalizing link"
rsh ln -s "../versions/$rvnum" "$REMOTE/.sync/clients/$CLIENT"
;;
"exists-missing")
log "pushing initial local version to server"
# TODO: tty logging
rsync -OHauq --exclude=.sync/ "$BASE/" "$SERVER:$REMOTE"
init_local_sandbox 0
init_remote_sandbox
log "finalizing link"
rsh ln -s "../versions/0" "$REMOTE/.sync/clients/$CLIENT"
;;
"exists-ready" | "exists-exists")
if [ "$server_state" = "exists" ]; then
init_remote_sandbox
fi
rvnum=$(rsh ls "$REMOTE/.sync/versions" | sort -rn | head -1)
init_local_sandbox "$rvnum"
# First, let's make sure that the lone local version matches
# the latest remote version.
mkdir -p "$BASE/.sync/versions/$rvnum"
log "merging local and remote versions"
# TODO: tty logging
rsync -viOHa --delete \
--link-dest=../../.. \
"$SERVER:$REMOTE/.sync/versions/$rvnum/" \
"$BASE/.sync/versions/$rvnum"
# By doing an update without deletes, we're forcing a merge
# between to the two systems
log "calling update to complete the link..."
update $NO_DELETES
;;
esac
log "done."
}
###############################################################################
###############################################################################
###############################################################################
# Unlink currently preserves the most recent version even if there are no
# more clients. Rmove the "sort -rn | tail -n +2" in the unused
# calculation to undo. Using it in this form for debugging purposes.
function unlink {
log "uninking $HOME/$BASE from $SERVER:$REMOTE ..."
log "removing link on server $SERVER"
ssh -q -oConnectTimeout=1 "$SERVER" <<- EOF
cd "$REMOTE/.sync"
rm -f "clients/$CLIENT"
used=\$(find clients -type l -exec readlink '{}' \; \
| xargs -r -n 1 basename)
filter='^('\$(echo \$used | tr ' ' '|')')$'
unused=\$(ls versions | sort -rn | tail -n +2 | egrep -v "\$filter" )
cd versions
rm -rf \$unused
if [ \$(ls | wc -l) -eq 0 ]; then
cd ../..
rm -rf .sync
fi
EOF
log "removing link on client inside of $BASE"
/bin/rm -rf "$BASE/.sync"
log "done."
}
###############################################################################
###############################################################################
###############################################################################
function selfupdate {
local self="$0"
local tmp=/tmp/$(basename "$self").sh
local update=/tmp/$(basename "$self").update.sh
local version=
log "checking for new version"
if ! curl -fs "$SOURCE" > "$tmp"; then
log "error downloading source." \
"continuing with the current version."
return
fi
version=$(grep '^VERSION=' $tmp | head -1 | sed 's/^VERSION=//g')
if [ "$version" = "$VERSION" ]; then
log "installed version is latest"
#/bin/rm "$tmp"
return
fi
log "updating from version $VERSION to $version"
# Copy over modes from old version
local mode=$(stat -f '%Mp%Lp' "$self")
if ! chmod "$mode" "$tmp" ; then
error "could not set mode of $tmp"
fi
# Create update script and spawn it
cat > "$update" <<- EOF
#!/bin/bash
if mv "$tmp" "$self"; then
#rm "$update"
exec "$self" $@
else
echo "Failed to update self"
fi
EOF
log "forking update process..." \
"----------------------------------------"
exec /bin/bash "$update"
exit 0
}
###############################################################################
###############################################################################
###############################################################################
function main {
selfupdate "$@"
if [ $# -eq 0 ]; then help; fi
case $1 in
-h|-\?|--help)
help
;;
link)
if [ $# -ne 3 ]; then
help "expected LOCAL, SERVER, and REMOTE to be specified."
fi
link "$2" "$3"
;;
unlink)
if [ $# -ne 2 ]; then
help "expected LOCAL to be specified."
fi
load_config "$2"
cd "$HOME"
unlink
;;
update)
if [ $# -ne 2 ]; then
help "expected LOCAL to be specified."
fi
load_config "$2"
cd "$HOME"
update $DO_DELETES
;;
status)
status "$2"
;;
*)
help "unexpected command-line arguments."
;;
esac
}
###############################################################################
main "$@"