#!/bin/bash
#                     /usr/lib/cgi-bin/silver
#                       /usr/local/bin/silver
# https://crystalfaeries.net/posix/bin/silver
# celeste:crystalfaery CGI-BIN/SILVER 2020-09-06 01:44:18+00:00

# output the webpage:
/bin/echo 'Content-Type: text/html'
/bin/echo ''
/bin/echo '<html><body><p>'

min_per_oz="15.00"					# minutes of consultation per Oz. of Ag.
#DEBUG# echo "min_per_oz: `echo $min_per_oz`"'<br>'
min_price="$(/bin/cat /home/`whoami`/.min_price.txt)"	# minimum 'price of silver' in U.$.F.R.N. upon which to peg my U.$.F.R.N. rate.
#DEBUG# echo "min_price: `echo $min_price`"'<br>'
silver_file=/usr/share/ag/kitco.txt			# where does silverd store the KitCo Silver Price?
#DEBUG# echo "silver_file: `echo $silver_file`"'<br>'

# the calculation code:
price="`/bin/cat $silver_file`"
#DEBUG# echo "price: `echo $price`"'<br>'
if [[ "$(/bin/echo $price | /bin/sed 's/\.//')" -lt "$(/bin/echo $min_price | /bin/sed 's/\.//')" ]]
then
	#DEBUG# echo -n "min_price: "
	max_price=$min_price
else
	#DEBUG# echo -n "price: "
	max_price=$price
fi
#DEBUG# echo "$max_price"'<br>'
rate=$(/bin/echo "scale=2; $max_price/$min_per_oz" | /usr/bin/bc -l)
#DEBUG# echo "rate: `echo $rate`"'<br>'
date_time="$(/bin/ls -l --time-style=full-iso $silver_file | /bin/sed 's/ /	/g' | cut -f 6-8 | /bin/sed 's/	/ /g ; s/\.[0-9]*//')"
#DEBUG# echo "date_time: `echo $date_time`"'<br>'

/bin/echo "As of $date_time:"'<br>'
/bin/echo 'Kitco New York: Silver Bid Price: U.$.F.R.N.s / Ounce: '"$price"'<br>'
/bin/echo 'requested donation rate: U.$.F.R.N.s / minute: '"$rate"'<br>'
/bin/echo '</p></body></html>'

