#!/bin/sh

# fixps --- fix as much as possible PS errors that break the psutils

# Copyright 1998-2023 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
# 02110-1301, USA.

# Author: Akim Demaille <Akim.Demaille@freefriends.org>

set -e

# Get the name of the program
program=`echo $0 | sed 's#.*/##g'`

# Local vars
debug=
file=
# Look for a running ghostscript
gs=${GHOSTSCRIPT:-${GS:-gs}}
# Run test in a subshell; some versions of sh will print an error if
# an executable is not found, even if stderr is redirected.
if ($gs -v) >/dev/null 2>&1; then :; else
  gs=
fi
output=-	# Default is stdout
# What action to perform: cat, check, and gs
task=gs
tmpdir=`mktemp -d -t fixps.XXXXXX` || { echo "$program: Cannot create temporary dir!" >&2 ; exit 1; }
verbose=echo

# The version/usage strings
version="fixps 1.6 (GNU a2ps 4.15.6)
Written by Akim Demaille.

Copyright (c) 1998-2000 Akim Demaille, Miguel Santana
Copyright (c) 2023 Reuben Thomas
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."

usage="\
Usage: $program [OPTIONS] FILE
Try to fix common PostScript problems that break postprocessing.

Options:
 -h, --help              display this help and exit
 -v, --version           display version information and exit
 -q, --quiet             don't print informational messages
 -o, --output=FILE       save result in FILE.  If FILE is \`-', send to stdout
 -c, --check, --dry-run  don't perform any action

Report bugs to <bug-a2ps@gnu.org>"

help="Try \`$program --help' for more information."

# Parse command line arguments.
option_without_arguments='vhqc'

# Push a token among the arguments that will be used to notice when
# we ended options/arguments parsing.
arg_sep="$$--$$"
set dummy ${1+"$@"} "$arg_sep"
shift
while test "x$1" != "x$arg_sep"; do

  # Handle --option=value by splitting apart and putting back on argv.
  case "$1" in
    --*=*)
      opt=`echo "$1" | sed -e 's/=.*//'`
      val=`echo "$1" | sed -e 's/[^=]*=//'`
      shift
      set dummy "$opt" "$val" ${1+"$@"}
      shift
      ;;

    -[$option_without_arguments]?*)
      # Prefix $1 with x to avoid running `echo -na' for instance.
      opt=`echo "x$1" | sed -e 's/x-\(.\).*/-\1/'`
      rest=`echo "x$1" | sed -e 's/x-.\(.*\)/-\1/'`
      shift
      set dummy "$opt" "$rest" ${1+"$@"}
      shift
      ;;

    # This case needs to be protected so that the case `-??*' does
    # not split long options without arguments
    --*)
      ;;

    # This is an option with argument.  Split apart and put back on argv.
    -??*)
      opt=`echo "x$1" | sed -e 's/x-\(.\).*/-\1/'`
      arg=`echo "x$1" | sed -e 's/x-.\(.*\)/\1/'`
      shift
      set dummy "$opt" "$arg" ${1+"$@"}
      shift
      ;;
  esac

  # Now, handle the options.  $1 is the option *only*.  If it has an
  # argument, it is now necessarily in $2 etc.  Remember to shift
  # when fetching an argument.
  case "$1" in
    -v | --v*) echo "$version"; exit 0;;
    -h | --h*) echo "$usage"; exit 0;;
    -q | --q*) verbose=:;;
    # Delay debugging so that options parsing does not appear
    -D | --debug)  debug=: ;;
    -o | --output) shift ; output=$1 ;;
    -c | --check | --dry-run)     task=check ;;
    -) # We are working with stdin ;;
      set dummy "$@" "$1"; shift;;

    --) # What remains are not options.
      shift
      while test "x$1" != "x$arg_sep"; do
        set dummy ${1+"$@"} "$1"
        shift
	shift
      done
      break;;

    -*)
      echo "$program: Unknown or ambiguous option \`$1'." >&2
      echo "$program: Try \`--help' for more information." >&2
      exit 1;;
    *) set dummy ${1+"$@"} "$1"
       shift
       ;;
   esac
   shift
done
# Pop the token
shift


# Check the number of arguments.
case $# in
  0)  file=-;;
  1)  file=$1;;
  *)  echo "$program: too many arguments" 1>&2
      echo "$help"  1>&2
      exit 1;;
esac

if test -n "$debug"; then
  # Set -x now if debugging
  set -x
else
  # Temp dir.  Get ready not to leave junk (if not debugging)
  trap "/bin/rm -rf $tmpdir" 0 1 2 3 13 15
fi

# If printing from stdin, save into a tmp file
if test $file = '-'; then
  file=$tmpdir/stdin.ps
  cat >$file
fi

# For dry runs, the output should not even be redirected
if test $task != check; then
  (
    test "$output" != "-" && exec >"$output"
    case $task in
      cat)
  	cat "$file"     ;;
      gs)
        $verbose "$program: making a full rewrite of the file ($gs)." >&2
        $gs -q -dSAFER -dNOPAUSE -dBATCH -sDEVICE=ps2write -sOutputFile=- -c save pop -f $file ;;
    esac
  )
fi

exit 0
