Update
[llpp.git] / misc / llpp.inotify
blobd53678d6f99fc68d729608605b0146b5a356a248
1 #!/bin/sh
2 # This wrapper provides automatic reloading on file modifications
3 # to the pdfviewer llpp via inotify.
5 # Prepare parameters.
6 cd `dirname $1`
7 pdf=`basename $1`
8 shift
9 passthrough="$@"
11 # Return with an error if the given file does not exist.
12 if [ ! -e $pdf ]; then
13 echo "$pdf: No such file or directory"
14 exit 1
17 # Start llpp with the given file.
18 llpp $pdf $passthrough &
20 # Track the PID of the llpp instance.
21 pid_llpp=$!
23 # Kill the llpp instance if the shell script terminates.
24 trap "kill ${pid_llpp}" SIGINT SIGTERM SIGQUIT SIGKILL
26 # Watch for changes in the directory of the given file. This is necessary
27 # to recieve events after the file was deleted.
28 inotifywait -m -e close_write "$PWD" -q | while read dir ev file; do
29 # Only refresh on events of the file in question and if this file exists.
30 if [ "$file" = "$pdf" ] && [ -e "$pdf" ]; then
31 kill -HUP $pid_llpp
33 done &
35 # If llpp terminates kill the inotifywait process.
36 wait $pid_llpp
37 pkill -P $$
38 cd - > /dev/null