#!/bin/bash
#                       /usr/local/bin/calundar
#  http://crystalfaeries.net/posix/bin/calundar
# celeste:crystalfaery CALUNDAR 2021-02-24 20:30:49+00:00

# calundar uses the command-line program tide (part of xtide package)
# to calculate this year's {New,Full} Moons, and {Moon,Sun}{Rise,Set}
# which then REPLACE such in the invoking user's ~/.calendar/calendar
# because the *NIX calendar program only works for the CURRENT year;
# it is invoked anually by each user who wishes to track lunations,
# with this crontab entry if you leave computer on for year change:
# 00	00	 1	 1	*	/usr/local/bin/calundar
# Optional argument is the xtide location from http://127.0.0.1:8080/
let help=13	# one less than this line#

# CONFIGURATION:
LOCATION="Hanalei"	# my default xtide location
TEMP="${HOME}/.calendar/.calendar.txt"

year="`date +%Y`"	# what year is it?

case $# in
0)	:	# no arguments - just do the default location
	;;
1)	# we have an argument:
	case "${1}" in
	-v | --version )
		head -n 4 $0 | tail -n 1
		exit
		;;
	-h | --help )
		head -n ${help} $0
		exit
		;;
	*)	# the argument is the xtide location from http://127.0.0.1:8080/
		LOCATION="${1}"	# the specified xtide location
		;;
	esac
	;;
*)	# multiple arguments? blah... you lose:
	echo "$0 does not cognize $@"
	echo "optional argument is the xtide location from http://127.0.0.1:8080/"
	firefox	http://127.0.0.1:8080/ < /dev/null >& /dev/null & disown %1	# may i assist you?
	exit 1
	;;
esac

# VERIFY we have necessary SOFTWARE
if [ ! -x /usr/bin/calendar ]
then
	echo 'sudo apt-get install calendar || exit -1'
	      sudo apt-get install calendar || exit -1
fi
if [ ! -x /usr/bin/tide     ]
then
	echo 'sudo apt-get install tide xtide || exit -2'
	      sudo apt-get install tide xtide || exit -2
fi

# BACKUP user's old calendar
for f in $(ls -r "${HOME}"/.calendar/calendar*)
do
     mv "${f}" "${f}"~	|| exit -3
done			|| exit -3


# STRIP OLD lunations and solations from old calendar
grep -v	"Full Moon"	"${HOME}"/.calendar/calendar~	| \
grep -v	"New Moon"					| \
grep -v	"Moonrise"					| \
grep -v	"Moonset"					| \
grep -v	"Sunrise"					| \
grep -v	"Sunset"	>	"${TEMP}"

# ADD NEW year's lunations
# maybe tide has an option to only show lunations without tides to save us some grepping here:
tide -l "$LOCATION" -tf "%T %Z" -b "$year-01-01 00:00" -e "$year-12-31 23:59" 2>/dev/null | grep Moon | \
sed "s/ /	/;s/^$year-//g;s/-/ /g;s/  / /g;s/  / /g;s/	 /	/g;s/ /\//"	>> "${TEMP}"

# ADD NEW year's solations
# maybe tide has an option to only show sunations without tides to save us some grepping here:
tide -l "$LOCATION" -tf "%T %Z" -b "$year-01-01 00:00" -e "$year-12-31 23:59" 2>/dev/null | grep Sun | \
sed "s/ /	/;s/^$year-//g;s/-/ /g;s/  / /g;s/  / /g;s/	 /	/g;s/ /\//"	>> "${TEMP}"

# CONSTRUCT user's NEW calendar
echo    "LANG=utf-8"				>  "${HOME}"/.calendar/calendar
grep -v "LANG"	"${TEMP}" | sort -u | sort -n	>> "${HOME}"/.calendar/calendar \
&&	rm	"${TEMP}"			   "${HOME}"/.calendar/calendar*\~\~*

exit $?	# pau for now
