#!/bin/sh
#
# $Id: darkbat,v 1.1 2005/01/02 19:54:14 hroch Exp $
#
#set -x

function rum ()
{
  S=$1
  DRK=$2
  OUT=$3
  ICIT=$4
  RENAME=$5

echo -n "($S) - ($DRK) = "

  BUF=pipe$RANDOM

cat >  $BUF << EOF
SCIFRAME = $S
DARKFRAME = $DRK
MASK = $OUT
ICIT = $ICIT
EOF

NECO=$(darkbat.bin < $BUF)
if [ "$NECO" ]; then
  echo -n "($NECO)"
  rm -rf $BUF

  if [ $RENAME = yes ]; then
     echo -n ", mv "
     mv -v $NECO $S
  elif [ $KEEP = yes ]; then
     echo -n ", mv "
     S1=$(basename $S)
     mv -v $NECO $S1
  else
     echo
  fi
  return 0
else
  echo 
  cat $S.err
  rm -f $S.err
  return 1
fi
  
}

function help ()
{
echo
echo "DARKBAT   Utility for the dark frame subtract."
echo "Usage:darkbat [options] [@dirfile] [dark=darkframe] [mask=?] [image1] ..."
echo "@dirfile= file with image names for dark substract, no wildcards *,?"
echo "          (for only '@', read image names from standart input)"
echo "dark= dark frame image"
echo "image1...= name(s) of simple image(s) to dark subtract, no wildcards *,?"
echo "mask= output mask (default = $DNAME)"
echo "      give 'mask=.' for overwrite input files (carefully!)"
echo "      give 'mask=$' for keep (overwrite) files with orginal name in current dir"
echo "options:"
echo "      -d <darkname> dark frame image (equivalent with 'dark=')"
echo "      -o <mask>    output mask (equivalent with 'mask=')"
echo "      -c <number>  counter initial value (default = 1)"
echo "      -h        give this help"
echo "      -L        display software license" 
}


function license ()
{
echo " This program is free software; you can redistribute it and/or modify"
echo " it under the terms of the GNU General Public License as published by"
echo " the Free Software Foundation; either version 2, or (at your option)"
echo " any later version."
echo " This program is distributed in the hope that it will be useful"
echo " but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
echo " GNU General Public License for more details."
echo " You should have received a copy of the GNU General Public License"
echo " along with this program; if not, write to the Free Software"
echo " Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA."
}

echo "DARKBAT Version 1.6, Copyright (C) 1997-4 F.Hroch, Masaryk University, Brno, CZ"

DNAME='dout????.fits'
OUT=$DNAME
ICIT0=1

if [ $# -eq 0 ]; then
  help
  exit 0
fi

while [ "$#" -ge 1 ]; do
  
  case "$1" in
     "--help" | "-h" )
         help
         exit 0
         ;;
     
     "--license" | "-L" )
         license
         exit 0
         ;;
     
     "-d" )
         shift
         DRK=$1
         ;;
     "-o" )
         shift
         OUT=$1
         ;;
     "-c" )
         shift
         ICIT0="$1"
         ;;

     *)
       if [ ! "${1//#dark=}" = "$1" ]; then
          DRK=${1#dark=}
       elif [ ! "${1//#mask=}" = "$1" ]; then 
          OUT="${1#mask=}"
       elif [ ! "${1//#@}" = "$1" ]; then 
          NAMSCI="${1#@}"
       else
          SCI="$SCI $1"
       fi
       ;;
  esac
  shift
done

if [ ! -f "$DRK" ]; then
   echo "Dark frame file not found." > /dev/stderr
   exit 1
fi

if [ "$OUT" = "." ]; then
   OUT=$DNAME
   RENAME=yes
else
   RENAME=no
fi

if [ "$OUT" = "$" ]; then
   OUT=$DNAME
   KEEP=yes
else
   KEEP=no
fi

ICIT=$ICIT0

if [ "$SCI" ]; then

  for S in $SCI; do
    rum $S $DRK $OUT $ICIT $RENAME
    ICIT=$(($ICIT+1))
  done

else

  if [ "$NAMSCI" ]; then

    for S in $(cat $NAMSCI); do
      rum $S $DRK "$OUT" $ICIT $RENAME
      ICIT=$((ICIT+1))
    done

  else

    for S in $(cat); do
      rum $S $DRK "$OUT" $ICIT $RENAME
      ICIT=$(($ICIT+1))
    done

  fi

fi

if [ $ICIT == $ICIT0 ]; then
  echo "No input files." > /dev/stderr
fi

exit 0
