#!/bin/sh domain="" phpsessid="" rememberme="" ebookpath="" uuid="" # ebook UUID override_notify() { sub="$1" shift notify-send -h "string:x-canonical-private-synchronous:wallabag" $@ "Wallabag" "$sub" } # check if ebook exists before starting download [ ! -e "/dev/disk/by-uuid/$uuid" ] && { override_notify "Ebook not connected." -u critical; exit 1; } # mount ebook and remove last entry if existent udisksctl mount -b "/dev/disk/by-uuid/$uuid" || { override_notify "Error mounting ebook." -u critical; exit 1; } rm -f $ebookpath/unread_articles_????-??-??.epub override_notify "Ebook mounted.\nDownloading..." -t 0 # download new epub file outfile=$(mktemp) curl -s "https://$domain/export/unread.json" \ -H "cookie: PHPSESSID=$phpsessid; REMEMBERME=$rememberme" \ -o "$outfile" \ || { override_notify "Download failed." -u critical; exit 1; } # if there new entries, download and transfer epub file if [ "0" != "$(jq length "$outfile")" ]; then curl -s "https://$domain/export/unread.epub" \ -H "cookie: PHPSESSID=$phpsessid; REMEMBERME=$rememberme" \ -o "$outfile" \ || { override_notify "Download failed." -u critical; exit 1; } override_notify "Transferring files." -t 0 mv "$outfile" "$ebookpath/unread_articles_$(date +"%Y-%m-%d").epub" fi # if unmount failed check 1 second later, it normally fixes it udisksctl unmount -b "/dev/disk/by-uuid/$uuid" \ || { sleep 1; udisksctl unmount -b "/dev/disk/by-uuid/$uuid"; } \ || { override_notify "Error unmounting ebook." -u critical; exit 1; } override_notify "Finished successfully." -u low