#! /bin/csh -f
# Last edited on 2001-11-01 14:07:58 by stolfi

set usage = "$0 [-center CX CY] [-size SZX SZY] [-label DX DY SIZE XXX] [-sampleDots] [-grid NNN] [-plotStep NNN] FLCFILE EPSNAME"

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

set opts = ( )
set center = ( 0 0 )
set size = ( 250 250 )
while ( ( $#argv > 0 ) && ( "x$1" =~ x-* ) )
  if ( ( $#argv >= 5) && ( "x$1" == "x-label" ) ) then
    set opts = ( ${opts} -label "$2" "$3" "$4" "$5" );
    shift; shift; shift; shift; shift;
  else if ( ( $#argv >= 3) && ( "x$1" == "x-center" ) ) then
    set center = ( "$2" "$3" ); shift; shift; shift;
  else if ( ( $#argv >= 3) && ( "x$1" == "x-size" ) ) then
    set size = ( "$2" "$3" ); shift; shift; shift;
  else if ( ( $#argv >= 2) && ( "x$1" == "x-plotStep" ) ) then
    set opts = ( ${opts} -plotStep "$2" ); shift; shift;
  else if ( ( $#argv >= 2) && ( "x$1" == "x-grid" ) ) then
    set opts = ( ${opts} -grid "$2" ); shift; shift;
  else if ( "x$1" == "x-sampleDots" ) then
    set opts = ( ${opts} "-sampleDots" ); shift
  else
    echo "usage: ${usage}"; exit 1
  endif
end

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

set flcfile = $1; shift;
set epsname = $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} \
)

set region = ( \
  ` gawk -v cx=${center[1]} -v cy=${center[2]} -v sx=${size[1]} -v sy=${size[2]} 'BEGIN{printf "%.3f %.3f %.3f %.3f", cx-sx/2, cx+sx/2, cy-sy/2, cy+sy/2;}' ` \
)

if ( "/${flcfile:e}" == "/" ) then
  set flcfile = "${flcfile}.flc"
endif

if ( "/${epsname:e}" == "/.eps" ) then
  set epsname = "${epsname:r}"
endif

set winname = "/tmp/$$-${flcfile:t}"

cat <<EOF > ${winname}.wnd
begin plot window (format of 96-04-11)
XMin = ${region[1]}
XMax = ${region[2]} 
YMin = ${region[3]}
YMax = ${region[4]}
end plot window
EOF

echo ${flcfile}' -> '${epsname}.eps' ...'

/bin/rm -f ${epsname}.eps

echo "opts = ( ${opts} )"

nice PZDraw \
  -window ${winname} \
  -output ${epsname} \
  ${opts} \
  -epsFormat \
  < ${flcfile}

/bin/rm ${winname}.wnd

ghostview ${epsname}.eps

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


