#!/bin/sh
# set -x

# make sure we have minimum arg count
if [ $# -lt 3 ]; then
    echo "funds9 [cmd] [xpa] [filename or filelist] [args ...]"
    exit 1
fi

# don't use gnuplot by default
DOGP=0
# disable multi-file support -- it conflicts with spaced directory support
DOMULTI=0

# process standard arguments
CMD="$1";     shift;
XPA="$1";     shift;
FILES="$1";   shift;

if [ x$DOMULTI = x1 ]; then
  if [ "NONE" != "NONE" ]; then
    NFILES=`echo $FILES | awk '{print NF}'`
    if [ $NFILES -gt 1 ]; then
      VERSION=`echo "show version" | gnuplot 2>&1 | grep version | awk '{print $3*10}'`
      if [ x"$VERSION" != x -a $VERSION -ge 37 ]; then
        DOGP=1
      fi
    fi
  fi
else
  FILE="$FILES"
fi

# process commands
case $CMD in

funcnts)
    if [ $# = 0 ]; then
      echo "ERROR: funcnts filename [source] [bkgd]"
      exit 1
    fi
    if [ x$DOMULTI = x1 ]; then
      for FILE in $FILES; do
        funcnts "$FILE" $* 2>&1
      done
    else
      funcnts "$FILE" $* 2>&1
    fi
    exit 0
    ;;

funhist)
    if [ $# != 3 ]; then
      echo "ERROR: funhist filename [norm width] [column] [bin]"
      exit 1
    fi
    ARGS=""
    PARAM=$1; shift;
    read NORM WIDTH <<EOF
      $PARAM
EOF
    if [ $NORM = 1 ]; then
      ARGS="$ARGS -n"
    fi
    if [ $WIDTH = 1 ]; then
      ARGS="$ARGS -w"
    fi
    if [ x$DOMULTI = x1 ]; then
      for FILE in $FILES; do
        funhist $ARGS "$FILE" $* 2>&1
      done
    else
      funhist $ARGS "$FILE" $* 2>&1
    fi
    exit 0
    ;;

funcnts_plot)
    if [ $# = 0 ]; then
      echo "ERROR: funcnts filename [source] [bkgd]"
      exit 1
    fi
    if [ x$DOMULTI = x1 ]; then
      for FILE in $FILES; do
        if [ $DOGP = 1 ]; then
          funcnts -rG "$FILE" $* | funcnts.plot -file "$FILE" gnuplot
        else
          funcnts -rG "$FILE" $* | funcnts.plot -file "$FILE" ds9 2>&1
        fi
      done
    else
      if [ $DOGP = 1 ]; then
        funcnts -rG "$FILE" $* | funcnts.plot -file "$FILE" gnuplot
      else
        funcnts -rG "$FILE" $* | funcnts.plot -file "$FILE" ds9 2>&1
      fi
    fi
    exit 0
    ;;

funhist_plot)
    if [ $# != 3 ]; then
      echo "ERROR: funhist filename [norm width] [column] [bin]"
      exit 1
    fi
    ARGS=""
    PARAM=$1; shift;
    read NORM WIDTH <<EOF
      $PARAM
EOF
    if [ $NORM = 1 ]; then
      ARGS="$ARGS -n"
    fi
    if [ $WIDTH = 1 ]; then
      ARGS="$ARGS -w"
    fi
    if [ x$DOMULTI = x1 ]; then
      for FILE in $FILES; do
        if [ $DOGP = 1 ]; then
          funhist $ARGS "$FILE" $* | funhist.plot -file "$FILE" gnuplot
        else
          funhist $ARGS "$FILE" $* | funhist.plot -file "$FILE" ds9 2>&1
        fi
      done
    else
      if [ $DOGP = 1 ]; then
        funhist $ARGS "$FILE" $* | funhist.plot -file "$FILE" gnuplot
      else
        funhist $ARGS "$FILE" $* | funhist.plot -file "$FILE" ds9 2>&1
      fi
    fi
    exit 0
    ;;

funcen)
    if [ $# != 5 ]; then
      echo "ERROR: funcen filename region(s) niter tol disp ftype"
      exit 1
    fi
    ARGS=""
    REGION=$1; shift;
    if [ x"$REGION" = x ]; then
      echo "ERROR: no region(s) specified"
      exit 1
    fi
    NITER=$1; shift;
    TOL=$1; shift;
    DISP=$1; shift;
    FTYPE=$1; shift;
    if [ "$FTYPE" = 1 ]; then
      ARGS="$ARGS -i"
    fi
    VERB=1
    TMP="/tmp/fun_${CMD}_$$.out"
    if [ x$DOMULTI = x1 ]; then
      for FILE in $FILES; do
        funcen $ARGS -n $NITER -t $TOL -v $VERB "$FILE" "$REGION" $* 2>&1 > $TMP
        cat $TMP
        if [ $DISP = "1" ]; then 
          xpaset -p $XPA regions deleteall
          grep final_region $TMP | awk '{print $2}'| xpaset $XPA regions
        fi
      done
    else
      funcen $ARGS -n $NITER -t $TOL -v $VERB "$FILE" "$REGION" $* 2>&1 > $TMP
      cat $TMP
      if [ $DISP = "1" ]; then 
        xpaset -p $XPA regions deleteall
        grep final_region $TMP | awk '{print $2}'| xpaset $XPA regions
      fi
    fi
    exit 0
    ;;

*)
    echo "ERROR: unknown function: " "$CMD"
    exit 1
    ;;
esac

