#!/bin/sh

#
# Copyright (c) 2000 Lars Bernhardsson, see README for licence details
#
# sample.barinfo - Used to show date/time and stock quotes on the status bar.
#
# This is an example of how you can write a program to
# start up in the background in your .xsession before
# starting larswm. It will keep sending the current date
# and time and stock quotes to larswm to display on the
# status bar.
#

# Define the time format you want to use.
TFORMAT="%m/%d/%Y %a %H:%M:%S"

# Define a list of stock symbols you want to watch, separated by '+'.
# Make sure you don't add more than can fit on the status bar or the
# status bar display will get further and further "behind".
# If you view more than one stock, you probably want to adjust
# which fields to view (only view a couple).
# 
# This shows VA Linux and Sun.
# STOCKWATCH="LNUX+SUNW"
# This shows Global Crossing.
STOCKWATCH="GBLX"

# Which fields to show - If not defined, show all.
# 1 = Symbol
# 2 = Last trade
# 3 = Last trade at date
# 4 = Last trade at time
# 5 = Change
# 6 = Opened at
# 7 = Day range high
# 8 = Day range low
# 9 = Volume
STOCKFIELDS="1,2,5"

# Point this to where you want the stock info cache.
STOCKCACHE=${HOME}/.larswmstocks

# No more configuration necessary.


# OS specifics.
case `uname -s` in
OpenBSD)
	CMD="ftp -V -r 10 -o -"
	;;
*)
	CMD="wget -q -t 1 -T 10 -O -"
	;;
esac

rm -f ${STOCKCACHE}

if [ "${STOCKFIELDS}" ]
then
	${CMD} 'http://quote.yahoo.com/d/quotes.csv?s='${STOCKWATCH}'&f=sl1d1t1c1ohgv&e=.csv' | cut -d, -f${STOCKFIELDS} > ${STOCKCACHE}
else
	${CMD} 'http://quote.yahoo.com/d/quotes.csv?s='${STOCKWATCH}'&f=sl1d1t1c1ohgv&e=.csv' > ${STOCKCACHE}
fi

while true
do
	# Get stock updates every 5 minutes.
	case `date '+%M%S'` in
	0000|0500|1000|1500|2000|2500|3000|3500|4000|4500|5000|5500)
		if [ "${STOCKFIELDS}" ]
		then
			${CMD} 'http://quote.yahoo.com/d/quotes.csv?s='${STOCKWATCH}'&f=sl1d1t1c1ohgv&e=.csv' | cut -d, -f${STOCKFIELDS} > ${STOCKCACHE}
		else
			${CMD} 'http://quote.yahoo.com/d/quotes.csv?s='${STOCKWATCH}'&f=sl1d1t1c1ohgv&e=.csv' > ${STOCKCACHE}
		fi
		;;
	esac

	MSG=`date "+${TFORMAT}"`

	if [ -s ${STOCKCACHE} ]
	then
		MSG="`cat ${STOCKCACHE}`  [$MSG]"
	fi

	larsremote message "$MSG"

	sleep 1
done
