#!/bin/bash
#                       /usr/local/bin/calundar
#  http://crystalfaeries.net/posix/bin/calundar
# celeste:crystalfaery 2016-07-31 06:30:19+00:00

# calundar uses the command-line program tide (part of xtide package)
# to calculate this year's New and Full Moons,
# which are then used to REPLACE same in the invoking user's ~/.calendar/calendar
# because the UNIX calendar program only works for the CURRENT year.
# It is invoked anually by each user who wishes to track lunations with this crontab entry:
# 00	00	 1	 1	*	/usr/local/bin/calundar

# CONFIGURATION:
LOCATION="Hanalei"	# the nearest xtide location

# VERIFY we have necessary SOFTWARE
if [ ! -x `which calendar` ]
then
	exit -1
fi
if [ ! -x `which tide`     ]
then
	exit -2
fi

# BACKUP user's old calendar
mv $HOME/.calendar/calendar \
   $HOME/.calendar/calendar~

# STRIP OLD lunations from old calendar
grep -v "Full Moon"	$HOME/.calendar/calendar~ | \
grep -v  "New Moon"		>> /tmp/$$

# ADD NEW year's lunations
year="`date +%Y`"
# maybe tide has an option to only show lunations without tides to save us some grepping here:
tide -l "$LOCATION" -b "$year-01-01 00:00" -e "$year-12-31 23:59" 2>/dev/null | \
grep Moon | grep -v Moonrise | grep -v Moonset | \
sed "s/ /	/;s/^$year-//g;s/-/ /g;s/  / /g;s/  / /g;s/	 /	/g;s/ New Moon/	New Moon/g;s/ Full Moon/	Full Moon/g"	>> /tmp/$$

# CONSTRUCT user's NEW calendar
echo    "LANG=utf-8"				>> $HOME/.calendar/calendar
grep -v "LANG"	/tmp/$$ | sort -u | sort -n	>> $HOME/.calendar/calendar



syntax highlighted by Code2HTML, v. 0.9.1