#!/bin/sh
#
# $NetBSD: getpapersize,v 1.9 2003/09/15 10:51:40 agc Exp $
#
# Small shell script to show the default papersize for packages
#

if [ $# -eq 0 ]; then
	pkgs="a2ps ddd enscript ghostscript gv lout magicfilter psutils";
else 
	pkgs=$@
fi

set $pkgs
while [ $# -gt 0 ]; do
	case $1 in
	a2ps)
		f=/usr/pkg/etc/a2ps-site.cfg
		if [ -s "$f" ]; then
			echo -n "$1 has default papersize: "
			/usr/bin/awk '/^Options: --medium=/ { gsub(".*=", "", $0); print $0; }' $f
		fi
		;;
	ddd)
		f=/usr/X11R7/lib/X11/app-defaults/Ddd
		if [ -s "$f" ]; then
			echo -n "$1 has default papersize: "
			/usr/bin/awk '/^@Ddd\*paperSize: / { gsub(".*:", "", $0); print $0; }' $f
		fi
		;;
	enscript)
		f=/usr/pkg/etc/enscript.cfg
		if [ -s "$f" ]; then
			echo -n "$1 has default papersize: "
			/usr/bin/awk '/^DefaultMedia:.*/ { gsub(".*:", "", $0); print $0; }' $f
		fi
		;;
	ghostscript)
		f=`ls /usr/pkg/share/ghostscript/*/lib/gs_init.ps 2>/dev/null | tail -1`
		if [ -s "$f" ]; then
			echo -n "$1 has default papersize: "
			gssize=`/usr/bin/sed -n -e '/^% Optionally choose a default paper size other than U.S. letter.$/N' -e 's/^% Optionally.*\n[[:space:]]*(\(.*\)) \/PAPERSIZE where.*$/\1/p' $f`
			if [ -z "$gssize" ] ; then
				echo "(unknown)"
			else
				echo $gssize
			fi
		fi
		;;
	gv)
		f=/usr/X11R7/lib/X11/app-defaults/GV
		if [ -s "$f" ]; then
			echo -n "$1 has default papersize: "
			/usr/bin/awk '/^GV.fallbackPageMedia:/ { print $2; }' $f
		fi
		;;
	lout)
		f=/usr/pkg/share/lout/include/dsf
		if [ -s "$f" ]; then
			echo -n "$1 has default papersize: "
			/usr/bin/awk '$1 == "named" && $2 == "@PageType" { print $4; }' $f
		fi
		;;
	magicfilter)
		magicdir=/usr/pkg/libexec/magicfilter
		if [ -s $magicdir ]; then
			done=0
			for f in $magicdir/*-filter; do
				if [ $done -eq 0 ]; then
					echo -n "$1 has default papersize: "
					/usr/bin/grep 'PAPERSIZE' $f | /usr/bin/sed -e 's|\(.*\)-sPAPERSIZE=\([a-zA-Z0-9_]*\)\(.*\)|\2|' | tail -n 1
					done=1
				fi
			done
		fi
		;;
	psutils)
		f=/usr/pkg/etc/psutils.cfg
		if [ -s "$f" ]; then
			echo -n "$1 has default papersize: "
			/usr/bin/awk '/^PAPERSIZE=/ { gsub(".*=", "", $0); print $0; }' $f
		fi
		;;
	*)
		echo "Warning: Package $1 not recognised"
		;;
	esac
	shift 1
done

exit 0
