#!/bin/bash

#Company:       PowerCraft Technology
#Author:        Copyright Jelle de Jong <jelledejong@powercraft.nl>
#Note:          Please send me an email if you enhanced this script
#Version:       0.0.5
#Date:          21-11-06 / 10-11-07 / 12-02-08 / 26-12-2008 / 2009-07-07
#System:        Ubuntu/Debian GNU/Linux
#Information:   man ffplay
#Category       Thunar scripts
#Name:          Play Video
#Description:   Play video file(s)
#Command:       xfce4-terminal --disable-server --hide-toolbars --execute /usr/share/pct-thunar-scripts/scripts/play-video %F
#Patterns:      *
#Directories:   True
#Video Files:   True

# Did the script work for you?
# Yes
# Yes, but with some errors
# Yes, but I had to change some things
# Not at all

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.

# nice -n -1 ffplay "$1"
# xfce4-terminal -x sudo nice -n -1 ffplay -vn -nodisp "$1"
# ffplay -vn "$1"

TEXTDOMAINDIR=/usr/share/locale
TEXTDOMAIN=${0##*/}

NAME=$"Play Video"
DESCRIPTION=$"Play video file(s)"

cecho()
{
    case "$1" in
        "black")
               echo -ne "\E[30m"
               ;;
        "red")
               echo -ne "\E[31m"
               ;;
        "green")
               echo -ne "\E[32m"
               ;;
        "yellow")
               echo -ne "\E[33m"
               ;;
        "blue")
               echo -ne "\E[34m"
               ;;
        "magenta")
               echo -ne "\E[35m"
               ;;
        "cyan")
               echo -ne "\E[36m"
               ;;
         *)
               echo -ne "\E[30m"
               exit
               ;;
    esac

    echo "$2" "$3"
    tput sgr0
}

cecho "green" $"program:"
echo "$0"

cecho "green" $"description:"
echo "$DESCRIPTION"

play_video_file()
{
    cecho "green" $"choose a programm and press enter"
    [ -x /usr/bin/ffplay ] && cecho "red" "[1] ffplay"
    [ -x /usr/bin/gxine ] && cecho "red" "[2] gxine"
    [ -x /usr/bin/mplayer ] && cecho "red" "[3] mplayer"
    [ -x /usr/bin/vlc ] && cecho "red" "[4] vlc"
    [ -x /usr/bin/totem-xine ] && cecho "red" "[5] totem-xine"
    [ -x /usr/bin/totem-gstreamer ] && cecho "red" "[6] totem-gstreamer"
    cecho "red" $"[7] exit"
    read option

    if [ "$option" = "1" ]
    then
        application="ffplay"
        echo $"starting $application command, please standby"
        check_program ffplay
        ffplay "$@"
    fi

    if [ "$option" = "2" ]
    then
        application="gxine"
        echo $"starting $application command, please standby"
        check_program gxine
        gxine "$@"
    fi

    if [ "$option" = "3" ]
    then
        application="mplayer"
        echo $"starting $application command, please standby"
        check_program mplayer
        mplayer "$@"
    fi

    if [ "$option" = "4" ]
    then
        application="vlc"
        echo $"starting $application command, please standby"
        check_program vlc
        vlc "$@"
    fi

    if [ "$option" = "5" ]
    then
        application="totem-xine"
        echo $"starting $application command, please standby"
        check_program totem-xine
        totem-xine "$@"
    fi

    if [ "$option" = "6" ]
    then
        application="totem-gstreamer"
        echo $"starting $application command, please standby"
        check_program totem-gstreamer
        totem-gstreamer "$@"
    fi

    if [ "$option" = "7" ]
    then
        exit
    fi
}

check_program()
{
    application="$1"

    if [ ! -e /usr/bin/$application ]
    then
        if [ "$application" = "ffplay" ]
        then
            application="ffmpeg"
        fi
        sudo apt-get install "$application"
    fi
}

scan_directory()
{
    #step 1: is there a mpg in the folder
    #step 2: query that mpg
    #step 3: check for other folders
    #step 4: scan that folder and repeat

    for file in "$1"/*
    do
        if [ -f "$file" ]
        then
            echo $"checking file:" ${file##*/}
            extension=${file##*.}
            extension=$(echo $extension | tr '[:upper:]' '[:lower:]')
            declare -a array_extension
            array_extension[0]="mpg"
            array_extension[1]="avi"
            array_extension[2]="wmv"
            array_extension[3]="mkv"
            array_extension[4]="mp4"
            array_extension[5]="vob"
            array_extension[6]="ogg"
            for match in "${array_extension[@]}"
            do
                [[ "$extension" = "$match" ]] && video_query+=("$file") && echo $"filling: $match video query:" ${file##*/}
            done
        fi

        if [ -d "$file" ]
        then
            echo $"checking directory:" ${file##*/}
            scan_directory "$file"
        fi
    done
}

cecho "green" $"processing:"
for I in "$@"
do
    filename=${I##*/}
    echo "$filename"
done

cecho "blue" "---------------"

unset video_query

for I in "$@"
do
    cecho "green" $"starting: "

    if [ -f "$I" ]
    then
        echo $"checking file:" ${I##*/}
        extension=${I##*.}
        extension=$(echo $extension | tr '[:upper:]' '[:lower:]')
        declare -a array_extension
        array_extension[0]="mpg"
        array_extension[1]="avi"
        array_extension[2]="wmv"
        array_extension[3]="mkv"
        array_extension[4]="mp4"
        array_extension[5]="vob"
        array_extension[6]="ogg"
        for match in "${array_extension[@]}"
        do
            [[ "$extension" = "$match" ]] && video_query+=("$I") && echo $"filling: $match video query:" ${file##*/}
        done
    fi

    if [ -d "$I" ]
    then
        echo $"checking directory:" ${I##*/}
        scan_directory "$I"
    fi

    cecho "green" $"completed:"
    cecho "blue" "---------------"
done

if [ -n "$video_query" ]
then
    play_video_file "${video_query[@]}"
fi

[ -e $HOME/.recently-used.xbel ] && rm $HOME/.recently-used.xbel

cecho "green" $"done, closing after 3 seconds"
sleep 3

exit