#!/usr/bin/env perl
use strict;
use warnings;


if (!@ARGV) {  #Part A

   #obtain filename
   print "Please enter a file path:\n";
   chomp (my $file = <STDIN>);
 
   #open, read, close file
   open my $fh, '<', $file or die "Cannot open $file: $!\n";
   chomp(my @lines = <$fh>);
   close $fh or die "Cannot close $file: $!\n";
 
   @lines == 3 or die "$file contains incorrect number of lines!\n";
   
   #Put three lines in separate vars, for ease of reading
   my ($newfile, $numbers, $dir) = @lines;  
 
   #Make the directory, create the file
   mkdir($dir) or die "Cannot create directory $dir: $!\n";
   $newfile = lcfirst(uc($newfile));
   open my $ofh, '>', "$dir/$newfile" or die "Cannot create file $dir/$newfile: $!\n";
 
   #obtain the two numbers;
   my @nums = split ',', $numbers;
   @nums == 2 or die "$file contained wrong amount of numbers!\n";
 
   #print the math results
   my $old_fh = select $ofh;
 
   print "Sum: "            . ($nums[0] + $nums[1]) . "\n";
   print "Product: "        . ($nums[0] * $nums[1]) . "\n";
   print "Difference: "     . ($nums[0] - $nums[1]) . "\n";
   print $nums[1] != 0 ? 
         "Quotient: "       . ($nums[0] / $nums[1]) . "\n" :
         "Cannot divide by 0!\n";
   print "Exponentiation: " . ($nums[0] ** $nums[1]) . "\n";
 
   close $ofh or die "Cannot close $dir/$newfile: $!\n";
 
   select $old_fh;

} else { # Part B

   my %id_of;
   my %rcs_of;

   for my $name (@ARGV) {
      print "Please enter the Id Number of $name:\n";
      chomp($id_of{$name} = <STDIN>);

      #separate the first and last names
      my ($first, $last) = split ' ', $name;

      #create the RCS Id
      $rcs_of{$name} = lc(substr($last, 0, 5) . substr($first, 0, 1));
      
   }

   #Determine sort method
   print "Please choose a sorting method, either (L)ast name, (F)irst name, (I)d number, or (R)CS Id:\n";
   chomp (my $sort_method = <STDIN>);

   #sort the names
   my @sorted_names;

   if ($sort_method eq 'L') {
      @sorted_names = sort by_lname @ARGV;
   } elsif ($sort_method eq 'F') {
      @sorted_names = sort by_fname @ARGV;
   } elsif ($sort_method eq 'I') {
      @sorted_names = sort by_idnum @ARGV;
   } elsif ($sort_method eq 'R') {
      @sorted_names = sort by_rcsid @ARGV;
   } else {
      die "Error, invalid sort method!\n";
   }

   #Print out the sorted students
   print "All Sorted Students:\n";
   for my $name (@sorted_names) {
      print "$name, $rcs_of{$name}, $id_of{$name}\n";
   }

   sub by_lname {
      #Obtain the last name of each sort term, and compare alphabetically
      my ($first_a, $last_a) = split ' ', $a;
      my ($first_b, $last_b) = split ' ', $b;
      return lc $last_a cmp lc $last_b;
   }
   
   sub by_fname {
      #Obtain the first name of each sort term, and compare alphabetically
      my ($first_a, $last_a) = split ' ', $a;
      my ($first_b, $last_b) = split ' ', $b;
      return lc $first_a cmp lc $first_b;
   }
   
   sub by_idnum {
      #compare the ids of each term, numerically
      return $id_of{$a} <=> $id_of{$b};
   }
   
   sub by_rcsid {
      #compare the rcs_ids of each term, asciibetically
      #no need for case insensitive - we already created the RCSIds to be all lowercase
      return $rcs_of{$a} cmp $rcs_of{$b};
   }

}


