cache="$HOME/.cache/unoconv-display"
tmp="$XDG_RUNTIME_DIR/${0##*/}"
mkdir -p "$cache" "$tmp"
-test -n "$DEBUG" &&\
- echo "[$(date -Is)] ${0##*/} D: Start." >> $tmp/log
if [ "$1" == "-e" ]; then fmt=emacs; shift; else fmt=pdf; fi
+
+debug () {
+ test -n "$DEBUG" &&\
+ echo "[$(date -Is)] ${0##*/} D: $1" >> $tmp/log
+}
+
+info () {
+ echo "[$(date -Is)] ${0##*/} I: $1" >> $tmp/log
+}
+
+error () {
+ debug "Error: $1"
+ echo -n "${0##*/}: Error: $1" 1>&2
+ test -n "$DEBUG" && echo " > Refer to debug log output '$tmp/log'" 1>&2
+ test -z "$DEBUG" && echo " > Rerun with DEBUG environment variable" 1>&2
+ exit 1
+}
+
+debug "Start"
+
if [ -n "$1" ]; then file=$1
else file="$tmp/$$"; cat > "$file"; fi
sha=$(sha1sum "$file")
pdf="$dir/$base.pdf"
txt="$dir/$base.txt"
-test -n "$DEBUG" &&\
- echo "[$(date -Is)] ${0##*/} D: Continue for '$fmt', '$tmp', '$file', '$pdf', '$txt'." >> $tmp/log
+debug "Continue for '$fmt', '$tmp', '$file', '$pdf', '$txt'."
if [ -d "$dir" ]; then # Start text viewer
rm -f "$tmp/$$"
- test -n "$DEBUG" &&\
- echo "[$(date -Is)] ${0##*/} D: Found '$dir'. Start '$fmt' viewer." >> $tmp/log
+ debug "Found '$dir'. Start '$fmt' viewer."
case $fmt in
emacs)
exec emacsclient -e "(view-file \"$txt\")" > /dev/null
esac
fi
-test -n "$DEBUG" &&\
- echo "[$(date -Is)] ${0##*/} D: Convert '$file'." >> $tmp/log
+# Convert to PDF and text, since not exited already
+
+debug "Convert '$file'."
-# Convert to PDF
mkdir "$dir"
unoconv -f pdf -o "$pdf" "$file" || \
unoconv -l && \
unoconv -f txt -o "$txt" "$file"
# Recursively open
-test -n "$DEBUG" &&\
- echo "[$(date -Is)] [$(date -Is)] ${0##*/} D: Recursively for '$file'." >> $tmp/log
+
+debug "Recursively wrap for '$file'."
if [ $fmt == "emacs" ]; then args=-e; fi
bash ${0} ${args} "$file"
exit $?