#!/usr/bin/perl
# version 0.01
use strict;
use warnings;
use Getopt::Long;
use Finance::Shares::MySQL;

my $help;
my $start  = '';
my $end    = '';
my $mode   = '';
my $file   = '';
my $user   = 'test';
my $pwd    = 'test';
my $dbase  = 'test';
my $debug  = 2;
my $stocks = '';
my $usage  = <<END;
Usage:
    $0 -h | --help
    $0 [ options ] stocks
    $0 -c <file> | --stocks=<file> [ options ]

The options available are:
  -u <user> |     --user=<user>  User defaults to 'test'
  -p <pwd>  | --password=<pwd>   Password defaults to 'test'
  -d <db>   | --database=<db>    Database defaults to 'test'
  -v <lvl>  |  --verbose=<lvl>   Verbosity level 0, 1 or 2
  -s <date> |    --start=<date>  First date, as 'YYYY-MM-DD'
  -e <date> |      --end=<date>  Last date, as 'YYYY-MM-DD'
  -m <mode> |     --mode=<mode>  online/fetch/cache/offline
  -c <file> |   --stocks=<file>  Source for stock codes
  -f <file> |     --file=<file>  Name of destination CSV file
  -h        |     --help         Show this help

'stocks' are a list of Yahoo stock codes e.g.
    MSFT BA.L 12126.PA

The file option is only useful with a single stock. If omitted
the CSV file is called
    <code>-<start>-<end>.csv

END

GetOptions (
    'help|h'       => \$help,
    'start|s=s'    => \$start,
    'end|e=s'      => \$end,
    'mode|m=s'     => \$mode,
    'file|f=s'     => \$file,
    'user|u=s'     => \$user,
    'password|p=s' => \$pwd,
    'database|d=s' => \$dbase,
    'stocks|c=s'   => \$stocks,
    'verbose|v=s'  => \$debug,
) or $help = 1;
print $usage and exit if $help;
print $usage and exit unless $user and $pwd and $dbase;
$mode = lc $mode;
print $usage and exit unless $mode =~ /offline|cache|fetch|online/ or not $mode;

### Read stocks file
our @stocks;
if ($stocks) {
    open(SYMBOLS, '<', $stocks) or die "Unable to open '$stocks': $!\nStopped";
    while( <SYMBOLS> ) {
	chomp;
	s/#.*//;
	next if /^\s*$/;
	my @line = split /[,\s]+/;
	push @stocks, @line;
    }
    close SYMBOLS;
}
push @stocks, @ARGV;
print $usage and exit unless @stocks;


# Create MySQL object giving access to the data
my $db = new Finance::Shares::MySQL(
    user     => $user,
    password => $pwd,
    database => $dbase,
    verbose  => $debug,
);

# Fetch the data from the internet
foreach my $stock (@stocks) {
    my @data;
    eval {
	@data = $db->fetch(
	    symbol     => $stock,
	    start_date => $start,
	    end_date   => $end,
	    mode       => $mode,
	);
    };
    if ($@) {
	warn "Error with $stock : $@";
    } else {
	warn "No data fetched for $stock\n" and next unless @data;
	warn "Finished $stock from ", $db->start_date(), " to ", $db->end_date(), "\n" if $debug;
    }

    my $filename = $file ? $file : "$stock-$start-$end.csv";
    my $fs;
    if ($filename) {
	open ($fs, '>', $filename) or die "Unable to write to '$filename' : $!\n";
	select $fs;
    }
    foreach my $row (@data) {
	my ($date, $open, $high, $low, $close, $volume) = @$row;
	printf('%s,%6.2f,%6.2f,%6.2f,%6.2f,%d%s',
	$date, $open, $high, $low, $close, $volume, "\n");
    }
    if ($file) {
	close $fs;
	select STDOUT;
    }
}

