synctodo script has fewer bugs.
2 files changed, 29 insertions(+), 11 deletions(-)

M README.md
M synctodo
M README.md +9 -0
@@ 79,6 79,15 @@ merge: I have the devices set to sync to
 running. When it detects a new sync conflict file, it runs todotxt-merge and
 resolves the conflict.
 
+Notes (mostly for me)
+---------------------
+
+An earlier version duplicated project tags; this removes them:
+
+```
+sed -i -r 's/(@\w+) (\1)/\1/g' todo.txt
+```
+
 Credits
 -------
 

          
M synctodo +20 -11
@@ 20,7 20,7 @@ This script depends on todotxt-merge (ht
 Examples:
    synctodo shared/shopping.txt    # equivalent to \$(pwd)/shared/shopping.txt
    synctodo ~/mobile/todo.txt      # equivalent to \$HOME/mobile/todo.txt
-" $STTDIR
+" $backupdir
 }
 
 while getopts dh opt ; do

          
@@ 73,6 73,18 @@ function merge() {
 	esac
 }
 
+function mergeBased() {
+	CONFL=(${ROOT}.sync-conflict*.txt)
+	RES=$(merge $CONFL $FILE)
+	# cleanup
+	set -e
+	bkup=$(basename ${FILE})-$(date +%+s)
+	mv $FILE "$backupdir/$bkup"
+	mv $CONFL "$backupdir/"
+	mv "$RES" "$FILE"
+	set +e
+}
+
 ## TESTING. Expects some files to merge in the test directory
 #K=($(ls ~/tmp/testmerge/*))
 #O=$(merge $K)

          
@@ 80,21 92,18 @@ function merge() {
 #exit 0
 
 mkdir -p $STTDIR
-printf "Watching %s\nbackups will be in %s\n" "$FILE" "$backupdir"
+printf "Backups will be in %s\n" "$backupdir"
 
+printf "Initial cleanup\n"
+fs=(${ROOT}.sync-conflict*.txt(N))
+[[ ${#fs} -gt 0 ]] && mergeBased
+
+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$"
 	if [[ $? -eq 0 ]]; then
-		CONFL=$(ls -tr ${ROOT}.sync-conflict*.txt)
-		RES=$(merge ($CONFL $FILE))
-		# cleanup
-		set -e
-		bkup=${FILE}-$(date %+s)
-		mv $FILE "$backupdir/$bkup"
-		mv $CONFL "$backupdir/"
-		mv "$RES" "$FILE"
-		set +e
+		mergeBased
 	fi
 done