#!/bin/bash
#                      /usr/local/bin/clockin
# http://crystalfaeries.net/posix/bin/clockin
# celeste crystalfaery 2016-06-14 01:34:50+00:00

#	Clockin for a reading/healing session
#	First and only argument is their name (no spaces allowed)

# User selected parameters:
min_per_oz="15"					# minutes of consultation per Oz. of Ag.
min_price="$(cat ~/.min_price.txt)"		# minimum 'price of silver' in U.$.F.R.N. upon which to peg my U.$.F.R.N. rate.
silver_file=/usr/local/share/ag/kitco.txt		# where does silverd store the KitCo Silver Price?
directory="/home/celeste/documents/ministry/"	# where do we keep our consultation records?

# Price Calculation Code:
PERSON="$(echo ${1} | sed 's/\.statement\.txt$//g')"
price="`cat $silver_file`"
if [[ "$(echo $price | sed 's/\.//')" -lt "$(echo $min_price | sed 's/\.//')" ]]
then
	max_price=$min_price
else
	max_price=$price
fi
rate=$(echo "scale=2; $max_price/$min_per_oz" | bc -l)

nowstring="`now | sed 's/ /./' | sed 's/:/-/' | sed 's/:/-/' | sed 's/:/-/'`"
cat			        $directory/${PERSON}.statement.txt		# show their history
touch			        $directory/$nowstring.${PERSON}.clockin.txt	# initiate this log

balance=0
let time=0
while true
do
	sleep 1m
	let time+=1

	price="`cat $silver_file`"
	if [[ "$(echo $price | sed 's/\.//')" -lt "$(echo $min_price | sed 's/\.//')" ]]
	then
		max_price=$min_price
	else
		max_price=$price
	fi
	rate=$(echo "scale=2; $max_price/$min_per_oz" | bc -l)

	balance=$(echo "scale=2; $balance+$rate" | bc -l)
	echo     "`now` Elapsed Time: $time minutes, Session: $balance" > \
			$directory/$nowstring.${PERSON}.clockin.txt
	echo -ne "`now` Elapsed Time: $time minutes, Session: $balance\r"
done
# If I caught the Ctrl-C interrupt or otherwise waited for input asynchronously,
# Then at the clock-out point I could automagically run a "statement" :-)

