#!/bin/sh

###############################################################################
#                                                                             #
#           GNU Interactive Tools 4.3.7 per file type action script           #
#                               Global version                                #
#               Copyright (C) 1994 Free Software Foundation, Inc.             #
#                   Written by Tudor Hulubei and Andrei Pitis.                #
#                          Enhanced by Verdoolaege Sven.                      #
#                                                                             #
###############################################################################

#
# This script executes a different action for each file type specified.
# The script tries to match (using gitmatch) the second parameter against
# the patterns given as command line arguments to gitmatch (see below).
# gitmatch returns 0 if the parameter doesn't match or the number in the
# list of the pattern that matched.
# If you want to add new file types & actions to this script, just add a
# new pattern after the last pattern in the list and a corresponding
# action to the 'case' statement.
#
# For grater flexibility, gitaction's first parameter is the name of the
# directory where the file resides. So, you can get the complete file
# name appending the file base name to the file path just like that: $1/$2
#
# If you enhance this script, please send me a patch (tudor@chang.pub.ro).
# I'll include it in the next release.
#

#
# The original patch sent by Verdoolaege Sven had 'less' instead of more.
# I don't know if all Unix systems have 'less', but I am pretty sure they
# have 'more', so I've put 'more' back, for the sake of portability.
# If I am wrong, please email me.  Anyway, if you really want 'less', feel
# free to change :-)
#
# NEW!!!
#   Instead of hardcoding "more" or "less" here, we can now use GIT_PAGER, a
# shell environment variable that binds on one of them, depending on your
# environment.  The default is to call "more", but if you change your
# GIT_PAGER environment variable, you will hopefully use "less" or any pager
# you like.
#

name=`basename $0`

if test "$#" -ne 2 -o ! -d "$1" -o ! -f "$2"; then
    echo "$name: GIT internal script" >&2
    exit 255
fi

type=0

if test -f .gitaction; then
    ./.gitaction $1 $2
    type=$?
fi

if test $type != 0; then
    exit 0
fi

gitmatch $2 "*.cc" "*.c" "*.l" "*.y" "*.h" "*.s" "*.S" "*.o" "*.a" "*.sa"   \
            "Makefile" "makefile"                                           \
            "*.tar.gz" "*.tgz" "*.tar.z" "*.tar.Z" "*.taz" "*.tar" "*.gz"   \
            "*.z" "*.Z"                                                     \
            "*.zip" "*.arj" "*.rar"                                         \
            "*.texi" "*.texinfo" "*.man"                                    \
            "*.doc" "*.txt" "*.lsm"                                         \
            "*.gif" "*.jpg" "*.jpeg" "*.tif" "*.bmp"                        \
            "*.mpg"                                                         \
            "*.mod" "*.s3m" "*.voc" "*.wav"                                 \
            "*.ps" "*.dvi" "*.tex"                                          \
	    "*.[1-9n]" "*.[1-9n][xXm]"					    \
	    "*.[1-9n].[zZ]" "*.[1-9n][xXm].[zZ]" 			    \
	    "*.[1-9n].gz" "*.[1-9n][xXm].gz"				    \
            "*.fli" "*.flc"						    \
	    "*.pgp" "*.asc"

type=$?

case $type in
0)              $GIT_PAGER $2;;                         # no match
1)              c++ -O2 $2;;                            # *.cc
2)              cc  -O2 $2;;                            # *.c
3)              lex     $2;;                            # *.l
4)              yacc -d $2;;                            # *.y
5)              $GIT_PAGER $2;;                         # *.h
6  | 7)         cc      $2;;                            # *.s, *.S
8)              objdump -hnrt $2 | $GIT_PAGER;;         # *.o
9  | 10)        ar -tv        $2 | $GIT_PAGER;;         # *.a, *.sa
11 | 12)        make;;                                  # Makefile, makefile
13 | 14)        (echo "Compressed file info:";          # *.tar.gz      \
                gunzip -l  $2;                          # *.tgz         \
                echo;                                                   \
                echo "Tar file info:";                                  \
                gunzip -c  $2 | tar tvf - ) | $GIT_PAGER;;
15 | 16 | 17)   (echo "Compressed file info:";          # *.tar.z       \
                gunzip -l  $2;                          # *.tar.Z       \
                echo;                                   # *.taz         \
                echo "Tar file info:";                                  \
                gunzip -c  $2 | tar tvf - ) | $GIT_PAGER;;
18)             tar tvf    $2 | $GIT_PAGER;;            # *.tar
19)             gunzip -c  $2 | $GIT_PAGER;;            # *.gz
20 | 21)        uncompress -c  $2 | $GIT_PAGER;;        # *.z, *.Z
22)             unzip -v   $2 | $GIT_PAGER;;            # *.zip
23)             unarj l    $2 | $GIT_PAGER;;            # *.arj
24)             unrar l    $2 | $GIT_PAGER;;            # *.rar
25 | 26)        makeinfo   $2;;                         # *.texi, *.texinfo
27)             nroff -man $2 | $GIT_PAGER;;            # *.man
28 | 29 | 30)   $GIT_PAGER $2;;                         # *.doc, *.txt
31 | 32 | 33)   zgv        $2;;                         # *.gif, *.jpg, *.jpeg
34)                                                     # *.tif         \
                echo;                                                   \
                echo "No *.tif viewer specified.";                      \
                echo "Please edit the gitaction script.";;
35)                                                     # *.bmp         \
                echo;                                                   \
                echo "No *.bmp viewer specified.";                      \
                echo "Please edit the gitaction script.";;
36)             if [ $DISPLAY ]; then mpeg_play $2; 
                                 else mpeg_vga $2;fi;;  # *.mpg
37 | 38)        s3mod $2 &;;                            # *.mod *.s3m
39 | 40)        vplay $2 &;;                            # *.voc
41)             if [ $DISPLAY ]; then (ghostview $2 &);fi;;     # *.ps
42)             if [ $DISPLAY ]; then (xdvi $2 &);fi;;          # *.dvi 
43)             latex $2;;
44 | 45)	nroff -man $2 | $GIT_PAGER;;
46 | 47 | 48 | 49)
		gunzip -c $2 | nroff -man | $GIT_PAGER;;
50 | 51)	flip $2;;
52 | 53)	pgp $2;;
                
esac

echo
echo "Press any key ..."

exit 0
