bm-watch (2047 bytes)
1 #!/bin/sh 2 3 # bm-watch: bring the bookmarks of browsers into bm, and keep them coming. 4 # 5 # bm-watch brave & 6 # bm-watch -i 30 brave vivaldi 7 # 8 # At the start, and each time that the bookmark file of a profile changes, 9 # bm-watch runs "bm-import <browser> | bm --merge". So a bookmark that you 10 # add in the browser comes into bm a few seconds later. bm-watch only adds: 11 # it does not remove from bm what you delete in the browser, and it never 12 # writes to the browser. It checks the files every 5 seconds (-i), and it 13 # looks for new profiles once a minute. -1 checks once and stops. 14 15 usage () { 16 printf 'usage: bm-watch [-1] [-i seconds] browser...\n' 17 } 18 19 die () { 20 printf 'bm-watch: %s\n' "$*" >&2 21 exit 1 22 } 23 24 interval=5 25 once= 26 while getopts 1i: opt; do 27 case $opt in 28 1) once=1 ;; 29 i) interval=$OPTARG ;; 30 *) usage >&2; exit 2 ;; 31 esac 32 done 33 shift $((OPTIND - 1)) 34 if [ $# -eq 0 ]; then 35 usage >&2 36 exit 2 37 fi 38 case $interval in 39 ''|*[!0-9]*|0) usage >&2; exit 2 ;; 40 esac 41 42 # files <browser>...: the bookmark files of all profiles, one per line. 43 files () { 44 for browser; do 45 bm-import --files "$browser" 2>/dev/null 46 done 47 } 48 49 # sync <browser>...: merge the bookmarks of the browsers that have profiles. 50 sync () { 51 for browser; do 52 if [ -n "$(bm-import --files "$browser" 2>/dev/null)" ]; then 53 bm-import "$browser" | bm --merge 54 fi 55 done 56 } 57 58 # stamp <files>: the checksum of each file. One cksum reads them all. 59 stamp () { 60 [ -n "$1" ] || return 0 61 ( 62 IFS=' 63 ' 64 set -f 65 # shellcheck disable=SC2086 # one file per line, on purpose 66 cksum $1 2>/dev/null 67 ) 68 } 69 70 list=$(files "$@") 71 [ -n "$list" ] || die "found no profiles of $*" 72 last= 73 ticks=0 74 while :; do 75 if [ "$ticks" -ge 60 ]; then 76 list=$(files "$@") 77 ticks=0 78 fi 79 now=$(stamp "$list") 80 if [ "$now" != "$last" ]; then 81 sync "$@" 82 last=$now 83 fi 84 [ -z "$once" ] || exit 0 85 sleep "$interval" 86 ticks=$((ticks + interval)) 87 done