From d1884749a0d8f9241fe6fc582218a41d0cc32927 Mon Sep 17 00:00:00 2001
From: Roman Bazalevskiy <rvb@rvb.name>
Date: Tue, 7 Apr 2015 00:23:55 +0300
Subject: [PATCH] Tool to monitor ebook device connection and sync remote
 (webdav) folder to ebook internal storage.

---
 ebook.conf    |  7 +++++++
 mount-gvfs    | 10 ++++++++++
 mount-watcher | 15 +++++++++++++++
 sync-ebook    | 22 ++++++++++++++++++++++
 4 files changed, 54 insertions(+)
 create mode 100644 ebook.conf
 create mode 100755 mount-gvfs
 create mode 100755 mount-watcher
 create mode 100755 sync-ebook

diff --git a/ebook.conf b/ebook.conf
new file mode 100644
index 0000000..a3bff68
--- /dev/null
+++ b/ebook.conf
@@ -0,0 +1,7 @@
+USER=user
+PASSWD=mystrongpasswd
+DAVFS=davs://server/path
+EBOOK=/media/rvb/EbookLabel
+SRCDIR="Books"
+DSTDIR="My Books"
+TEMPLATE="*.fb2,*.fb2.zip"
diff --git a/mount-gvfs b/mount-gvfs
new file mode 100755
index 0000000..58a68a9
--- /dev/null
+++ b/mount-gvfs
@@ -0,0 +1,10 @@
+#!/usr/bin/expect
+
+set timeout 10
+spawn gvfs-mount [lindex $argv 0]
+expect "User:"
+send "[lindex $argv 1]\r"
+expect "Password:"
+send "[lindex $argv 2]\r"
+expect eof
+
diff --git a/mount-watcher b/mount-watcher
new file mode 100755
index 0000000..de64e73
--- /dev/null
+++ b/mount-watcher
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+. ~/.config/ebook.conf
+
+udisksctl monitor | grep --line-buffered MountPoints | while read -r line
+do
+
+  MOUNTED=`echo $line | awk '{ print $2 }'`
+  if [ "$MOUNTED" == "$EBOOK" ];
+  then
+    sleep 5
+    sync-ebook
+  fi
+
+done
diff --git a/sync-ebook b/sync-ebook
new file mode 100755
index 0000000..4e9a665
--- /dev/null
+++ b/sync-ebook
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+. ~/.config/ebook.conf
+
+mount-gvfs $DAVFS $USER $PASSWD
+
+MOUNT=$XDG_RUNTIME_DIR/gvfs/
+DIR=`gvfs-info $DAVFS | grep "id::filesystem" | awk '{ print $2; }'`
+
+if [ "$DIR" != "" ]
+then
+
+MOUNT=$XDG_RUNTIME_DIR/gvfs/$DIR
+
+if [ -d $EBOOK ]
+then
+
+rsync -ru --include=$TEMPLATE --exclude=.* "$MOUNT/$SRCDIR/" "$EBOOK/$DSTDIR/"
+
+fi
+
+fi
\ No newline at end of file
-- 
2.34.1