#!/bin/bash
if [ -x /usr/bin/open ] ; then
    exec /usr/bin/open "$@"
fi
text=()
image=()
sound=()
video=()
pdf=()
ps=()
html=()
for file ; do
    type="$(file "$file")"
    case "$type" in
    *HTML*)                    html[${#html}]="$file" ;; # must be before text.
    *Postscript*|*PostScript*) ps[${#ps}]="$file" ;;
    *PDF*)                     pdf[${#pdf}]="$file" ;;
    *text*)                    text[${#text}]="$file" ;;
    *image*)                   image[${#image}]="$file" ;;
    *ogg*|*mp3*|*MP3*|*audio*) sound[${#sound}]="$file" ;;
    *AVI*|*movie*)             video[${#video}]="$file";;
    *)                   echo "Don't know how to open $file";echo "$type";;
    esac
done

if [ ${#text}  -ne 0 ] ; then
     ( emacsclient -n "${text[@]}" -a emacs || echo "Do not know how to open $f" )
fi
if [ ${#html}  -ne 0 ] ; then
    for file in ${html[@]} ; do
        case "$file" in
        /*)
            emacsclient -e "(browse-url \"file://${file}\")"
            ;;
        *)
            emacsclient -e "(browse-url \"file://$(pwd)/${file}\")"
            ;;
        esac
    done
fi
if [ ${#image} -ne 0 ] ; then
     ( xv "${image[@]}" 2>/dev/null || xview "${image[@]}" 2>/dev/null || gimp "${image[@]}" 2>/dev/null || echo "Do not know how to open $f" ) &
fi
if [ ${#sound} -ne 0 ] ; then
     ( xmms -p "${sound[@]}" 2>/dev/null  || mplayer "${sound[@]}" 2>/dev/null  || echo "Do not know how to open $f" ) &
fi
if [ ${#video} -ne 0 ] ; then
    for f in "${video[@]}" ; do
        mplayer "$f" 2>/dev/null || aviplay "$f" 2>/dev/null || echo "Do not know how to open $f"
    done &
fi
if [ ${#pdf}   -ne 0 ] ; then
     for f in "${pdf[@]}" ; do
         ( acroread "$f" 2>/dev/null || xpdf "$f" 2>/dev/null || echo "Do not know how to open $f" ) &
     done
fi
if [ ${#ps}    -ne 0 ] ; then
     for f in "${ps[@]}" ; do
         ( gv "$f" 2>/dev/null || gsview "$f" 2>/dev/null  || echo "Do not know how to open $f" ) &
     done
fi
exit 0
#### THE END ####
ViewGit