#!/bin/bash
#
# merge PS files into one big file (works---in contrast to psmerge
# from psutils---with almost every input file)
#
# (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="20090513"

PAGEORI=""
if [ "${1}" = "-n" ]; then
  PAGEORI="-dAutoRotatePages=/None"
  shift
fi

OUTFILE="${1#-o}"
if [ "${OUTFILE}" != "${1}" -a "${OUTFILE}" != "" -a "${2}" != "" ]; then
  shift
  case "${OUTFILE}" in
    *.ps)
      OUTFMT="pswrite"
      ;;
    *.pdf)
      OUTFMT="pdfwrite"
      ;;
    *)
      echo "ERROR: Invalid extension in output file name!" >&2
      echo "Usage: ${0} [-n] -o<outfile> <infile1> <infile2> ..." >&2
      return 2
      ;;
  esac
  gs ${PAGEORI} -dNOPAUSE -sDEVICE="${OUTFMT}" \
    -sOutputFile="${OUTFILE}" "${@}" -c quit
else
  echo "ERROR: Invalid parameters!" >&2
  echo "Usage: psmerge [-n] -ooutfile.ps file1.ps [ file2.ps ... ]" >&2
  exit 1
fi
