#! /bin/csh -f
# Last edited on 2001-11-10 23:57:11 by stolfi

set usage = "$0 [ -show ] [ PZDRAWCANDS_OPTION ]... CANFILE FLCDIR BAND PSNAME"

# If "-epsFormat", Output is PSNAME-pg00000.eps, PSNAME-pg00001.eps, ...
# Otherwise it is PSNAME.ps

set cmd = $0
echo "=== begin ${cmd:t} ===" 
echo "${cmd:t} $*"

set opts = ( )
set show = 0
set epsFormat = 0
while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) )
  if ( "x$1" == "x-show" ) then
    set show = 1; shift
  else if ( ( $#argv >= 2 ) && ( "x$1" == "x-displace" ) ) then
    set opts = ( ${opts} -displace "$2" ); shift; shift
  else if ( ( $#argv >= 3 ) && ( "x$1" == "x-tilt" ) ) then
    set opts = ( ${opts} -tilt "$2" "$3" ); shift; shift; shift
  else if ( ( $#argv >= 5 ) && ( "x$1" == "x-shift" ) ) then
    set opts = ( ${opts} -shift "$2" "$3" "$4" "$5" ); 
    shift; shift; shift; shift; shift
  else if ( ( $#argv >= 2 ) && ( "x$1" == "x-labelSize" ) ) then
    set opts = ( ${opts} -labelSize "$2" ); shift; shift
  else if ( ( $#argv >= 2 ) && ( "x$1" == "x-grid" ) ) then
    set opts = ( ${opts} -grid "$2" ); shift; shift
  else if ( ( $#argv >= 3 ) && ( "x$1" == "x-plotSize" ) ) then
    set opts = ( ${opts} -plotSize "$2" "$3" ); shift; shift; shift
  else if ( ( $#argv >= 2 ) && ( "x$1" == "x-nominalCandWidth" ) ) then
    set opts = ( ${opts} -nominalCandWidth "$2" ); shift; shift
  else if ( ( $#argv >= 2 ) && ( "x$1" == "x-drawMatchEvery" ) ) then
    set opts = ( ${opts} -drawMatchEvery "$2" ); shift; shift
  else if ( "x$1" == "x-drawAllSamples" ) then
    set opts = ( ${opts} -drawAllSamples" ); shift;
  else if ( "x$1" == "x-trim" ) then
    set opts = ( ${opts} -trim ); shift
  else if ( "x$1" == "x-sampleDots" ) then
    set opts = ( ${opts} -sampleDots ); shift
  else if ( "x$1" == "x-drawMatch" ) then
    set opts = ( ${opts} -drawMatch ); shift
  else if ( "x$1" == "x-noColors" ) then
    set opts = ( ${opts} -noColors ); shift
  else if ( "x$1" == "x-noThicker" ) then
    set opts = ( ${opts} -noThicker ); shift
  else if ( "x$1" == "x-noPointers" ) then
    set opts = ( ${opts} -noPointers ); shift
  else if ( "x$1" == "x-noFrame" ) then
    set opts = ( ${opts} -noFrame ); shift
  else if ( "x$1" == "x-epsFormat" ) then
    set opts = ( ${opts} -epsFormat ); set epsFormat = 1; shift
  else
    echo "usage: ${usage}"; exit 1
  endif
end

if ( $#argv != 4 ) then
  echo "usage: ${usage}"; exit 1
endif

set canfile = "$1"; shift
set flcdir = "$1"; shift;
set band = "$1"; shift;
set psname = "$1"; shift;

if ( $#argv != 0 ) then
  echo "usage: ${usage}"; exit 1
endif

set path = ( \
  ${STOLFIHOME}/programs/pm3-1.1.15/THESES/hcgl/1/bin/${PLATFORM} \
  ${cmd:h} \
  ${path} \
)

if ( "/${canfile:e}" == "/" ) then
  set canfile = "${canfile}.can"
endif

if ( ! -d ${flcdir} ) then
  echo "directory ${flcdir} not found"; exit 1
endif

source ${flcdir}/f${band}.parms
set step = `gawk -v st=${step} 'BEGIN{if (st==0){st=0.0212;} printf "%10.3f", st;}'`
echo "step = ${step}"

if ( "/${psname:e}" != "/" ) then
  echo "EPS prefix should have no extension"; exit 1
endif

echo "converting ${canfile} to ${psname}-pgNNNNN.eps..."

which PZDrawCands
nice PZDrawCands \
  -input ${canfile:r} -step ${step} \
  -chainDir ${flcdir} -chainPrefix f \
  -band ${band} \
  -output ${psname} \
  -nominalCandWidth 200 \
  -nCandsPerPage 1 1 \
  -maxCands 60 \
  ${opts}

if ( $show ) then
  if ( $epsFormat ) then
    foreach f ( ${psname}-pg[0-9][0-9][0-9][0-9][0-9].eps )
      ghostview ${f}
    end
  else
    ghostview ${psname}.ps
  endif
endif

echo "=== end ${cmd:t} =========" 


