Last active
December 8, 2016 11:04
-
-
Save julianthome/66a31203b9b25493fa2a43889f948212 to your computer and use it in GitHub Desktop.
Automatically render PDFs from dot files. A filesystem notifier is watching a directory; once a dot file is created within this directory, this script will fully automatically create the corresponding PDF file and display it using the standard PDF viewer on your system. Note that this script is platform dependent (MAC OSX). However, by exchangin…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
INPUT_DIR="/tmp/spool/in" | |
OUTPUT_DIR="/tmp/spool/out" | |
DELAY=4 | |
PIDFILE="/tmp/workdog.pid" | |
FIFO="/tmp/render.fifo" | |
LOGGING="stdout" | |
function createDir() { | |
[ ! -d "${1}" ] && mkdir -p "$1" | |
} | |
function cleanup(){ | |
info "${FUNCNAME[0]}" "cleanup called" | |
[ -e "$FIFO" ] && rm $FIFO | |
return 0 | |
} | |
function log(){ | |
local fun='' | |
fun="$(basename "$0")" | |
logger -t "$fun" "$1" | |
} | |
function info(){ | |
local output="info: $1 : $2" | |
[ "$LOGGING" == "stdout" ] || [ "$LOGGING" == "all" ] && echo >&1 "$output" | |
[ "$LOGGING" == "file" ] || [ "$LOGGING" == "all" ] && log "$output" | |
} | |
function error(){ | |
local output="error: $1 : $2" | |
[ "$LOGGING" == "stdout" ] -o [ "$LOGGING" == "all" ] && echo >&2 "$output" | |
[ "$LOGGING" == "file" ] -o [ "$LOGGING" == "all" ] && log "$output" | |
} | |
function fileaction() { | |
local file='' | |
local ftype='' | |
local filename='' | |
file="$1" | |
ftype="$(file -b --mime-type "$1")" | |
info "${FUNCNAME[0]}" "$ftype" | |
[ "$ftype" == "text/plain" ] && { | |
info "${FUNCNAME[0]}" "fileaction on $file" | |
filename="$(basename "$file")" | |
filename="${filename%.*}" | |
local outfile="${OUTPUT_DIR}/${filename}.pdf" | |
info "${FUNCNAME[0]}" "generate outfile $outfile" | |
dot -T pdf "$file" -o "$outfile" && open "$outfile" | |
} | |
} | |
function main(){ | |
if [ ! -e "$FIFO" ] | |
then | |
mkfifo "$FIFO" | |
fi | |
createDir "$INPUT_DIR" | |
createDir "$OUTPUT_DIR" | |
fswatch --event Created --event Updated "$INPUT_DIR" > "$FIFO" & | |
echo $! > $PIDFILE | |
info "${FUNCNAME[0]}" "write PID ($!) to file" | |
trap cleanup EXIT TERM INT | |
while read -r file; do | |
info "Fileaction $file" | |
fileaction "$file" | |
sleep $DELAY | |
done < $FIFO | |
cleanup | |
return 0 | |
} | |
USAGE="$0 [start|stop]" | |
case $1 in | |
start) | |
main | |
;; | |
stop) | |
cleanup | |
kill -9 "$(cat $PIDFILE)" | |
;; | |
*) | |
echo "$USAGE" | |
exit 1 | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment