#!/usr/local/bin/bash
## #############################################################
## Translate an [e]ps[i] picutre to gif
##
## Usage:
##   ps2gif [switches] arg
##
## Frozen Versions:
##                  1.3
##
## Relies on:
##  ps2bmp     V2.4 or higher
##  ppmtogif
##
## #############################################################
## @FILE:    ps2gif
## @PLACE:   BRICS AArhus; host:harald
## @FORMAT:  Bash Script
## @AUTHOR:  M. Oliver M'o'ller     <omoeller@verify-it.de>
## @BEGUN:   Thu Nov  9 15:22:58 2000
## @VERSION: V1.4                       Thu Jan 10 17:14:46 2002
## #############################################################
##
SHELL=/usr/local/bin/bash
## -- elementaries --------------------------------------------------------
## 
## Shell settings
ARG="";
OUTFILE="";
VERBOSE=false;
LANDSCAPE_INPUT=false;
PS2BMP_LANDSCAPE_OPTION="";
CLIPPING_OPTION="";
FATTEN_FACTOR_OPTION="";
FATTEN_GRANULARITY_OPTION="";
SCALE="1";
TRANSPARENT="";
VERSION_NUMBER="V1.4  ";
VERSION_DATE="Thu Jan 10 17:14:46 2002" ;
VERSION="${VERSION_NUMBER} ${VERSION_DATE}";
## --------------------
function print_usage()
{
  echo "ps2gif  (${VERSION})" >&2;
  echo "" >&2;
  echo "usage:  ps2gif [-hlv] [-s SCALE] [-F FACTOR] [-G GRANULARITY] INFILE [OUTFILE]" >&2;
  echo "        -h             : print help" >&2;
  echo "        -l             : input is in landscape" >&2;
  echo "        -s SCALE       : scale up picture (by integer factor)"  >&2;
  echo "        -t TRANSPARENT : define color TRANSPARENT to be background"  >&2;
  echo "        -C             : clip image (i.e. reduce to bounding box)";
  echo "        -F FATTEN      : fatten up the picture FATTEN=1,2,3,4,5";
  echo "        -G GRANULARITY : pixel granularity for fattening (default: $FATTEN_GRANULARITY)";
  echo "        -v             : verbose on"  >&2;
  echo "        --version      : print version"  >&2;
}
function print_help()
{
  print_usage
  echo "description:";
  echo " Transforms a postscript (or eps or epsi) file into a gif file.";
  echo " SCALE is used for magnification, so the resolution of the result";
  echo " gets higher (and the file gets bigger."
  echo " If a TRANSPARENT color is defined (e.g. \"white\"), then this" ;
  echo " color is declared to be background, which is very useful e.g. for";
  echo " web pages.";
  echo " IF OUTFILE is \"-\", then output goes to stdout.";
  echo " If not OUTFILE is specified, the infile name (minus suffix, plus .gif)";
  echo " is used for writing, even if it already exists.";
  echo " If FATTEN is set, then the image is 'smeared' prior to processing.";
  echo " GRANULARITY gives a way to fine-grain the smearing. The smaller the";
  echo " GRANULARITY, the finer can be the line that is continuously smeared."
  echo " (FATTENING consumes some temporary disc space.)";
  echo " ";
  echo "note: ";
  echo " Since the translation goes over the intermediate ppm format (which";
  echo " is rather big), the translation may take some time, and hopefully";
  echo " your pipes don't break ;o)";
  echo "";
  echo "relies on:   ps2bmp (V2.4 or higher) ppmtogif sed bc ";
  echo "";
  echo "author:      M. Oliver M\"oller     omoeller@verify-it.de";
  exit 1;
}
function abort { ## -- clear the temp files --
  echo "$0: ERROR: $1" >&2;
  echo "** ps2gif aborted." >&2;
  exit 1;
}
function confirm()
{
  echo -n "$1 (y/N)? " >&2
  read CONFIRM_QUERY_ANSWER
  case $CONFIRM_QUERY_ANSWER in
    y	  ) true;;
    Y	  ) true;;
    yes  ) true;;
    YES  ) true;;
    *	  ) false;;
  esac	
}
function get_version_number_of_script { ## 0.00: nonexistent!
  PROG=$(which $1 2>/dev/null);
  if [ ! -z "${PROG}" ] && [ -x "${PROG}" ]; then
    $PROG --version 2>/dev/null |sed -e "s/@//g" |sed -e "s/\\./@/1" | sed -e "s/[^0123456789@]//g" | sed -e "s/@/./g" ;
  else
    echo "0.00";
  fi
}
function stripoffsuffix { ## remove last suffix (if present)
  if [ -z "$(echo $1 |grep  "\\.")" ]; then
    echo $1;
  else
    POS=${#1};
    while [ "${1:${POS}:1}" != "." ]; do
      POS=$((${POS}-1));
    done;
    echo ${1:0:${POS}};
  fi;
}
function compute_minus { ## minus, scaled (used for comparison of floats)
  echo "10000000.0*(($1)-($2))" | bc -l | cut -d'.' -f 1;
}
function set_noswitch_arguments {
  if [ -z ${INFILE} ]; then
    INFILE=$1;
  else
    if [ -z ${OUTFILE} ]; then
      OUTFILE=$1;
    else
      abort "surplus argument: $1";
    fi
  fi
}
## -- other aux functions:

## -- reading options --------------------
AWAIT="";
for switch in $@; do
  case $AWAIT in
	    -s ) AWAIT=""; SCALE=$switch;;
	    -t ) AWAIT=""; TRANSPARENT=$switch;;
            -F ) AWAIT=""; FATTEN_FACTOR_OPTION="-F $switch";;
            -G ) AWAIT=""; FATTEN_GRANULARITY_OPTION="-G $switch";;
	    *  ) case $switch in
	     --help ) print_help;;
	      -help ) print_help;;
	         -h ) print_help;;
	         -v ) VERBOSE=true;;
	         -q ) QUIET=true;;
	         -l ) LANDSCAPE_INPUT=true;;
	         -s ) AWAIT="-s";;
	         -t ) AWAIT="-t";;
		 -C ) CLIPPING_OPTION="-C";;
	         -F ) AWAIT="-F";;
	         -G ) AWAIT="-G";;
	         -  ) OUTFILE="-";;
          --version ) echo "Version:   $VERSION";exit 1;;
		 -* ) echo "Unknown switch: $switch";print_help;;
                 *  ) set_noswitch_arguments $switch;; 
	         esac
  esac
done;
## -- propagate -------------------------
if [ -z "$INFILE" ]; then
  echo "missing INFILE.";
  print_usage;
  exit 1;
fi;
if [ ! -z "$TRANSPARENT" ]; then
  TRANSPARENT="-transparent ${TRANSPARENT}";
fi;
if $LANDSCAPE_INPUT; then
  PS2BMP_LANDSCAPE_OPTION="-l";
fi;
if [ -z "${OUTFILE}" ]; then
  OUTFILE=$(stripoffsuffix ${INFILE}).gif;
fi;
## -- spam ------------------------------
if $VERBOSE; then
  echo "ARG:                       $ARG"
  echo "VERBOSE:                   $VERBOSE"
  echo "LANDSCAPE_INPUT:           $LANDSCAPE_INPUT"
  echo "PS2BMP_LANDSCAPE_OPTION:   $PS2BMP_LANDSCAPE_OPTION";
  echo "CLIPPING_OPTION:           $CLIPPING_OPTION";
  echo "FATTEN_FACTOR_OPTION:      $FATTEN_FACTOR_OPTION";
  echo "FATTEN_GRANULARITY_OPTION: $FATTEN_GRANULARITY_OPTION";
  echo "VERSION:                   $VERSION"
  echo "INFILE:                    $INFILE"
  echo "OUTFILE:                   $OUTFILE"
  echo "SCALE:                     $SCALE"
  echo "TRANSPARENT:               $TRANSPARENT"
fi;
## -- START ------------------------------------

PS_TO_BMP_VERSION=$(get_version_number_of_script ps2bmp);
if [ $(compute_minus 0$PS_TO_BMP_VERSION 2.4) -lt 0 ]; then
  echo "Need ps2bmp 2.4 or higher. ";
  echo "Sorry. ";
  exit 1;
fi;
if [ -z "$(which ppmtogif)" ]; then
  echo "Need ppmtogif.";
  echo "Sorry. ";
  exit 1;
fi;

## -------------------------------------------------------------------------
TMP_FILE=/tmp/tmp.file.2821.$$.ps;
if $LANDSCAPE_INPUT; then
  ofixps -r -90 -x 110 -y -120 ${INFILE} ${TMP_FILE};
  INFILE=${TMP_FILE};
fi;

if [ "${OUTFILE}" == "-" ]; then 
  ps2bmp ${PS2BMP_LANDSCAPE_OPTION} ${CLIPPING_OPTION} ${FATTEN_FACTOR_OPTION} ${FATTEN_GRANULARITY_OPTION} -c -f ppm -s ${SCALE} ${INFILE}  | ppmtogif ${TRANSPARENT}
else
  ps2bmp ${PS2BMP_LANDSCAPE_OPTION} ${CLIPPING_OPTION} ${FATTEN_FACTOR_OPTION} ${FATTEN_GRANULARITY_OPTION} -c -f ppm -s ${SCALE} ${INFILE}  | ppmtogif ${TRANSPARENT} > ${OUTFILE}
fi;

if [ -e ${TMP_FILE} ]; then
  rm ${TMP_FILE};
fi;

echo "** $0: done.";
exit 0; ## --- OK ------------------------------

### Local Variables: ***
### mode: lisp ***
### eval: (defun update-global-date () (let ((pos (point-marker))) (goto-char (point-min)) (if (search-forward-regexp "^VERSION_DATE=" (point-max) t) (progn (kill-line) (insert (format "\"%s\" ;" (current-time-string))) (basic-save-buffer) (message "** Version Date Updated."))) (goto-char pos))) ***
### eval: (defun new-global-hh-insert-disclaimer () (interactive) (insert-disclaimer) (update-global-date) (ksh-mode) (local-set-key [f4] #'new-global-hh-insert-disclaimer)) ***
### eval: (ksh-mode) (local-set-key [f4] #'new-global-hh-insert-disclaimer) ***
### comment-column:0 ***
### comment-start: "### "  ***
### comment-end:"***" ***
### End: ***
