19393a
#!/bin/sh
19393a
#
19393a
# To use this filter with less, define LESSOPEN:
19393a
# export LESSOPEN="|/usr/bin/lesspipe.sh %s"
19393a
#
19393a
# The script should return zero if the output was valid and non-zero
19393a
# otherwise, so less could detect even a valid empty output
19393a
# (for example while uncompressing gzipped empty file).
19393a
# For backward-compatibility, this is not required by default. To turn
19393a
# this functionality there should be another vertical bar (|) straight
19393a
# after the first one in the LESSOPEN environment variable:
19393a
# export LESSOPEN="||/usr/bin/lesspipe.sh %s"
19393a
19393a
if [ ! -e "$1" ] ; then
19393a
	exit 1
19393a
fi
19393a
19393a
if [ -d "$1" ] ; then
19393a
	ls -alF -- "$1"
19393a
	exit $?
19393a
fi
19393a
19393a
exec 2>/dev/null
19393a
19393a
# Allow for user defined filters
19393a
if [ -x ~/.lessfilter ]; then
19393a
	~/.lessfilter "$1"
19393a
	if [ $? -eq 0 ]; then
19393a
		exit 0
19393a
	fi
19393a
fi
19393a
19393a
case "$1" in
19393a
*.[1-9n].bz2|*.[1-9]x.bz2|*.man.bz2|*.[1-9n].[gx]z|*.[1-9]x.[gx]z|*.man.[gx]z|*.[1-9n].lzma|*.[1-9]x.lzma|*.man.lzma)
19393a
	case "$1" in
19393a
	*.gz)		DECOMPRESSOR="gzip -dc" ;;
19393a
	*.bz2)		DECOMPRESSOR="bzip2 -dc" ;;
19393a
	*.xz|*.lzma)	DECOMPRESSOR="xz -dc" ;;
19393a
	esac
19393a
	if [ -n "$DECOMPRESSOR" ] && $DECOMPRESSOR -- "$1" | file - | grep -q troff; then
19393a
		$DECOMPRESSOR -- "$1" | groff -Tascii -mandoc -
19393a
		exit $?
19393a
	fi ;;&
19393a
*.[1-9n]|*.[1-9]x|*.man)
19393a
	if file "$1" | grep -q troff; then
19393a
		man -l "$1" | cat -s
19393a
		exit $?
19393a
	fi ;;&
19393a
*.tar) tar tvvf "$1" ;;
19393a
*.tgz|*.tar.gz|*.tar.[zZ]) tar tzvvf "$1" ;;
19393a
*.tar.xz) tar Jtvvf "$1" ;;
19393a
*.xz|*.lzma) xz -dc -- "$1" ;;
19393a
*.tar.bz2|*.tbz2) bzip2 -dc -- "$1" | tar tvvf - ;;
19393a
*.[zZ]|*.gz) gzip -dc -- "$1" ;;
19393a
*.bz2) bzip2 -dc -- "$1" ;;
19393a
*.zip|*.jar|*.nbm) zipinfo -- "$1" ;;
19393a
*.rpm) rpm -qpivl --changelog -- "$1" ;;
19393a
*.cpi|*.cpio) cpio -itv < "$1" ;;
19393a
*.gpg) gpg -d "$1" ;;
19393a
*.gif|*.jpeg|*.jpg|*.pcd|*.png|*.tga|*.tiff|*.tif)
19393a
	if [ -x /usr/bin/identify ]; then
19393a
		identify "$1"
19393a
	elif [ -x /usr/bin/gm ]; then
19393a
		gm identify "$1"
19393a
	else
19393a
		echo "No identify available"
19393a
		echo "Install ImageMagick or GraphicsMagick to browse images"
19393a
		exit 1
19393a
	fi ;;
19393a
*)
19393a
	if [ -x /usr/bin/file ] && [ -x /usr/bin/iconv ] && [ -x /usr/bin/cut ]; then
19393a
		case `file -b "$1"` in
19393a
		*UTF-16*) conv='UTF-16' ;;
19393a
		*UTF-32*) conv='UTF-32' ;;
19393a
		esac
19393a
		if [ -n "$conv" ]; then
19393a
			env=`echo $LANG | cut -d. -f2`
19393a
			if [ -n "$env" -a "$conv" != "$env" ]; then
19393a
				iconv -f $conv -t $env "$1"
19393a
				exit $?
19393a
			fi
19393a
		fi
19393a
	fi
19393a
	exit 1
19393a
esac
19393a
exit $?
19393a