#!/usr/bin/bash

# sweep - Erases files from the erase directory for the specified mooring
# and year/yearday (yyyyjjj).

# Copyright (C) 2004 MBARI
# Author: Kent Headley
# MBARI provides this documentation and code "as is", with no warranty,
# express or implied, of its quality or consistency. It is provided without
# support and without obligation on the part of the Monterey Bay Aquarium
# Research Institute to assist in its use, correction, modification, or
# enhancement. This information should not be published or distributed to
# third parties without specific written permission from MBARI.

# Set default values
OASISDIR=/oasis
erasedir=$OASISDIR/raw/erase
mooring="*"
verbose=0
yryrday=""
quiet=0
let period=-1
datestr=`date +%m/%d/%Y`

# function usage()
# Prints use message to console and exits
# arguments: none
# returns: none
usage(){
echo
echo "Erases files for specified mooring and year/yearday (yyyyjjj)"
echo
echo "Usage $0 -y <year/yearday> [ -m <mooring> -d <directory> -hvQ [-p <period> [-D <startdate>]] ]"
echo
echo "-d <directory>     Directory [$erasedir]"
echo "-m <mooring>       Mooring [$mooring]"
echo "-y <year/yearday>  Year and yearday (yyyyjjj) [no default]"
echo "-p <period>        Days before start date (use if >0, else use year/yearday) [$period]"
echo "-D <start date>    Start date [current date]"
echo 
echo "-h                 Display this help message"
echo "-v                 Enable verbose output [$verbose]"
echo "-Q                 Quiet (no prompt) [$quiet]"
echo 

exit 1
}

# Process command line
while getopts "d:D:hm:p:Qvy:" Option 
do
   case $Option in
   [d] ) 
	    erasedir="$OPTARG"
    ;;
   [D] ) 
	    datestr="$OPTARG"
    ;;
   [m] ) 
	    mooring="$OPTARG"
    ;;
   [p] ) 
	    let period="$OPTARG"
    ;;
   [y] ) 
	    yryrday="$OPTARG"
    ;;
   [Q] ) 
	    quiet=1
    ;;
   [v] ) 
	    verbose=1
    ;;
   [h] ) 
	    usage
    ;;
    * ) echo unrecognized option 
    ;;
    esac
done

if [ "$yryrday" == "" ]
then 
 if [ "$period" -lt 0 ]
 then
  usage
 else
  yryrday=`yyd -p $period -d $datestr`
 fi
fi

filespec="$erasedir/$mooring.$yryrday*"

if [ "$verbose" -ne 0 ]
then
echo erasedir $erasedir mooring $mooring yryrday $yryrday
fi

if [ "$quiet" -eq 0 ]
then
echo "You are about to delete $filespec"
read -p "Press CTRL-C to abort or any other key to continue" 
fi

if [ "$verbose" -ne 0 ]
then
echo "Deleting $filespec"
fi

rm $filespec