#!/usr/bin/env perl
use strict;
use warnings;
use lib '/cs/lallip/lib/perl5/site_perl/5.10.0';
use DBI;

my $dir = '/cs/rcsid/csvdb'; #change this to whatever directory you want to use

my $dbh = DBI->connect("DBI:CSV:f_dir=$dir") or die "Cannot connect: " . DBI->errstr();
$dbh->{csv_eol} = "\012";  
$dbh->{RaiseError} = 1;

$dbh->do("CREATE TABLE students  ( idnum int, name char(30) )           ") unless -e "$dir/students";
$dbh->do("CREATE TABLE homeworks ( idnum int, hwnum int, hwgrade real ) ") unless -e "$dir/homeworks";
$dbh->do("CREATE TABLE icas      ( idnum int, icnum int, icgrade real ) ") unless -e "$dir/icas";

#Create and prepare your SQL statements here.


my $idnum = 0;
while (my $input = <STDIN>) {
   chomp $input;
   last if lc $input eq 'done';

   if ($input =~ /^new (\S+)$/) {
       my $name = $1;
       $idnum++;
       #insert this new student;


   } elsif ($input =~ /^hw (\S+) (\d+) (\d+(?:\.\d+)?)$/) {
       my ($name, $hwnum, $hwgrade) = ($1, $2, $3);
       #insert this new homework grade


   } elsif ($input =~ /^ica (\S+) (\d+) (\d+(?:\.\d+)?)$/) {
       my ($name, $icnum, $icgrade) = ($1, $2, $3);
       #insert this new ica grade

       
   } elsif ($input =~ /^(\S+)$/) {
       my $name = $1;
       #find and display this student's final grade.


   } else {
       warn "Invalid input format '$input'\n";
   }

}
