#!/bin/bash

# NAME
#	getStockHistory -- Downloads historic stock prices for a given company stock.
#
# SYNOPSIS
#	getStockHistory symbol [optional saveLocation]
#	getStockHistory -f file [optional saveLocation]
#
# DESCRIPTION
#	The getSockHistory command attempts to download complete stock history for a given company or
#	for list of companies in a file.
#
# OPTIONS
#	-f	Use a file to have a list of stock symbols to download. Each stock symbol should be on a
#		differnt line in the file.
#	
#
# EXAMPLES
#	The following downloads Apple's stock history.
#		getStockHistory AAPL
#
# BUGS
#
# Written by: Rowland O'Flaherty (rowland.oflaherty@gmail.com)
# Created on: 02/08/10/

function getHistory() {
	stockSymbol=$1
	if [[ -z $stockSymbol ]]
	then
		echo "Invalid stock symbol"
		exit 1
	fi
	stockSymbol=$(echo $stockSymbol | tr '[:lower:]' '[:upper:]')
	saveLocation=$2
	wget -q -O ${stockSymbol}.csv "http://ichart.finance.yahoo.com/table.csv?s=${stockSymbol}&ignore=.csv"
	sed '1s/\ /_/g' ${stockSymbol}.csv > ${stockSymbol}.tmp
	mv ${stockSymbol}.tmp ${stockSymbol}.csv
	mv ${stockSymbol}.csv ${saveLocation}
}

fOpition=false
while getopts :f OPTION
do
	case $OPTION in
		f)
			fOpition=true
		;;
		'?')
			echo "Invalid option $OPTARG"
		;;
	esac
done
if [[ "$fOpition" = "true" ]]
then
	fileLocation=$2
	saveLocation=$3
	saveLocation=${saveLocation:=$HOME/Downloads}
	for aSym in $(cat $fileLocation)
	do
		eval "getHistory $aSym $saveLocation"
	done
else
	saveLocation=$2
	saveLocation=${saveLocation:=$HOME/Downloads}
	eval "getHistory $1 $saveLocation"
fi