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

# Make sure we've been given an argument
if [ $# -lt 1 ]; then
	echo "Usage: setpapersize papersize"
	exit 1
fi

papersize=$1
shift 1

# Make sure the argument is valid.
case $papersize in
a4|A4)
	papersize=A4
	dimensions="210mm x 297mm"
	;;
letter|letterdj)
	papersize=`echo $papersize|/usr/bin/sed -e 's|^.|L|'`
	dimensions='8.5" x 11"'
	;;
Letter|Letterdj)
	dimensions='8.5" x 11"'
	;;
*)
	echo "Invalid paper size: $papersize"
	echo "It should be one of: A4, Letter or Letterdj"
	exit 1
	;;
esac

lowpaper=`echo $papersize | /usr/bin/tr '[A-Z]' '[a-z]'`

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

set $pkgs
while [ $# -gt 0 ]; do
	case $1 in
	a2ps)
		a2psfile=/usr/pkg/etc/a2ps-site.cfg
		if [ -s "$a2psfile" ]; then
			mv -f $a2psfile $a2psfile.old
			/usr/bin/sed -e 's|^Options: --medium=.*|Options: --medium='$papersize'|' $a2psfile.old > $a2psfile && /bin/rm $a2psfile.old
			echo "Set $1 package default paper size to $papersize in $a2psfile"
		fi
		;;
	ddd)
		dddfile=/usr/X11R7/lib/X11/app-defaults/Ddd
		if [ -s "$dddfile" ]; then
			mv -f $dddfile $dddfile.old
			/usr/bin/sed -e 's|@Ddd@\*paperSize:.*|@Ddd@*paperSize: '$dimensions'|' $dddfile.old > $dddfile && /bin/rm $dddfile.old
			echo "Set $1 package default paper size to $papersize in $dddfile"
		fi
		;;
	enscript)
		enscriptfile=/usr/pkg/etc/enscript.cfg
		if [ -s "$enscriptfile" ]; then
			mv -f $enscriptfile $enscriptfile.old
			/usr/bin/sed -e 's|^DefaultMedia:.*|DefaultMedia: '$papersize'|' $enscriptfile.old > $enscriptfile && /bin/rm $enscriptfile.old
			echo "Set $1 package default paper size to $papersize in $enscriptfile"
		fi
		;;
	ghostscript)
		ghostscriptfile=`ls /usr/pkg/share/ghostscript/*/lib/gs_init.ps 2>/dev/null | tail -1`
		if [ -s "$ghostscriptfile" ]; then
			mv -f $ghostscriptfile $ghostscriptfile.old
			/usr/bin/sed -e '/^% Optionally choose a default paper size other than U.S. letter.$/N' -e 's/^\(% Optionally.*\n\)[%[:space:]]*(.*\() \/PAPERSIZE where.*\)$/\1('$lowpaper'\2/' $ghostscriptfile.old > $ghostscriptfile && /bin/rm $ghostscriptfile.old
			echo "Set $1 package default paper size to $papersize in $ghostscriptfile"
		fi
		;;
	gv)
		gvfile=/usr/X11R7/lib/X11/app-defaults/GV
		if [ -s "$gvfile" ]; then
			mv -f $gvfile $gvfile.old
			/usr/bin/sed -e 's|^GV.fallbackPageMedia:.*|GV.fallbackPageMedia:	'$papersize'|' $gvfile.old > $gvfile && /bin/rm $gvfile.old
			echo "Set $1 package default paper size to $papersize in $gvfile"
		fi
		;;
	lout)
		loutfile=/usr/pkg/share/lout/include/dsf
		if [ -s "$loutfile" ]; then
			mv -f $loutfile $loutfile.old
			env PAPERSIZE=$papersize /usr/bin/awk '$1 == "named" && $2 == "@PageType" { printf("    %s %s\t\t{ %s\t%s %s # page type (width, height)\n", $1, $2, ENVIRON["PAPERSIZE"], $5, $6); next } { print $0 }' $loutfile.old > $loutfile
			echo "Set $1 package default paper size to $papersize in $loutfile"
		fi
		;;
	magicfilter)
		magicdir=/usr/pkg/libexec/magicfilter
		if [ -s "$magicdir" ]; then
			for f in $magicdir/*-filter; do
				mv -f $f $f.old
				/usr/bin/sed -e 's|-sPAPERSIZE=[a-zA-Z0-9_]*|-sPAPERSIZE='$lowpaper'|' $f.old > $f && /bin/rm $f.old
				/bin/chmod 555 $f
			done
			echo "Set $1 package default paper size to $papersize in $magicdir/*-filter"
		fi
		;;
	psutils)
		psutilsfile=/usr/pkg/etc/psutils.cfg
		if [ -s "$psutilsfile" ]; then
			mv -f $psutilsfile $psutilsfile.old
			/usr/bin/sed -e 's|^PAPERSIZE.*|PAPERSIZE='$lowpaper'|' $psutilsfile.old > $psutilsfile && /bin/rm $psutilsfile.old
			echo "Set $1 package default paper size to $papersize in $psutilsfile"
		fi
		;;
	*)
		echo "Warning: Package $1 not recognised"
		;;
	esac
	shift 1
done

exit 0
