#!/bin/ksh93

#==============================================================================
# Requires:	pstops (http://www.dcs.ed.ac.uk/home/ajcd/psutils/), dvips
# Created:      19.11.1992
# Last Change:  13.08.1999
#==============================================================================
VERSION='0.3'
typeset -r FPROG=${.sh.file}
typeset -r PROG=${FPROG##*/}
SDIR=${FPROG%/*}

function showUsage {
	[[ -n $1 ]] && X='-?' ||  X='--man'
    getopts -a ${PROG} "${ print ${USAGE} ; }" OPT $X
}

function doMain {
	[[ -z $1 ]] && showUsage 1 && exit 1
	
	# This will work for A4 paper.
	typeset DVIFILE="$1" \
		PAPER='a4' PSTOPSOPT='2:0(7.44mm,7.44mm)+1(7.44mm,-141.06mm)'
	shift
	
	# The following are *UNTESTED*. Please let me know whether they work
	# or not, if you can test them.
	#PAPER=a3 ;	PSTOPSOPT='2:0(7.44mm,7.44mm)+1(7.44mm,-202.56mm)'
	#PAPER=letter ;	PSTOPSOPT='2:0(7.44mm,7.44mm)+1(7.44mm,-132.26mm)'
	#PAPER=legal ;	PSTOPSOPT='2:0(7.44mm,7.44mm)+1(7.44mm,-170.36mm)'
	#PAPER=ledger ;	PSTOPSOPT='2:0(7.44mm,7.44mm)+1(7.44mm,-132.26mm)'
	#PAPER=tabloid ;	PSTOPSOPT='2:0(7.44mm,7.44mm)+1(7.44mm,-208.46mm)'
	DVIPS_PRE+=" -t ${PAPER} -t landscape"
	
	if [[ -z ${OF} ]]; then
		dvips -x707 ${DVIPS_PRE} "$@" -f ${DVIFILE} | pstops -q ${PSTOPSOPT} | \
			lp ${LP_OPT}
	elif [[ ${OF} == '-' ]]; then
		dvips -x707 ${DVIPS_PRE} "$@" -f ${DVIFILE} | pstops -q ${PSTOPSOPT}
	else
		dvips -x707 ${DVIPS_PRE} "$@" -f ${DVIFILE} | pstops -q ${PSTOPSOPT} \
			> "${OF}"
	fi
}

USAGE='[-?'"${VERSION}"' ]
[-copyright?(c) 1994, 1999 by Thomas Esser <te@dbs.uni-hannover.de>]
[-copyright?(c) 2015 Jens Elkner (ksh93 port/cleanup).]
[-license?GNU GENERAL PUBLIC LICENSE]
[+NAME?'"${PROG}"' - dvi to ps with reduced output size]
[+DESCRIPTION?'"${PROG}"' behaves like \bdvipd\b(1) and translates the given \advifile\a into postscript with reduced output size: two logical pages will be put onto each physical sheet of paper. All \advi_option\as will directly be passed to \bdvips\b(1).]
[+?If your paper is not in A4 format, you need to adjust the dimensions in this program.]
[+EXAMPLES?It is assumed that the \bPRINTER\b environment variable is set:]{
[+dvired -Plw foo?# send output to printer lw]
[+dvired -o foo.ps foo?# send output to file foo.ps]
[+dvired foo -pp4-7?# send 4 output-pages to printer]
[+dvired -f foo| ghostview -?# preview output with ghostview]
}
[h:help?Print this help and exit.]
[F:functions?Print a list of all functions available.]
[T:trace]:[fnList?A comma separated list of functions of this script to trace (convinience for troubleshooting).]
[+?]
[o:output]:[dst?Send the created postscript to the given destination \adst\a. A dash (-) can be used for stdout, everything else is assumed to be a valid filename. If this option is not given, the default, i.e. lineprinter \b$PRINTER\b will be used.]
[f?Same as "-o -".]
[P:Printer]:[printer?Send the created postscript to the given printer. Can be used multiple times. Naote, that this resets the output possibly given by -o ... to the default.]
\n\n\advifile\a [\advi_option\a]...
'
X="${ print ${USAGE} ; }"
OF= LP_OPT=
[[ -n ${PRINTER} ]] && DVIPS_PRE= || DVIPS_PRE="-P ${PRINTER}"

while getopts "${X}" OPT ; do
	case ${OPT} in
		h) showUsage ; exit 0 ;;
		T)	if [[ ${OPTARG} == 'ALL' ]]; then
				typeset -ft ${ typeset +f ; }
			else
				typeset -ft ${OPTARG//,/ }
			fi
			;;
		F) typeset +f && exit 0 ;;
		V) print ${VERSION} ; exit 0 ;;
		P)	OF= ; DVIPS_PRE+=" -P ${OPTARG}" ; LP_OPT="-d ${OPTARG}" ;;
		o)	OF="${OPTARG}" ;;	# we do not remove .. prefix anymore (jel)
		f)	OF='-' ;;
	esac
done
X=$((OPTIND-1))
shift $X && OPTIND=1
unset X

doMain "$@"
