#!/bin/csh 
# A script to convert generic postscript to Adobe Illustrator file format
# with the correct bounding box information, qQ-matching and other
# useful options.
#               Jason Olszewski <olszewsk@splash.princeton.edu> 04/06/94
#
# Usage: aimaker [-cbfgtijprshv] infile.ps 
#     (the output file will be "infile.aieps" or stout)
# 
set version = 1.26
#       Needed Files:
# gs                 (the gnu-ghostscript postscript interpreter)
# ps2ai.ps           (postscript to AI filter)
# bb4gsj.ps          (%%BoundingBox calculating psfile)
# qmind.awk          (awk script to make sure there are matched pairs q & Q)
# bwcmyk2gray.awk    (awk script to change b/w cmyk to gray)
# proc2cust.awk      (awk script to change process colors to custom colors)
# gray2cust.awk      (awk script to change gray to custom colors)
# gray2tintblack.awk (awk script to change gray to tinted black custom color)
# invertb.awk        (awk script to invert the grays)
# invertc.awk        (awk script to invert the colors)
# remvc.awk          (awk script to NTSC convert colors to grays)
# richblack.awk      (awk script to make blacks look "richer")
# seg.awk            (awk script to join line segments together)

set ps2aidir = ( /opt/local/aimaker )      # path to ps2ai.ps
set psutdir = ( /opt/local/aimaker )        # path to other files

set argv = (`getopt "cbfgtijprshv" $*`)
if ( $status ) then
  echo "Usage: aimaker [-cbfgtijprshv] infile.ps"
  echo "  -h :  help option"
  exit 1
endif
                    #           Defaults
set verb = 0        # show information messages (yes=0)
set cust = 0        # convert process to custom color (no=0)
set cugr = 0        # convert gray to custom color (no=0)
set ctgr = 0        # convert gray to tint black custom color (no=0)
set covt = 0        # invert the colors (no=0)
set bwvt = 0        # invert the grays (no=0)
set cobw = 0        # convert colors to grays (no=0)
set pipe = 0        # output (file=0, stout=1)
set rich = 0        # convert black to rich black (no=0)
set cseg = 0        # connect line segments 
set form = 0        # format of text/output (0=AI3, 1=AI88)

while  "$1" != "--" 
    switch ("$1")
       case '-c':
         set cust = 1
       breaksw
       case '-g':
         set cugr = 1
         breaksw
       case '-t':
         set ctgr = 1
         breaksw
       case '-i':
         set covt = 1
         breaksw
       case '-j':
         set bwvt = 1
         breaksw
       case '-b':
         set cobw = 1
         breaksw
       case '-r':
         set rich = 1
         breaksw
       case '-p':
         set pipe = 1
         breaksw
       case '-s':
         set cseg = 1
         breaksw
       case '-f':
         set form = 1
         breaksw
       case '-h':
         echo " "
         echo "  Usage:  aimaker [-cbfgtijprshv] infile.ps"
         echo " "
         echo "    -c : convert process to custom colors"
         echo "    -g : convert grays to custom colors"
         echo "    -t : convert grays to tinted black custom colors"
         echo "    -i : invert the colors"
         echo "    -j : invert the grays"
         echo "    -b : convert colors to grays"
         echo "    -r : convert black to rich black"
         echo "    -s : convert line segments to continuous lines"
         echo "    -p : send output to stout instead of file (infile.aieps)"
         echo "    -f : convert to AI88 format instead of default AI3"
         echo "    -v : surpress the information status messages"
         echo "    -h : this help message"
         echo " "
         echo "  Version:" $version
         echo " "
         exit 1
         breaksw
      case '-v':
         set verb = 1
         breaksw
     endsw
     shift
end
shift

set source = $1
set psfile = $source:t
set name = $psfile:r
set eps = ${name}.aieps

if ($source == "") then
 echo " ERROR: You did not give a source postscript file"
 echo " Usage:  aimaker [-cbfgtijprshv] infile.ps"
 echo "    -h:  help message"
 exit
endif

#file $source | grep -s PostScript
head -1 $source | grep -s '^%\\!'
set jjstate = $status
if ( $jjstate == 1) then
 echo $source "is not a PostScript file."
 exit
endif


# if sending output to stout, surpress information messages
if ($pipe != 0) then
 set verb = 1
endif

if ($verb != 1) echo " Converting PS to AI format..."


cat ${ps2aidir}/ps2ai.ps  >aitemp2.ps
if ($form != 0) then
 if ($verb != 1) echo " Using AI88 format..."
 sed -e 's/jtxt3 true/jtxt3 false/g' aitemp2.ps>aitemp1.ps
 cat aitemp1.ps>aitemp2.ps
endif
cat $source >>aitemp2.ps
cat -<<EOF >>aitemp2.ps
showpage
EOF

gs -q -dNODISPLAY aitemp2.ps >aitemp1.ps
cat ${psutdir}/bb4gsj.ps aitemp1.ps>aitemp2.ps

if ($verb != 1) echo " Calculating %%BoundingBox..."
gs -q -dNODISPLAY aitemp2.ps>aitempbb.ps

if ($verb != 1) echo " Putting %%BoundingBox info in file..."
cat -<<EOF> aitemp2.ps
%!PS-Adobe-2.0 EPSF-2.0
EOF
cat aitempbb.ps >> aitemp2.ps
cat -<<EOF>> aitemp2.ps

EOF
awk 'NR <=2 {} ; NR >=3 {print $0}' aitemp1.ps >>aitemp2.ps

# gray to custom and gray to tint black can't both be used
if ( $ctgr != 0 ) then
 set cugr = 0 
endif
# check for right grouping and convert bwcmyk to gray
awk -f ${psutdir}/qmind.awk aitemp2.ps | \
awk -f ${psutdir}/bwcmyk2gray.awk > aitemp1.ps

cat aitemp1.ps > aitemp2.ps
if ($rich != 0 ) then
  if ($verb != 1) echo " Converting black to rich black..."
  awk -f ${psutdir}/richblack.awk aitemp2.ps > aitemp1.ps
endif

cat aitemp1.ps > aitemp2.ps
if ($cust != 0 ) then
  if ($verb != 1) echo " Converting process colors to custom colors..."
  awk -f ${psutdir}/proc2cust.awk aitemp2.ps > aitemp1.ps 
endif
cat aitemp1.ps > aitemp2.ps
if ($cobw != 0 ) then
  if ($verb != 1) echo " Converting colors to grays..."
  awk -f ${psutdir}/remvcol.awk aitemp2.ps > aitemp1.ps
endif

cat aitemp1.ps > aitemp2.ps
if ($cugr != 0 ) then
 if ($verb != 1) echo " Converting grays to custom colors..."
 awk -f ${psutdir}/gray2cust.awk aitemp2.ps >aitemp1.ps
endif
cat aitemp1.ps > aitemp2.ps
if ($ctgr != 0 ) then
 if ($verb != 1) echo " Converting grays to tinted black custom colors..."
 awk -f ${psutdir}/gray2tintblack.awk aitemp2.ps >aitemp1.ps
endif
cat aitemp1.ps > aitemp2.ps
if ($covt != 0 ) then
 if ($verb != 1) echo " Inverting the colors..."
 awk -f ${psutdir}/invertc.awk aitemp2.ps >aitemp1.ps
endif
cat aitemp1.ps > aitemp2.ps
if ($bwvt != 0 ) then
 if ($verb != 1) echo " Inverting the grays..."
 awk -f ${psutdir}/invertb.awk aitemp2.ps >aitemp1.ps
endif
cat aitemp1.ps > aitemp2.ps
if ($cseg != 0 ) then
 if ($verb != 1) echo " Connecting line segments..."
 awk '{if ($0!="") {print $0}}' aitemp2.ps > aitemp1.ps
 cat aitemp1.ps > aitemp2.ps
 awk -f ${psutdir}/seg.awk aitemp2.ps >aitemp1.ps
endif
cat aitemp1.ps>aitemp2.ps
if ($form != 0 ) then
 grep -v '\*u' aitemp2.ps |\
 grep -v '\*U' > aitemp1.ps
endif
if ($pipe != 1) then
 cat aitemp1.ps> $eps
else
 cat aitemp1.ps
endif

if ($verb != 1) echo " Removing the temp files..." 
/bin/rm  aitemp1.ps aitemp2.ps aitempbb.ps 
if ($verb != 1) echo " Done."

# Version History
# 1.26 fixed ps check to work with sys w/o "file"
# 1.25 added switch to output AI88 formate (-f)
# 1.24 added test to check if infile.ps is PostScript
# 1.23 added line segment joining (-s)
# 1.22 elimiated need for 'bb2top.sh' so script works on SGI
# 1.21 added richblack filter (-r)
# 1.2  added error handling if no infile.ps given
# 1.15 man page
# 1.1  big update added command line options
# 1.0  minimum boundingbox handling 
