#!/bin/bash
#                        /usr/local/bin/astrolog
#  https://crystalfaeries.net/posix/bin/astrolog
# celeste:crystalfaery ASTROLOG 2017-11-05 22:05:37+00:00
# a bash shell wrapper around use of /usr/local/games/astrolog-6.20/astrolog

# Invocation with no arguments:
# generate current calendar report for website
# generate current time-space file for astrolog
# (used also by manual invocations for astrolog)
# generate current astrolog chart  for website
# generate current astrolog report for website
# generate current weather  report for website
# this should be invoked every 15 minutes by cron;
# (if you change the period here, change also the refresh line for the generated file):
# 09,24,39,54  * * * *	/usr/local/bin/astrolog 2>> /var/log/chronicle.log 1>&2

# Optional Arguments specify the date and time of the astrolog chart to be generated,
# optionally followed by the event name, e.g. "Full Moon":
# astrolog 2017 Aug 21 06:17

# Optional Arguments are "-d" and the date argument, to delete a previously generated astrolog
# astrolog -d 2017 Aug 21 06:17

help=24	# the file's line number of this line -1

# install ERROR / EXIT HANDLER:
function cleanup {
	if	[ -e	/tmp/$$.txt ]
	then
		rm	/tmp/$$.txt	# cleanup our temporary file
	fi
	exit		$exitval	# return error code
}
let exitval=0				# default to success
trap	cleanup		0 1 2 15	# activate error handler

# Handle Arguments:
if [ $# -eq 0 ]
then	# There are no arguments to process, just do the current chart for the website:
# /usr/bin/hcal -h > /home/crystalfaeries.net/hcal.html							2>> /var/log/chronicle.log
# ARCHIVE HEADER:
cd	/home/crystalfaeries.net/									2>> /var/log/chronicle.log 1>&2
echo '<html><head><title>crystalfaeries Chronology</title><meta http-equiv="refresh" content="900"></head>' \
	>  /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
echo '<body bgcolor="#000000" text="#FFFFFF" vlink="#FFFFFF" alink="#333333" link="#CCCCCC">' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
# echo '<a href="http://crystalfaeries.net:8080/locations/Hanalei"><img src="/clipart/matthewg42_lighthouse.png" alt="[Tides]" align="left" border="0" width="36" height="144"></a>' >> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
echo '<a href="https://www.youtube.com/watch?v=WogssRzGbZU"><img src="/imgs/moon.png" alt="[moon phase]" align="left" border="0" width="95" height="95"></a>' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
echo '<p align="left"><a href="https://ixquick.com/">http://</a><a href="/">crystalfaeries.net/</a><a href="/fae/">fae/</a><a href="/recently_updated.html">archive/</a></p>' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
# echo '<a href="/news.html"><img align="left" alt="[News]" width="96" height="96" src="/clipart/dog_with_a_rolled_up_newspaper_in_its_mouth_blackground.jpg"></a>'  >> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
# echo '<a href="http://crystalfaeries.net:8000/"><img alt="[IceCast2]" width="90" height="96" align="left" src="/clipart/icecast.png"></a>' >> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
echo '<h1 align="center"><a href="/fae/">crystalfaeries</a> <a href="http://www.leapsecond.com/java/gpsclock.htm">Chronology</a></h1>' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
#echo '<p align="center">This page auto-updates every 15 minutes.</p>' >> /home/crystalfaeries.net/fae/archive/HEADER.html	2>> /var/log/chronicle.log
echo '<br clear="all">' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
echo '<a href="/imgs/astro/astrological_house_keywords.png"><img src="/imgs/astro.bmp"	align="left" alt="[Astrolog BitMap]" width="640" height="480"></a><br clear="all">' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
echo "<pre>" \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
echo "     <a href="/fae/archive/">`date +'%Y-%m-%d'`</a>" \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
/usr/bin/cal -A1 | sed 's/_//g' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
let exitval=$? ; if [[ $exitval -ne 0 ]] ; then exit $exitval ; fi			# ERROR EXIT
let DAYS=0	# try for automatic	today or weekend:
/usr/bin/calendar	>		/tmp/$$.txt					2>> /var/log/chronicle.log
let exitval=$?
# if [[ $exitval -ne 0 ]] ; then exit $exitval ; fi			# ERROR EXIT
until [ -s 				/tmp/$$.txt ]
do	# progressively scan beyond	today or weekend for next event:
	let		DAYS=$DAYS+1							2>> /var/log/chronicle.log
	/usr/bin/calendar -l $DAYS >	/tmp/$$.txt					2>> /var/log/chronicle.log
	let exitval=$?
	if [[ $exitval -ne 0 ]] ; then break ; fi
	if [ 365 -lt   $DAYS ]
	then
	    echo "12 31			New Year's Eve" >> $HOME/.calendar/calendar	2>> /var/log/chronicle.log
	    echo "12 31			New Year's Eve" \
				>	/tmp/$$.txt					2>> /var/log/chronicle.log
	fi
done
while [ -s				/tmp/$$.txt ]
do	# expand escape sequences from ~/.calendar/calendar file
	echo -e "$(head -n 1		/tmp/$$.txt)" \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
	tail -n +2			/tmp/$$.txt > \
		/tmp/.$$.txt								2>> /var/log/chronicle.log
	mv	/tmp/.$$.txt		/tmp/$$.txt					2>> /var/log/chronicle.log
done
echo	"" \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
/usr/bin/tide -m c -l hanalei \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
echo	"" \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log

# ASTROLOG:
# generate current ASTROLOG TIME-SPACE file
cd			/usr/local/games/astrolog-6.20/					2>> /var/log/chronicle.log 1>&2
echo '@0103  ; Astrolog chart info.'				>	kilauea.txt	2>> /var/log/chronicle.log
echo "-qb `date +'%b %d %Y %H:%M'` ST 10:00W 159:24W 22:13N"	>>	kilauea.txt	2>> /var/log/chronicle.log
echo '-zi "Today" "Kilauea"'					>>	kilauea.txt	2>> /var/log/chronicle.log
# generate current ASTROLOG CHART:
/usr/local/games/astrolog-6.20/astrolog -YQ 32765 -Yt -I	-i	kilauea.txt	\
			-Xbb -Xo /home/crystalfaeries.net/imgs/astro.bmp				2>> /var/log/chronicle.log 1>&2
let exitval=$? ; if [[ $exitval -ne 0 ]] ; then exit $exitval ; fi			# ERROR EXIT
# generate current ASTROLOG ANALYSIS:
/usr/local/games/astrolog-6.20/astrolog -v -g0 -j0 -7 -d -D -Yt -Zd -I -YQ 32765 -i kilauea.txt \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
let exitval=$? ; if [[ $exitval -ne 0 ]] ; then exit $exitval ; fi			# ERROR EXIT
echo	'</pre>' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log

# WEATHER:
cd	/home/crystalfaeries.net/									2>> /var/log/chronicle.log 1>&2
echo	'<a name="weather"><a href="http://www.prh.noaa.gov/hnl/satellite/Hawaii_IR_loop.gif"><img src="http://www.ssd.noaa.gov/goes/west/hi/rb.jpg" align="left" alt="[GOES West Rainbow Infra-Red Ch.4]" width="720" height="480"></a></a>' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
echo	'<a href="http://weather.hawaii.edu/satellite/still.cgi?product=satellite&res=4km&banner=uhmet&chnl=ir&domain=hus&size=large&satplat=goeswest&anim=no&overlay=on"><img src="http://www.prh.noaa.gov/hnl/satellite/latest/CPAC_IR.gif" align="left" alt="[Central Pacific Huricane Center Infra-Red satellite image]" width="525" height="420"></a>' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
echo	'<br	clear="all">'	>> /home/crystalfaeries.net/fae/archive/HEADER.html			2>> /var/log/chronicle.log
echo	'<a href="http://www.hamqsl.com/solar.html" title="Click to add Solar-Terrestrial Data to your website!"><img src="http://www.hamqsl.com/solargraph.php" align="left"></a><br clear="left">' \
	>> /home/crystalfaeries.net/fae/archive/HEADER.html						2>> /var/log/chronicle.log
# rm /home/crystalfaeries.net/fae/archive/index.html
rsync -auvzH /home/crystalfaeries.net/fae/archive/HEADER.html	divservi@box6537.bluehost.com:~/crystalfaeries.net/fae/archive	2>> /var/log/chronicle.log 1>&2
sleep 10	# bluehost rate-limits ssh connections :-(
rsync -auvzH /home/crystalfaeries.net/imgs/{astro.bmp,moon.png}	divservi@box6537.bluehost.com:~/crystalfaeries.net/imgs		2>> /var/log/chronicle.log 1>&2
let exitval=$?

###################################################################################################################################
else	# OH! we have an argument! let's hope we win? :-) #########################################################################
case "${1}" in
-v)
	head -n 4 $0 | tail -n 1	# just the date line
	let exitval=$?
	exit	$exitval
	;;
--version)
	head -n 4 $0 | tail -n 1	# just the date line
	let exitval=$?
	exit	$exitval
	;;
-h)
	head -n $help $0		# the whole header of comments
	let exitval=$?
	exit	$exitval
	;;
--help)
	head -n $help $0		# the whole header of comments
	let exitval=$?
	exit	$exitval
	;;
-d)
	shift	# consume the -d argument
	# remove the astrolog chart wheel and interpretation
	the_year="${1}"
	the_month="${2}"
	the_day="${3}"
	the_time="${4}"
	the_date=$(date --rfc-3339=date --date="${the_month} ${the_day} ${the_year}")
		      the_file="/home/crystalfaeries.net/src/${the_date}.$(echo ${the_time}|sed 's/:/-/').txt"
		  the_prefile="/home/crystalfaeries.net/src/.${the_date}.$(echo ${the_time}|sed 's/:/-/').txt"
	      the_image="/home/crystalfaeries.net/imgs/astro/${the_date}.$(echo ${the_time}|sed 's/:/-/').bmp"
	optimized_image="/home/crystalfaeries.net/imgs/astro/${the_date}.$(echo ${the_time}|sed 's/:/-/').png"
	delete	"${the_prefile}"	"${the_file}"	"${the_image}"	"${optimized_image}"	2>/dev/null
	let exitval=$?
	exit	$exitval
	;;
*)
############################################################################
# Arguments constitute a date+time spec in the format of 2017 Aug 21 06:17 #
############################################################################

the_year="${1}"
the_month="${2}"
the_day="${3}"
the_time="${4}"
the_event="${5}"
the_date=$(date --rfc-3339=date --date="${the_month} ${the_day} ${the_year}")
the_file="/home/crystalfaeries.net/src/${the_date}.$(echo ${the_time}|sed 's/:/-/').txt"
                the_image="/imgs/astro/${the_date}.$(echo ${the_time}|sed 's/:/-/').bmp"
# generate the file header:
echo	"Date:	${the_date} ${the_time}:00+00:00"> "${the_file}"	2>> /var/log/chronicle.log
expire						>> "${the_file}"	2>> /var/log/chronicle.log
echo	""					>> "${the_file}"	2>> /var/log/chronicle.log
echo	'<a	href="/imgs/astro/astrological_house_keywords.png"'	\
						>> "${the_file}"	2>> /var/log/chronicle.log
echo -n	'><img	align="left" width="640" height="480" src="'			\
						>> "${the_file}"	2>> /var/log/chronicle.log

echo -n	"${the_image}"				>> "${the_file}"	2>> /var/log/chronicle.log
echo	'"></a>'				>> "${the_file}"	2>> /var/log/chronicle.log
echo	'<br	clear="all">'			>> "${the_file}"	2>> /var/log/chronicle.log
echo	"<pre>"					>> "${the_file}"	2>> /var/log/chronicle.log
##################################################################################################
# ASTROLOG:
# generate current ASTROLOG TIME-SPACE file
cd	/usr/local/games/astrolog-6.20/							2>> /var/log/chronicle.log 1>&2
echo '@0103  ; Astrolog chart info.'				> kilauea.txt		2>> /var/log/chronicle.log
echo "-qb ${the_month} ${the_day} ${the_year} ${the_time} ST 10:00W 159:24W 22:13N"	2>> /var/log/chronicle.log >> kilauea.txt
echo '-zi "Today" "Kilauea"'					>> kilauea.txt		2>> /var/log/chronicle.log
# generate the ASTROLOG CHART:
/usr/local/games/astrolog-6.20/astrolog -YQ 32765 -Yt -I	-i kilauea.txt	\
					-Xbb -Xo "${the_image}"				2>> /var/log/chronicle.log 1>&2
let exitval=$? ; if [[ $exitval -ne 0 ]] ; then exit $exitval ; fi			# ERROR EXIT
# generate the ASTROLOG ANALYSIS:
if [ "${the_event}" == "" ]
then	# no event name, defaults to "Today"
/usr/local/games/astrolog-6.20/astrolog -v -g0 -j0 -7 -d -D -Yt -Zd -I -YQ 32765 -i kilauea.txt		\
								>> "${the_file}"	2>> /var/log/chronicle.log
else	# replace "Today" with "${the_event}"
/usr/local/games/astrolog-6.20/astrolog -v -g0 -j0 -7 -d -D -Yt -Zd -I -YQ 32765 -i kilauea.txt	|	\
sed "s/Today/${the_event}/g"					>> "${the_file}"	2>> /var/log/chronicle.log
fi
let exitval=$? ; if [[ $exitval -ne 0 ]] ; then exit $exitval ; fi			# ERROR EXIT
# generate the file footer:
echo	"</pre>"				>> "${the_file}"	2>> /var/log/chronicle.log
cat	${HOME}/crystalfaeries.net/README.html	>> "${the_file}"	2>> /var/log/chronicle.log
preview						   "${the_file}"	2>> /var/log/chronicle.log
let exitval=$?	# really only the results of the final cat, due to lazy coding :-)
;; # done with arguments ##########################################################################
esac
fi
exit	$exitval	# Pau for now



syntax highlighted by Code2HTML, v. 0.9.1