#! /bin/sh
#
# title:       cdclpsmargin
#
# description: cdclpsmargin trims surrounding white borders of the PostScript
#             image created by DCL (Dennou Club Library) and modifies the 
#             BoundingBox in the DSC comments. The border can be completely 
#             removed or can be left at the specified width.
#            
# creator:     Ver.1.0 Takashi Kagimoto (2002/Sep/06)
#
# modify:      Ver.1.1 Takashi kagimoto (2002/Sep/06)
#               - increase command options (-i, -o), so that the single input
#                file and output file can be specified. If the input file is
#                not specified, the standard input will be used. If the output
#                file is not specified, the standard output will be used.
#               - remove the function which can deal with multiple files,
#                because conflicting with the additional functions
#
################################################################################

AWK=@@AWK@@
DCLVER=`cdclver`

COMMAND=cdclpsmargin
TMP=/tmp/${COMMAND}_$$
COPY=/tmp/${COMMAND}2_$$
trap "rm -f $TMP $COPY ; exit 0" 0 1 2 3 4 13 15

usage () {
  echo "Usage: cdclpsmargin [options]"
  echo "  [options]"
  echo "   -m margin        : ratio of border to real figure size (default: 0.05)"
  echo "   -i infile[.ps]   : input file created by dcl. If not specified, the"
  echo "                     standard input is used"
  echo "   -o outfile[.eps] : output file. If not specified, the standard output"
  echo "                     is used"
  echo "   -h               : print this message"
  exit 0
}

create_dsc () {
#
#     /x'\   / a  c \ /x\   /e\
#     |  | = |      | | | + | |  <=>  [a b c d e f] concat
#     \y'/   \ b  d / \y/   \f/
#
  $AWK '
    BEGIN {
      llx=100000; lly=100000; urx=0; ury=0
    }

    /%%DocumentPaperSizes/ {
      papersize = $2
    }

    /%%Orientation:/ {
      orientation = $2
    }
    /concat/ {
      gsub(/\[/,""); gsub(/\]/,"")
      a = $1; b = $2; c = $3; d = $4; e = $5; f = $6
    }

    /[0-9.]+[ ]+[0-9.]+[ ]+[ML]/ {
      llx = min(llx, $1); lly = min(lly, $2)
      urx = max(urx, $1); ury = max(ury, $2)
    }

    END {
      nllx = a*llx + c*lly + e
      nlly = b*llx + d*lly + f
      nurx = a*urx + c*ury + e
      nury = b*urx + d*ury + f

      llx = min(nllx,nurx)
      lly = min(nlly,nury)
      urx = max(nllx,nurx)
      ury = max(nlly,nury)

      xwid = urx-llx
      ywid = ury-lly
      xborder = xwid * border
      yborder = ywid * border

      llx -= xborder
      urx += xborder
      lly -= yborder
      ury += yborder

      llx = int(llx)
      lly = int(lly)
      urx = int(urx)
      ury = int(ury)

      printf("%s\n", "%!PS-Adobe-2.0 EPSF-1.0")
      printf("%s %s\n", "%%Creator:", dclver)
      printf("%s\n", "%%Title: dcl.ps")
      printf("%s\n", "%%Pages: (atend)")
      printf("%s %d %d %d %d\n", "%%BoundingBox:", llx, lly, urx, ury)
      printf("%s %s\n", "%%Orientation:", orientation)
      printf("%s\n", "%%EndComments")
    }

    function min(val1, val2) {
      if (val1 < val2) {
        return val1
      } else {
        return val2
      }
    }
    function max(val1, val2) {
      if (val1 < val2) {
        return val2
      } else {
        return val1
      }
    }
  ' border=$border_ratio dclver=$DCLVER
}

append_body () {
  $AWK '
    /%%BeginProlog/ {
      print
      while (getline == 1) {
        print
      }
    }
    {
      next
    }
  '
}

#-------------------------------------------------------------------------------
border_ratio=0.05
infile=
outfile=
stdin=0
stdout=0

for i in $*
do
  case $i in
    -h) usage;;
    -m) border_ratio=$2; shift 2;;
    -i) infile=$2; shift 2;;
    -o) outfile=$2; shift 2;;
    --) shift; break;;
    -*) usage; break;;
  esac
done

#-------------------------------------------------------------------------------
# consistency check for specified arguments
#-------------------------------------------------------------------------------
if [ x"$infile" = "x" ]; then
  stdin=1
fi
if [ x"$outfile" = "x" ]; then
  stdout=1
fi

#-------------------------------------------------------------------------------
# main procedures
#-------------------------------------------------------------------------------
if [ $stdout = 0 ]; then
  outfile=`basename $outfile .eps`.eps
fi

if [ $stdout = 0 ] && [ -f $outfile ]; then
  echo "$COMMAND : ***WARNING*** $outfile is updated"
fi

if [ $stdin = 1 ]; then
  cat - > $COPY
  infile=$COPY
fi

create_dsc < $infile > $TMP
append_body < $infile >> $TMP

if [ $stdout = 0 ]; then
  mv $TMP $outfile
else
  cat $TMP
fi
