#! /bin/sh

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

### $Id: gitaction,v 1.5 1998/03/10 05:12:11 tudor Exp $

###
### This script executes a different action for each file type
### specified.  It tries to match the second parameter against the
### patterns specified in the 'case' statement (see below).
###
### If you want to add new file types & actions to this script, just
### add a new entry to the 'case' statement *before* the last one
### ( *) ... )
###
### For greater 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 at
### tudor@cs.unh.edu.  I'll include it in the next release.
###

###
### Instead of hardcoding `more' or `less` here, we can use GIT_PAGER,
### a shell environment variable that is bound on one of them, depending
### on your environment.  The default is to call "more", but you can
### change GIT_PAGER to point to your favorite pager...
###

###
### Thanks to John Stump for suggesting me to get rid of gitmatch.
###

###
### Note:
### Not all the programs called here are able to handle the -- command
### line switch.  These are: emacs, ghostview, tar, basename, ar, xv,
### xanim, xfig, lynx, mpeg_vga, zip/unzip.  There are some that I couldn't
### test.
###
### When these programs will be fixed, we should add -- to them...
### Until then, we will fail to run commands like `xfig -P -P' to
### process a file called `-P'.
###

name=`basename "$0"`
msg="Press almost any key to continue..."

if test "$#" -ne 2; then
    echo "$name: GIT internal script" >&2
    echo "$msg"; read key;
    exit 1
fi

if test ! -d "$1"; then
    echo "$name: $1 is not a directory"
    echo "$msg"; read key;
    exit 1
fi

if test ! -f "$2"; then
    echo "$name: file $2 does not exist"
    echo "$msg"; read key;
    exit 1
fi

done=0

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

if test $done -ne 0; then
    echo "$msg"; read key;
    exit 0
fi

if test -f $HOME/.gitaction; then
    $HOME/.gitaction "$1" "$2"
    done=$?
fi

if test $done -ne 0; then
    echo "$msg"; read key;
    exit 0
fi


case "$2" in
*.[oO])	 	objdump --syms --all-headers -- "$2" | "$GIT_PAGER";	\
		echo "$msg"; read key;;
*.[aA] | *.[sS][aA])							\
		ar -tv "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[tT][aA][rR])	tar tvf "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[tT][aA][rR].[bB][zZ])						\
		(echo "Tar archive:";					\
		 bunzip -c "$2" | tar tvf -) | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*.[tT][aA][rR].[bB][zZ]2)						\
		(echo "Tar archive:";					\
		 bunzip2 -c "$2" | tar tvf -) | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*.[tT][aA][rR].[gG][zZ]  |						\
*.[tT][aA][rR].[zZ]      |						\
*.[tT][aArRgG][zZ])							\
		(echo "Compressed archive:";				\
		 gunzip -l -- "$2";					\
		 echo;							\
		 echo "Tar archive:";					\
		 gunzip -c -- "$2" | tar tvf -) | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*.[cC][pP][iI][oO])							\
		cat "$2" | cpio -t 2>&1 | "$GIT_PAGER";			\
		echo "$msg"; read key;;
*.[cC][pP][iI][oO].[bB][zZ])						\
		(echo "CPIO archive:";					\
		 bunzip -c "$2" | cpio -t 2>&1) | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*.[cC][pP][iI][oO].[bB][zZ]2)						\
		(echo "CPIO archive:";					\
		 bunzip2 -c "$2" | cpio -t 2>&1) | "$GIT_PAGER";	\
		echo "$msg"; read key;;
*.[cC][pP][iI][oO].[gG][zZ] |						\
*.[cC][pP][iI][oO].[zZ])						\
		(echo "Compressed archive:";				\
		 gunzip -l -- "$2";					\
		 echo;							\
		 echo "Tar archive:";					\
		 gunzip -c -- "$2" | cpio -t 2>&1) | "$GIT_PAGER";	\
		echo "$msg"; read key;;
*.[mM][aA][nN])	nroff -man "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[hH][tT][mM][lL] |							\
*.[hH][tT][mM])	lynx "$2";;
[uU][tT][mM][pP])							\
		w | "$GIT_PAGER";					\
		echo "$msg"; read key;;
[wW][tT][mM][pP])							\
		last | "$GIT_PAGER";					\
		echo "$msg"; read key;;
*[rR][iI][nN][gG].[pP][gG][pP])						\
		pgp -kv -- "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[fF][iI][gG])	if test $DISPLAY; then					\
		    (xfig "$2" > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[gG][iI][fF]     |							\
*.[jJ][pP][gG]     |							\
*.[jJ][pP][eE][gG] |							\
*.[tT][gG][aA]     |							\
*.[bB][mM][pP]     |							\
*.[pP][pP][mM]     |							\
*.[pP][gG][mM]	   |							\
*.[pP][bB][mM])	if test $DISPLAY; then					\
		    (xv "$2" > /dev/null 2>&1 &);			\
		else							\
		    zgv -- "$2";					\
		fi;;
*.[tT][iI][fF][fF] |							\
*.[tT][iI][fF] |							\
*.[bB][mM]     |							\
*.[pP][mM]     |							\
*.[xX][bB][mM] |							\
*.[xX][pP][mM] |							\
*.[rR][aA][sS] |							\
*.[rR][gG][bB] |							\
*.[fF][tT][sS])	if test $DISPLAY; then					\
		    (xv "$2" > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[mM][oO][vV] |							\
*.[aA][vV][iI])	if test $DISPLAY; then					\
		    (xanim "$2" > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[mM][pP]3)	(amp $AMPOPT -q "$2" > /dev/null 2>&1 &);;
*.[mM][pP]3.[gG][zZ] |							\
*.[mM][pP]3.[zZ])							\
		(gunzip -c -- "$2" | amp -q - > /dev/null 2>&1 &);;
*.[mM][pP]3.[bB][zZ])							\
		(bunzip -c "$2" | amp -q - > /dev/null 2>&1 &);;
*.[mM][pP]3.[bB][zZ]2)							\
		(bunzip2 -c "$2" | amp -q - > /dev/null 2>&1 &);;
*.[mM][pP][eE][gG] |							\
*.[mM][pP][gG])								\
		if test $DISPLAY; then					\
		    (xanim "$2" > /dev/null 2>&1 &);			\
		else							\
		    mpeg_vga "$2";					\
		fi;;
*.[mM][oO][vV].[bB][zZ] |						\
*.[aA][vV][iI].[bB][zZ])						\
		if test $DISPLAY; then					\
		    (bunzip -c "$2" |					\
		     xanim - > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[mM][oO][vV].[bB][zZ]2 |						\
*.[aA][vV][iI].[bB][zZ]2)						\
		if test $DISPLAY; then					\
		    (bunzip2 -c "$2" |					\
		     xanim - > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[mM][oO][vV].[gG][zZ] |						\
*.[mM][oO][vV].[zZ]     |						\
*.[aA][vV][iI].[gG][zZ] |						\
*.[aA][vV][iI].[zZ])							\
		if test $DISPLAY; then					\
		    (gunzip -c -- "$2" |				\
		     xanim - > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[mM][pP][eE][gG].[bB][zZ] |						\
*.[mM][pP][gG].[bB][zZ])						\
		if test $DISPLAY; then					\
		    (bunzip -c "$2" |					\
		     xanim - > /dev/null 2>&1 &);			\
		else							\
		    bunzip -c "$2" | mpeg_vga;				\
		fi;;
*.[mM][pP][eE][gG].[bB][zZ]2 |						\
*.[mM][pP][gG].[bB][zZ]2)						\
		if test $DISPLAY; then					\
		    (bunzip2 -c "$2" |					\
		     xanim - > /dev/null 2>&1 &);			\
		else							\
		    bunzip2 -c "$2" | mpeg_vga;				\
		fi;;
*.[mM][pP][eE][gG].[gG][zZ] |						\
*.[mM][pP][eE][gG].[zZ]     |						\
*.[mM][pP][gG].[gG][zZ]     |						\
*.[mM][pP][gG].[zZ])							\
		if test $DISPLAY; then					\
		    (gunzip -c -- "$2" |				\
		     xanim - > /dev/null 2>&1 &);			\
		else							\
		    gunzip -c -- "$2" | mpeg_vga;			\
		fi;;
*.[fF][lL][iI] |							\
*.[fF][lL][cC])	if test $DISPLAY; then					\
		    (xanim "$2" > /dev/null 2>&1 &);			\
		else							\
		    flip "$2";						\
		fi;;
*.[fF][lL][iIcC].[bB][zZ])						\
		if test $DISPLAY; then					\
		    (bunzip -c "$2" |					\
		     xanim - > /dev/null 2>&1 &);			\
		else							\
		    bunzip -c "$2" | flip -;				\
		fi;;
*.[fF][lL][iIcC].[bB][zZ]2)						\
		if test $DISPLAY; then					\
		    (bunzip2 -c "$2" |					\
		     xanim - > /dev/null 2>&1 &);			\
		else							\
		    bunzip2 -c "$2" | flip -;				\
		fi;;
*.[fF][lL][iIcC].[gG][zZ] |						\
*.[fF][lL][iIcC].[zZ])							\
		if test $DISPLAY; then					\
		    (gunzip -c -- "$2" |				\
		     xanim - > /dev/null 2>&1 &);			\
		else							\
		    gunzip -c -- "$2" | flip -;				\
		fi;;
*.[gG][lL])	if test $DISPLAY; then					\
		    (xgrasp "$2" > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[gG][lL].[bB][zZ])							\
		if test $DISPLAY; then					\
		    (bunzip -c "$2" | 					\
		     xgrasp > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[gG][lL].[bB][zZ]2)							\
		if test $DISPLAY; then					\
		    (bunzip2 -c "$2" | 					\
		     xgrasp > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[gG][lL].[gG][zZ] |							\
*.[gG][lL].[zZ])							\
		if test $DISPLAY; then					\
		    (gunzip -c -- "$2" | 				\
		     xgrasp > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[mM][oO][dD] 		    |						\
*.[mM][oO][dD].[gG][zZ]     |						\
*.[mM][oO][dD].[zZ]	    |						\
*.[mM][oO][dD].[lL][hH][aA] |						\
*.[mM][oO][dD].[lL][zZ][hH] |						\
*.[mM][oO][dD].[zZ][iI][pP] |						\
*.[sS]3[mM]		    |						\
*.[sS]3[mM].[gG][zZ] 	    |						\
*.[sS]3[mM].[zZ] 	    |						\
*.[sS]3[mM].[lL][hH][aA]    |						\
*.[sS]3[mM].[lL][zZ][hH]    |						\
*.[sS]3[mM].[zZ][iI][pP]    |						\
*.[mM][tT][mM]		    |						\
*.[mM][tT][mM].[gG][zZ]     |						\
*.[mM][tT][mM].[zZ] 	    |						\
*.[mM][tT][mM].[lL][hH][aA] |						\
*.[mM][tT][mM].[lL][zZ][hH] |						\
*.[mM][tT][mM].[zZ][iI][pP] |						\
*.[xX][mM]		    |						\
*.[xX][mM].[gG][zZ]	    |						\
*.[xX][mM].[zZ]		    |						\
*.[xX][mM].[lL][hH][aA]	    |						\
*.[xX][mM].[lL][zZ][hH]     |						\
*.[xX][mM].[zZ][iI][pP])						\
		(gmod "$2" > /dev/null 2>&1 &);;
*.[rR][aA] |								\
*.[rR][aA][mM])	if test $DISPLAY; then					\
		    (rvplayer "$2" &);					\
		else							\
		    echo "$msg"; read key;				\
		fi;;
*.[vV][oO][cC])	(vplay "$2" > /dev/null 2>&1 &);;
*.[wW][aA][vV])	(wavplay "$2" > /dev/null 2>&1 &);;
*.[rR][mM][dD])	(rmdtopvf -8 "$2" 2> /dev/null |			\
		 pvfspeed -8 -s 8000 2> /dev/null |			\
		 pvftobasic > /dev/audio 2> /dev/null &);;
*.[aA][uU])	(cat "$2" > /dev/audio 2> /dev/null &);;
*.[mM][iI][dD][iI] |							\
*.[mM][iI][dD])	(playmidi $PLAYMIDIOPT "$2" > /dev/null 2>&1 &);;
*.[pP][dD][fF])	if test $DISPLAY; then					\
		    (acroread "$2" > /dev/null 2>&1 &);			\
		else							\
		    echo "$smg"; read key;				\
		fi;;
*.[sS][fF]2 |								\
*.[sS][fF][xX] |							\
*.[sS][bB][kK])	echo "Sound Font file.  No information available."	\
		echo "$msg"; read key;;
*.[pP][sS] |								\
*.[eE][pP][sS])	if test $DISPLAY; then					\
		    (ghostview "$2" > /dev/null 2>&1 &);		\
		else							\
		    gs -sDEVICE=linux "$2";				\
		fi;;
*.[dD][vV][iI])	if test $DISPLAY; then					\
			(xdvi "$2" > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[dD][oO][cC] |							\
*.[aA][wW])	if test $DISPLAY; then					\
		    (applix "$2" > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[pP][dD][fF])	if test $DISPLAY; then					\
		    (xpdf "$2" > /dev/null 2>&1 &);			\
		else							\
		    "$GIT_PAGER" "$2";					\
		    echo "$msg"; read key;				\
		fi;;
*.[mM][sS])	nroff -ms "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[mM][sS].[gG][zZ] |							\
*.[mM][sS].[zZ])							\
		gunzip -c -- "$2" | nroff -ms | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*.[mM][sS].[bB][zZ])							\
		bunzip -c "$2" | nroff -ms | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*.[mM][sS].[bB][zZ]2)							\
		bunzip2 -c "$2" | nroff -ms | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*.[1-9nN] | *.[1-9nN][xXmM])						\
		nroff -man "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[1-9nN][xXmM].[gG][zZ] |						\
*.[1-9nN][xXmM].[zZ]     |						\
*.[1-9nN].[gG][zZ]       |						\
*.[1-9nN].[zZ])	gunzip -c -- "$2" | nroff -man | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*.[1-9nN][xXmM].[bB][zZ] |						\
*.[1-9nN].[bB][zZ])							\
		bunzip -c "$2" | nroff -man | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*.[1-9nN][xXmM].[bB][zZ]2 |						\
*.[1-9nN].[bB][zZ]2)							\
		bunzip2 -c "$2" | nroff -man | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*.[iI][nN][fF][oO])
		info --file "$2";;
RMAIL)		emacs -nw -f rmail;;
INBOX)		emacs -nw -f vm;;
OUTBOX)		emacs -nw -f vm;;
*.[bB][zZ])	bunzip -c "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[bB][zZ]2)	bunzip2 -c "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[gG][zZ] |								\
*.[zZ])		gunzip -c -- "$2" | "$GIT_PAGER";			\
		echo "$msg"; read key;;
*.[zZ][iI][pP])	unzip -v "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[aA][rR][jJ] |							\
*.[aA][0-9][1-9])							\
		unarj l "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[rR][aA][rR])	unrar l "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[jJ][aA][rR])	jar tvf "$2" 2>&1 | "$GIT_PAGER";			\
		echo "$msg"; read key;;
*.[cC][lL][aA][sS][sS])							\
		javap -c -s -l -verbose `echo "$2" | sed 's/.class//'`	\
		| "$GIT_PAGER";						\
		echo "$msg"; read key;;
*.[lL][hH][aA] |							\
*.[lL][zZ][hH])	lha -v "$2" | "$GIT_PAGER";				\
		echo "$msg"; read key;;
*.[rR][pP][mM])	rpm -qilRsp -- "$2" | "$GIT_PAGER";			\
		echo "$msg"; read key;;
*.[dD][eE][bB])	(echo "$2"; dpkg --info "$2";				\
		 echo; dpkg --contents "$2") | "$GIT_PAGER";		\
		echo "$msg"; read key;;
*)		if test -x "$2"; then					\
		    if test `uname` = "Linux"; then			\
		        FILE="file -L";					\
		    else						\
			FILE="file";					\
		    fi;							\
		    $FILE "$2" | grep "text\|data" > /dev/null 2>&1;	\
		    if test $? -eq 0; then				\
			"$GIT_PAGER" "$2";				\
		    else						\
			$FILE "$2" | grep "ELF" > /dev/null 2>&1;	\
		        if test $? -eq 0; then				\
			    $FILE "$2";					\
		            ldd "$2";					\
		        fi;						\
		    fi;							\
		else							\
		    "$GIT_PAGER" "$2";					\
		fi;							\
		echo "$msg"; read key;;
esac

exit 0
