# -*- mode: shell-script -*-
-# Convert to PDF and display a libreoffice compatible document read
-# file on command line or else on stdandard in
+# Convert to PDF and plain text and display a libreoffice compatible
+# document read file on command line or else on stdandard in
+#
+# OPTIONS
+#
+# -e View file in Emacs client
+
+set -e
DEBUG=1
-C="$HOME/.cache/unoconv-display"
-tmp="/tmp/${0##*/}"
-mkdir -p "$C" "$tmp"
+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
-file=$1
-if [ -z "$file" ]; then
- file="$tmp/$$"
- cat > "$file"
-fi
-sha=$(sha1sum "$file");
-pdf="$C/${sha:0:16}.pdf"
+if [ "$1" == "-e" ]; then fmt=emacs; shift; else fmt=pdf; fi
+if [ -n "$1" ]; then file=$1
+else file="$tmp/$$"; cat > "$file"; fi
+sha=$(sha1sum "$file")
+base=$(basename ${file%.*})
+dir="$cache/${sha:0:16}"
+pdf="$dir/$base.pdf"
+txt="$dir/$base.txt"
test -n "$DEBUG" &&\
- echo "[$(date -Is)] ${0##*/} D: Continue for '$tmp', '$file', '$pdf'." >> $tmp/log
+ echo "[$(date -Is)] ${0##*/} D: Continue for '$fmt', '$tmp', '$file', '$pdf', '$txt'." >> $tmp/log
-if [ -f "$pdf" ]; then # Start PDF viewer
- test -n "$DEBUG" &&\
- echo "[$(date -Is)] ${0##*/} D: Found '$pdf'. Start PDF viewer." >> $tmp/log
- exec zathura --fork "$pdf" 2>> "/tmp/err" &
+if [ -d "$dir" ]; then # Start text viewer
rm -f "$tmp/$$"
- exit 0
+ test -n "$DEBUG" &&\
+ echo "[$(date -Is)] ${0##*/} D: Found '$dir'. Start '$fmt' viewer." >> $tmp/log
+ case $fmt in
+ emacs)
+ exec emacsclient -e "(view-file \"$txt\")" > /dev/null
+ exit 0
+ ;;
+ pdf)
+ exec zathura --fork "$pdf" 2>> "/tmp/err" &
+ exit 0
+ ;;
+ *)
+ echo "[$(date -Is)] ${0##*/} E: Unknown format '$fmt'." 1>&2
+ exit 1
+ ;;
+ esac
fi
test -n "$DEBUG" &&\
- echo "[$(date -Is)] ${0##*/} D: Convert '$file' to '$pdf'." >> $tmp/log
+ echo "[$(date -Is)] ${0##*/} D: Convert '$file'." >> $tmp/log
# Convert to PDF
+mkdir "$dir"
unoconv -f pdf -o "$pdf" "$file" || \
unoconv -l && \
unoconv -f pdf -o "$pdf" "$file"
+unoconv -f txt -o "$txt" "$file"
-if [ -f "$pdf" ]; then # Recursively start evince
- test -n "$DEBUG" &&\
- echo "[$(date -Is)] [$(date -Is)] ${0##*/} D: Recursively for '$file'." >> $tmp/log
- bash ${0} "$file"
- exit $?
-fi
-
-echo "[$(date -Is)] [$(date -Is)] ${0##*/} E: Error. Why are we here?" >> $tmp/log
-exit 1
+# Recursively open
+test -n "$DEBUG" &&\
+ echo "[$(date -Is)] [$(date -Is)] ${0##*/} D: Recursively for '$file'." >> $tmp/log
+if [ $fmt == "emacs" ]; then args=-e; fi
+bash ${0} ${args} "$file"
+exit $?