@@ 7,6 7,7 @@ function usage() {
printf "USAGE: synctodo [-dh] <file>
-d Debug
-h Help (exits)
+ -1 Sync up a file and exit (no-daemon mode)
<file> the path to the todo.txt-format file
Finds all SyncThing *.sync-conflict* files for a given file and merges them,
@@ 23,7 24,7 @@ Examples:
" $backupdir
}
-while getopts dh opt ; do
+while getopts dh1 opt ; do
case $opt in
d)
set -x
@@ 32,6 33,9 @@ while getopts dh opt ; do
usage
exit 0
;;
+ 1)
+ local onetime=1
+ ;;
?)
print $OPTERR
usage
@@ 46,7 50,8 @@ if [[ -z $FILE ]]; then
exit 1
fi
-ROOT=${FILE%.txt}
+ROOT=$FILE:r
+SUFF=$FILE:t:e
# merge [F] => F
# merge [Fâ, Fâ] => [Fââ]
@@ 57,6 62,7 @@ function merge() {
0)
;;
1)
+ local onetime=1
;;
2)
local tmpfile=$(tempname -d /tmp mergefile)
@@ 74,7 80,7 @@ function merge() {
}
function mergeBased() {
- CONFL=(${ROOT}.sync-conflict*.txt)
+ CONFL=(${ROOT}.sync-conflict*.${SUFF})
RES=$(merge $CONFL $FILE)
# cleanup
set -e
@@ 95,14 101,17 @@ mkdir -p $STTDIR
printf "Backups will be in %s\n" "$backupdir"
printf "Initial cleanup\n"
-fs=(${ROOT}.sync-conflict*.txt(N))
+fs=(${ROOT}.sync-conflict*.${SUFF}(N))
[[ ${#fs} -gt 0 ]] && mergeBased
+# Exit if the user specified -1
+[[ -n $onetime ]] && exit 0
+
printf "Watching %s" "$FILE"
fswatch -l 5 --event Created --event IsFile "$(dirname $FILE)" | \
while read -r event ; do
# because of the fswatch bug, we have to check this the hard way
- echo "$event" | grep -qP "^${ROOT}\.sync-conflict.*\.txt$"
+ echo "$event" | grep -qP "^${ROOT}\.sync-conflict.*\.${SUFF}$"
if [[ $? -eq 0 ]]; then
mergeBased
fi