#!usr/bin/local/perl -w

# NOTE: students are not required to use strict
use strict;

my $source = "data.txt"; #Sets name for file to be read in.
my $target1 = "output.txt"; #Sets output file name.

&printUsage();  #Make first call to subroutine to print usage.

print "\n>>> ";  #Prints the loop prompt before first iteration.

while (<STDIN>)  # Main loop.  Takes input and assigns it to the 
                 # anonymous var $_.  Runs until user requests "exit".
{
  my $in = $_;   # Assign loop variable $in to value of anon. var.
                 # $in has scope strictly limited to within this loop.
  chomp($in);

  if ($in eq "show file")
	# open file for reading.
    {
        open(FILE, $source);
        while (<FILE>)  #Loop outputs file line by line.
        {
            print $_;
        }
        close(FILE);    #Immediately close file.
    }
    
  elsif ($in eq "show music")
    # open file for reading, filter music
    {
        open(FILE, $source);
        while (<FILE>)
        {
            if ($_ !~ m/VAN GULIK/)  #Assign to $_ when NOT match.
            {
                print $_;
            }
        }
        close(FILE);
    }
    
  elsif ($in eq "show mystery")
    # open file for reading, filter mystery
    {
        open(FILE, $source);
        while (<FILE>)
        {
            if ($_ =~ m/VAN GULIK/)  #Assign to $_ when does match.
            {
                print $_;
            }
        }
        close(FILE);
    }
    
  elsif($in eq "show remainder")
    # open file for reading, filter other
    {
        open(FILE, $source);
        while (<FILE>)
        {
            my @line = split(/::/, $_); #Remove "::" separators from input.
            if ($line[-1] < 1992)  #Test for remainder date.
            {
                print "$line[0]: $line[1], $line[-1]";
            }
        }
        close(FILE);
    }
    
  elsif($in eq "create verdi")
    # write to file
    {
        open(FILE, $source); 
        open(TARGET, ">$target1"); #Open output file with 
                    # $target1 value. Uses write-only-overwrite mode.
        while (<FILE>)
        {
            my @line = split(/::/, $_);
            if ($line[0] eq "VERDI")
            {
                print TARGET "$line[0]\t$line[1]\t$line[-1]";
                    #Prints tab separated output to file.
            }
        }
        close(TARGET);
        close(FILE);
        print "created file $target1\n";
    }
    
  elsif($in eq "add rossini")
    # append file
    {
        open(FILE, $source);
        open(TARGET, ">>$target1"); #Uses write-only-append mode.
        while (<FILE>)
        {
            my @line = split(/::/, $_);
            if ($line[0] eq "ROSSINI")
            {
                print TARGET "$line[0]\t$line[1]\t$line[-1]";
            }
        }
        close(TARGET);
        close(FILE);
        print "appended file $target1\n";
    }
    
  elsif ($in eq "usage")
    # print usage
    {
        &printUsage();   #Call for subroutine that prints usage.
    }
    
  elsif ($in eq "exit")
    # quit program
    {
        print "Bye now!\n\n";
        exit(0);
    }
    
  else
    # fallback error handling for bad input.
    {
        print "Don't know what that means.\n";
    }
  print "\n>>> ";
}  #End of while user input loop.


print "done\n";


sub printUsage()  #Code for subroutine that prints usage. No arguments.
{
  print "AVAILABLE COMMANDS:\n\n" .
      "show file\nshow music\nshow mystery\nshow remainder\n\n" .
      "create verdi\nadd rossini\n\n" .
      "usage\nexit\n\n";
}

