#!/bin/bash
#
# put two pages of a PS file in one
#
# (c) 2005-2009 Christian Schneider <software(at)chschneider(dot)eu>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation, not any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

VERSION="20091008"

[ "${#}" = 3 -o "${#}" = "4" ] || {
  echo "Error: Invalid number of parameters." >&2
  echo "Usage: ${0} [y-offset in mm] <scale factor> <infile> <outfile>" >&2
  exit 1
}

YOFFS="0"
[ "${#}" = "4" ] && {
  YOFFS="${1}"
  shift
}

SCALE="${1}"
POSY2="$(echo "scale = 3; (297.0 / 2.0 - 210.0 * ${SCALE}) / 2.0 + 297.0 / 2.0 - ${YOFFS}" | bc)mm"
POSY1="$(echo "scale = 3; (297.0 / 2.0 - 210.0 * ${SCALE}) / 2.0  + ${YOFFS}" | bc)mm"
POSX="$(echo "scale = 3; 210.0 + (${SCALE} * 297.0 - 210.0) / 2.0" | bc)mm"

pstops "2:0L@${SCALE}(${POSX},${POSY1})+1L@${SCALE}(${POSX},${POSY2})" "${2}" "${3}"
