1#!/usr/local/bin/bash 2 3# MIT License 4# 5# Copyright (c) 2016-2017 Tobias Ellinghaus 6# 7# Permission is hereby granted, free of charge, to any person obtaining a copy 8# of this software and associated documentation files (the "Software"), to deal 9# in the Software without restriction, including without limitation the rights 10# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11# copies of the Software, and to permit persons to whom the Software is 12# furnished to do so, subject to the following conditions: 13# 14# The above copyright notice and this permission notice shall be included in all 15# copies or substantial portions of the Software. 16# 17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23# SOFTWARE. 24 25. "$(dirname "$0")/common.sh" 26 27msg() { 28 echo >&2 -e "${1-}" 29} 30 31die() { 32 local msg=$1 33 local code=${2-1} # default exit status 1 34 msg "$msg" 35 exit "$code" 36} 37 38parse_params() { 39 # default values of variables set from params 40 recursive=0 41 42 while :; do 43 case "${1-}" in 44 -h | --help) usage ;; 45 -v | --verbose) set -x ;; 46 --no-color) NO_COLOR=1 ;; 47 -r | --recursice) recursive=1 ;; # example flag 48 -?*) die "Unknown option: $1" ;; 49 *) break ;; 50 esac 51 shift 52 done 53 54 args=("$@") 55 56 # check required params and arguments 57 [[ ${#args[@]} -eq 0 ]] && die "Missing script arguments" 58 59 return 0 60} 61 62parse_params "$@" 63 64if [ ${#args[@]} -ne 1 ]; then 65 echo "This script watches a folder for new images and imports them into a running instance of darktable" 66 echo "Usage: $0 <folder>" 67 exit 1 68fi 69 70BASE_FOLDER=$($ReadLink "${args[0]}") 71 72if [ ! -d "${BASE_FOLDER}" ]; then 73 echo "error accessing directory '$BASE_FOLDER'" 74 exit 1 75fi 76 77DBUS_SEND=$(which dbus-send) 78if [ $? -ne 0 ]; then 79 echo "can't find 'dbus-send' in PATH" 80 exit 1 81fi 82 83INOTIFYWAIT=$(which inotifywait) 84if [ $? -ne 0 ]; then 85 echo "can't find 'inotifywait' in PATH" 86 exit 1 87fi 88if [ recursive == 1 ]; then 89 INOTIFYWAIT=$INOTIFYWAIT -r 90fi 91 92HAVE_LUA=$("${DBUS_SEND}" --print-reply --type=method_call --dest=org.darktable.service /darktable org.freedesktop.DBus.Properties.Get string:org.darktable.service.Remote string:LuaEnabled 2>/dev/null) 93if [ $? -ne 0 ]; then 94 echo "darktable isn't running or DBUS isn't working properly" 95 exit 1 96fi 97 98echo "${HAVE_LUA}" | grep "true$" >/dev/null 99HAVE_LUA=$? 100 101cleanup() { 102 "${DBUS_SEND}" --type=method_call --dest=org.darktable.service /darktable org.darktable.service.Remote.Lua string:"require('darktable').print('stopping to watch \`${BASE_FOLDER}\'')" 103} 104 105if [ ${HAVE_LUA} -eq 0 ]; then 106 echo "Using Lua to load images, no error handling but uninterrupted workflow" 107 "${DBUS_SEND}" --type=method_call --dest=org.darktable.service /darktable org.darktable.service.Remote.Lua string:"require('darktable').print('watching \`${BASE_FOLDER}\'')" 108 trap cleanup INT 109 trap "echo; echo clean up done. bye" EXIT 110else 111 echo "darktable doesn't seem to support Lua, loading images directly. This results in better error handling but might interrupt the workflow" 112fi 113 114"${INOTIFYWAIT}" --monitor "${BASE_FOLDER}" --event close_write --excludei ".*\.xmp$" | 115 while read -r path event file; do 116 if [ ${HAVE_LUA} -eq 0 ]; then 117 echo "'${file}' added" 118 "${DBUS_SEND}" --type=method_call --dest=org.darktable.service /darktable org.darktable.service.Remote.Lua string:"local dt = require('darktable') dt.database.import('${path}/${file}') dt.print('a new image was added')" 119 else 120 ID=$("${DBUS_SEND}" --print-reply --type=method_call --dest=org.darktable.service /darktable org.darktable.service.Remote.Open string:"${path}/${file}" | tail --lines 1 | sed 's/.* //') 121 if [ "${ID}" -eq 0 ]; then 122 # TODO: maybe try to wait a few seconds and retry? Not sure if that is needed. 123 echo "'${file}' couldn't be added" 124 else 125 echo "'${file}' added with id ${ID}" 126 fi 127 fi 128 129 done 130