#!/bin/bash

# A script that runs as a cron job daily to graphically display
# the current angle fron North of the rising sun.
#
# You can find a link to the current output on my main gophermap
# gopher://tildecow.com
#
# Questions? Comments? Unrelated Niceties?
# [thenumbersixtytwo]connolly[at]gmail[period]com
#

# The raw data is in a table on this website
data=$(lynx --dump https://www.timeanddate.com/sun/@z-us-13676)

# Extract the part we want
data=${data%Sunset Today:*}  # retain the part before the colon
data=${data##*Sunrise Today:}  # retain the part after the last slash

# Begin formatting output
echo "" > /var/gopher/sunrise
today=$(date +%d-%B)
echo -n "$today - Local Sunrise:$data" >> /var/gopher/sunrise

# Place the sun at the appropriate position along the track
deg=($data)
deg=${deg[2]}
deg=${deg::-1}
n=53
printf "%-$((deg-n))s" >> /var/gopher/sunrise
#echo -n "${line// /*}"
echo '   ,    /),' >> /var/gopher/sunrise
printf "%-$((deg-n))s" >> /var/gopher/sunrise
echo '  (( -.((_))  _,)' >> /var/gopher/sunrise
printf "%-$((deg-n))s" >> /var/gopher/sunrise
echo '  ,\`.`_  _`-`,`' >> /var/gopher/sunrise
printf "%-$((deg-n))s" >> /var/gopher/sunrise
echo '  `.> <> <>  (,-' >> /var/gopher/sunrise
printf "%-$((deg-n))s" >> /var/gopher/sunrise
echo ' ,`,    |     `._,)' >> /var/gopher/sunrise
printf "%-$((deg-n))s" >> /var/gopher/sunrise
echo '((  )   |,   (`--`' >> /var/gopher/sunrise
printf "%-$((deg-n))s" >> /var/gopher/sunrise
echo ' ``( ) _--_,-.\' >> /var/gopher/sunrise
printf "%-$((deg-n))s" >> /var/gopher/sunrise
echo '    /,` \( )  ``' >> /var/gopher/sunrise
printf "%-$((deg-n))s" >> /var/gopher/sunrise
echo '   ((    `\' >> /var/gopher/sunrise
printf "%-$((deg-n))s" >> /var/gopher/sunrise
echo '        !' >> /var/gopher/sunrise

# Place the axis for this one-dimensional graph
echo "<N -------+---------------------------------<E>-------------------------------+---- S>" >> /var/gopher/sunrise
echo "          ^                                  ^                                ^" >> /var/gopher/sunrise
echo "   Summer Solstice                        Equinox                      Winter Solstice" >> /var/gopher/sunrise
echo "       55 deg                              90 deg                          123 deg" >> /var/gopher/sunrise
echo "" >> /var/gopher/sunrise

# Closing stuff
echo "                                  (ascii sun courtesy SSt)" >> /var/gopher/sunrise
echo "                          Thanks for visiting gopher://tildecow.com" >> /var/gopher/sunrise

# Grace those poor, backward denizens of the WWW with this greatness. ;-)
cp /var/gopher/sunrise /var/www/html/sunrise.txt

# END SCRIPT