#!/usr/bin/perl

use strict;
use warnings;

my $usage="Usage: $0 [-h] [--csv|--tab] -f <personal_data> -g <general_info> -o <output>\n"; #reads two files and for commandline

die "$usage" if $#ARGV < 5;

my $file1; #declaring file1
my $file2; #declaring file2
my $using_csv = 0; #set csv to 0
my $using_tab = 0; #sets tab to 0
my $output; #declaring output

while(@ARGV) {
  if($ARGV[0] eq '-h') { #exits if -h is used in command line
    print $usage;
    exit;
  }
  elsif($ARGV[0] eq '--tab') { #same but for tabs instead
    $using_tab = 1;
    shift;
  }
  elsif($ARGV[0] eq '--csv') { #else if it will set using_csv to 1, removed and return value
    $using_csv = 1;
    shift;
  }
  elsif($ARGV[0] eq '-f') { #go next if arguement is -f 
    shift;
    if($ARGV[0] eq '') { #go to next if it is equal to a white space
      $ARGV[0] = '-h'; #set arguement to -h in command line
      next;
    }
    else {
      $file1 = $ARGV[0]; #set the arguement to file 1
      shift;
    }
  }
  elsif($ARGV[0] eq '-g') { # if arguement is -g in commandline shift and the check aruements for whitespace then -h to arguement
    shift;
    if($ARGV[0] eq '') {
      $ARGV[0] = '-h';
      next;
    }
    else { #sets argument to file 2
      $file2 = $ARGV[0];
      shift;
    }
  }
  elsif($ARGV[0] eq '-o') { #checks if arguement is equal to -0
    shift;
    if($ARGV[0] eq '') {
      $ARGV[0] = '-h';
      next;
    }
    else { #sets arguement to the output
      $output = $ARGV[0];
      shift;
    }
  }
  else {
    print "Invalid arguments, try again. Proper usage is:\n"; #error message
    print $usage;
    exit;
  }
}

die "Cannot use both --csv and --tab.\n$usage" if($using_tab == 1 && $using_csv == 1); #error message if both csv and tab isnt being used

open(DATA, $file1) or die "Cannot open $file1 for reading: $!.\n";
open(INFO, $file2) or die "Cannot open $file2 for reading: $!.\n";
open(OUT, ">$output") or die "Cannot open $output for writing: $!.\n";

my $delim = ($using_tab == 1) ? "\t" : ","; # task 4 using delimeter to splits using tabs or commas

my %stocks;
my %Prices;
my %Names;
my %Values;

my $line;
my @fields;

while(<DATA>) {
  chomp;
  $line = $_;
  @fields = split /$delim/, $line;
  $stocks{$fields[0]} = $fields[1];
}

while(<INFO>) {
  chomp;
  $line = $_;
  @fields = split /$delim/, $line;
  $Prices{$fields[0]} = $fields[1];
  $Names{$fields[0]} = $fields[2];
}

my $key; #declaration of the key variable

foreach (keys %stocks) {
  $key = $_;
  $Values{$key} = $stocks{$key} * $Prices{$key}; #multiplies the prices
  print OUT "$Names{$key}$delim$key$delim$stocks{$key}$delim$Prices{$key}";
  print OUT "$delim$Values{$key}\n";
}

close DATA; #closes thw file task 8
close INFO;
close OUT;

my $symbol;
print "Enter a symbol to liquidate: ";
$symbol = <stdin>;
chomp($symbol);
$symbol =~ tr/a-z/A-Z/; #convert up to lowercase to uppercase task 7

if($Values{$symbol}) { #
  print "$symbol liquidated for \$$Values{$symbol}.\n";
}
else { #printing out error message because its not a personal data hash
  print "$symbol is not in the personal stock data.\n";
