#!/bin/sh
# Breed 5.6 - manages external viewers for Reed
# Copyright (C)2019 Chris Rees <crees@FreeBSD.org>
# Copyright (C)2001-2002 Joe Wreschnig <piman@sacredchao.net>
#
# 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. Read the COPYING file included with
# this program for the full text of the GNU GPL.


program="reed"

while getopts whvupbqd:j: o; do
	case "$o" in
	w)
		wrap="| fold -w 75 -s"
		;;
	[dj])
		opts="$opts -$o $OPTARG"
		;;
	[?])
		reed -h
		exit 1
		;;
	*)
		opts="$opts -$o"
		;;
	esac
done
shift $(expr $OPTIND - 1)

if [ "${1#:*}" = "$1" ]; then	# Not a URL
	if [ ! -f "$1" ]; then
		echo "breed: error: No valid filenames found."
		exit 1
	fi
	file=$(realpath $1)
else
	file=$1
fi

pipe="${wrap}| $program $opts -f $file -"

file_lower=$(echo $file | tr A-Z a-z)

case $file_lower in
*.html|*.shtml|*.htm)	eval lynx -dump $file $pipe;;
*.tar.bz2|tbz)		eval bunzip2 -dc $file | tar tvvf - $pipe;;
*.tar.gz|tgz)		eval gunzip -dc $file | tar tvvf - $pipe;;
*.gz|*.z)		eval gunzip -dc $file $pipe;;
*.lha|lhz)		eval lha v $file $pipe;;
*.zip|*.jar)		eval unzip -v $file $pipe;;
*.arj)			eval unarj l $file $pipe;;
*.bz)			eval bunzip -c $file $pipe;;
*.bz2)			eval bunzip2 -dc $file $pipe;;
*.deb)			eval dpkg --info $file $pipe;;
*.doc)			eval catdoc $file $pipe;;
*.ps)			eval ps2ascii $file $pipe;;
*.pdf)			eval pdftotext $file - $pipe;;
*.rpm)			eval rpm -qip $file $pipe;;
*.tar)			eval tar tvvf $file $pipe;;
*.pdb)			eval doctotext $file $pipe;;
*.gpg|*.pgp)		eval gpg --decrypt $file $pipe;;
*)			eval cat $file $pipe;;
esac
