#!/bin/sh -e
# The script to show video with x.org and mplayer
# Michael Pozhidaev <msp@altlinux.org>
# Date:2014-02-02 

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

if [ "$1" == '--help' ]; then
    cat <<EOF
$THIS: shows video with x.org and mplayer

Usage: 
    $THIS [--help] [FILE_NAME]

Mplayer is launched only with '-fs' option.
Use "q" to exit from viewver mode
EOF
    exit 0
fi
LOGFILE='/tmp/.show-movie.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/mplayer -fs "$TMPFILE" &>"$LOGFILE" 
