#!/bin/sh
#
# $Id: tideplot,v 1.2 2006/04/19 04:34:48 geoff Exp $
#
# Plot the tide for a given location in a gkrellm chart, using the xtide
# package.
#
USAGE='Usage: tideplot [options] xtide-location
Options:
    -i interval	Minutes between tide samples (default 15)
    -o output	Give name of output file (default is
		~/.gkrellm2/data/fchart/tide.chart)
    -q		Omit sun and moon data from tooltip
    -s sleep	Seconds to sleep between updates (default same as -i)
    -w width	The width of the gkrellm chart (default 60)'
#
# FORMAT OF THE CHART FILE
#
# Only one value is charted, which is the tide height.  At each
# invocation, "width" values are charted, such that the left side of
# the resulting chart show be the current tide height and the right
# side will show the tide at some time in the future (the exact time will be .
#
# The alert string is always NORMAL.
#
# The displayable parameterized strings are:
#
#	$0	The current tide height, as a floating-point number
#	$1	The units of the current height (feet or meters)
#	$2	The lower tide bound used to adjust the chart to a
#		nonnegative value
#	$3	The scale factor applied to make the tide value integer
#		(always 100 for feet, 1000 for meters).
#
# The tooltip is the current output of "tide -m p" and "tide -v",
# possibly with sun and moon data grepped out.
#
# $Log: tideplot,v $
# Revision 1.2  2006/04/19 04:34:48  geoff
# Don't report moon quarter-phases in quiet mode.
#
# Revision 1.1  2006/03/14 08:54:19  geoff
# Initial revision
#

CHART_WIDTH=60
MINUTES=15
OUTPUT_FILE=/$HOME/.gkrellm2/data/fchart/tide.chart
SLEEP_TIME=
QUIET=false

while [ $# -gt 0 ]
do
    case "$1" in
	-i)
	    MINUTES="$2"
	    shift
	    ;;
	-o)
	    OUTPUT_FILE="$2"
	    shift
	    ;;
	-q)
	    QUIET=true
	    ;;
	-s)
	    SLEEP_TIME="$2"
	    shift
	    ;;
	-w)
	    CHART_WIDTH="$2"
	    shift
	    ;;
	--)
	    shift
	    break
	    ;;
	-*)
	    echo "$USAGE" 1>&2
	    exit 2
	    ;;
	*)
	    break
	    ;;
    esac
    shift
done

if [ $# -ne 1 ]
then
    echo "$USAGE" 1>&2
    exit 2
fi

LOCATION="$1"

INTERVAL=`expr "$MINUTES" \* 60`
if [ -z "$SLEEP_TIME" ]
then
    SLEEP_TIME="$INTERVAL"
fi
HOURS=`expr "$MINUTES" / 60`
MINUTES=`expr "$MINUTES" % 60`
case "$MINUTES" in
    ?)
	MINUTES="0$MINUTES"
	;;
esac

#
# Pick out the mathematical lower bound for the location.
#
scale=`tide -l "$LOCATION" -m s 2>/dev/null \
  | awk '
	/Mathematical lower bound/ \
	    {
	    if ($5 == "feet")
		print 100
	    else
		print 1000
	    }'`
lowerBound=`tide -l "$LOCATION" -m s 2>/dev/null \
  | awk '
	/Mathematical lower bound:/ \
	    {
	    if ($5 == "feet")
		scale = 100
	    else
		scale = 1000
	    if ($4 != int($4))
		{
		if ($4 < 0)
		    print (int($4) - 1) * scale
		else
		    print int($4) * scale
		}
	    }'`

while :
do
    #
    # It is well known that I hate perl.  However, there isn't another
    # good choice for portably invoking strftime, and it seems a bit
    # silly to write my own date-conversion code when perl is
    # available everywhere.  So we'll use perl to calculate the
    # parameters to tide.
    #
    tideStart=`perl -e '
	use POSIX qw(strftime);
	print strftime "%Y-%m-%d %H:%M", localtime;
	'`
    tideEnd=`perl -e '
	use POSIX qw(strftime);
	$then = time + '"$CHART_WIDTH * $INTERVAL"';
	print strftime "%Y-%m-%d %H:%M", localtime($then);
	'`

    #
    #
    # Now let the tide program generate lots of data, and convert it
    # to a format useful for gkrellm-fchart.
    #
    tide -l "$LOCATION" -m r -s "$HOURS:$MINUTES" \
	-b "$tideStart" -e "$tideEnd" \
	2>/dev/null \
      | awk 'NR == 1 \
		{
		levelNow = $2
		}
		{
		print int($2 * '"$scale - $lowerBound"' + 0.5)
		}
	    END \
		{
		print "NORMAL"
		printf "%.2f\n", levelNow
		if ('"$scale"' == 1000)
		    print "meters"
		else
		    print "feet"
		print '"$lowerBound"'
		print '"$scale"'
		print "!!TOOLTIP!!"
		}' \
      > "$OUTPUT_FILE"
    tide -l "$LOCATION" -m p 2>/dev/null \
      | tr -d \\260 \
      | if $QUIET
	then
	    egrep -v 'Sun|Moon|Quarter'
	else
	    cat
	fi \
      >> "$OUTPUT_FILE"
    echo "" >> "$OUTPUT_FILE"
    tide -v 2>> "$OUTPUT_FILE"
    sleep $SLEEP_TIME
done
