#!/bin/sh -e
# Shows PDF-file with xpdf in X session
# Michael Pozhidaev <msp@altlinux.org>
# Date: 2014-02-02

THIS="${0##*/}"

if [ "$1" == '--help' ]; then
    cat <<EOF
$THIS: shows PDF-file with xpdf in X session

Usage: 
    $THIS [--help]

Use "q" to exit from viewver mode
EOF
    exit 0
fi

LOGFILE='/tmp/.show-pdf.log'
    FILENAME=$1

[ -z "$FILENAME" ] && echo "$THIS:no files are mentioned" >&2 && exit 1

FILENAME="$(realpath "$FILENAME")"
TMP="$(mktemp -d)"

exit_handler()
{
    local rc=$?
    trap - EXIT
    /bin/rm -rf -- "$TMP"
    exit $rc
}

trap exit_handler EXIT HUP INT QUIT PIPE TERM

if echo "$FILENAME" | egrep --silent '\.[^ ]+$'; then
    TMPFILE="$TMP/file.${FILENAME##*.}"
else
    TMPFILE="$TMP/file"
fi

/bin/ln -s "$FILENAME" "$TMPFILE"

/usr/bin/startx /usr/bin/xpdf -fullscreen "$TMPFILE" &>"$LOGFILE" 
