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

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

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

Usage: 
    $THIS [--help]
    $THIS [--verbose] the_file_to_show

Mplayer is launched only with -fs option.
EOF
    exit 0
fi

if [ "$1" == '--verbose' ]; then
    VERBOSE=1
    FILENAME="$2"
else
    VERBOSE=0
    FILENAME=$1
fi

if [ -z "$FILENAME" ]; then
    echo "$THIS:no files to show is specified" >&2
    exit 1
fi

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"

if [ "$VERBOSE" == 1 ]; then
    /usr/bin/startx /usr/bin/mplayer -fs "$TMPFILE"
else
    /usr/bin/startx /usr/bin/mplayer -fs "$TMPFILE" > /dev/null 2>&1
fi
