#!/usr/bin/perl

##!/usr/local/bin/perl
#############################################################################
# getsub.pl   (GET SUBMITTED x)
# Chris Cera, cera@drexel.edu
# Created :  1/31/2001
# Modified: everyday Last: 10/6/2001
# Version: 2.0
#############################################################################
# FEATURES
#
# Works on M$ Windows with the help of cygwin from http://www.cygwin.com
# Postprocessor flattens any combination of zip, tar, gz, uu and copies all
#     source code into a special source directory so it can be easily submitted
#     to moss (plagarism detection system offered at Berkeley).
# Postprocessor's main purpose is for submission of source code to a plagiarism
#     detection system.  Check out http://www.cs.berkeley.edu/~aiken/moss.html
# doesn't print password to tty for verbose mode
# intelligently and safely removes the annoying DOS ^M
# Allows you to use a database, see below for more information on the database.
#############################################################################
# tested on WebCT version 3.1.3, used to download WebCT assignments and quizzes
# you must run this program for each assignment, get its ID from the url first
# it will create a directory tree for each person's submission, if it exists
# you must have wget on your system in this version, cURL or lynx could work
#############################################################################
# WISHLIST
#
# option to create unames.txt file
#
# have getsub require the moss.pl file, and run it!!!!!! YES!!
# a real license
#
# ADD DIRNAME FOR THE DIRECTORY, OR RECOGNITION SO THEN 9982392 isn't dirname
#
# automatically change modification dates to match submitted on webct filesys
# MOD_DATE THING
#############################################################################
# DATABASE FILE FORMAT (This header should appear exactly)   --- **OPTIONAL**
#
# USERID, Last Name, First Name, Lec Section, Lab Section, E-mail Address
#
# If you elect to use the database feature, it will sort all submissions by
# section, and pass the email address along to other programs which may want
# to email the recipient of the submission.
#############################################################################


#############################################################################
# DECLARE MODULES AND SUBROUTINES
#############################################################################

# use: pragma's
use strict 'vars';     # i [used to] hate this option
use strict 'refs';
use strict 'subs';

# use: this can be considered like a global declaration
# use Term::ReadKey; 
use File::Copy;
use Getopt::Std;
use vars qw($opt_u $opt_p $opt_c $opt_b $opt_a $opt_o $opt_e $opt_v $opt_m $opt_q $opt_h $opt_f $opt_d $opt_s $opt_z);

sub main;
sub getAssignments;
sub ppAssignments;
sub getQuizzes;
sub getPassword;
sub printUsage;
sub exiterr;
sub mysystem;
sub myprint;
sub testandcreate;
sub removeDosChars;
sub myrmdir;
sub uniquify;
sub getUnameList;
sub strDropBox2idDropBox;

# only global variables, until I figure out how to nicely export them
my $VERBOSE  = "0";
my $PASSWORD = "0";

my $QUIZ_DEBUG = 0;



#############################################################################
# START CODE
#############################################################################

# globals suck, force lexical scoping, yes!
main();

#############################################################################
# END CODE
#############################################################################





#############################################################################
# START MAIN FUNCTION
#############################################################################

sub main {

    #my @current_classes = ("DES101", "CS171", "CS172", "CS350", "CS171_Summer2001", "CS172_Summer2001", "CS164_Fall2001", "CS171_Fall2001", "CS172_Fall2001");
    
    ##########################################################################
    # CONSTANTS
    ##########################################################################
    
    my $PROGNAME         = "getsub";
    my $VERSION          = 2.0;
    my $DOMAIN           = "webct.drexel.edu";
    my $MAX_PASS_LEN     = 32; # please seek counseling if its longer than this
    my $MAX_LAB_ATTEMPTS = 3;
    
    # lower b/c we make the handles caps too
    my $strSrcDir    = "src";
    
    #on insomnio
    #my $WGET = "/usr/bin/wget";

    my $WGET = "wget";
    
    # ENUMERATION OF MODES
    my %modes;
    $modes{'assignments'} = 0;
    $modes{'quizzes'}     = 1;
    $modes{'num_modes'}   = 2;
    
    my $MODE = $modes{'num_modes'};   # sets this to a bunk value
    
    ##########################################################################
    # OPTS SECTION - check command line usage
    ##########################################################################

    if (@ARGV == 0) {$NOARGS == 1;}

    getopts ('u:p:c:b:f:d:z:aoevmqhs');
    
    # sample, this is a hidden option
    if ($opt_s == 1) {
        $opt_u = "CSDev4";
        $opt_c = "CS172_Summer2001";
        $opt_b = "993576040";
        $opt_a = 1;
        $opt_e = 1;
    }
    
    # this is the uniquely assigned id number for each assignment
    #$idCourse  = "CS999";  # cs999 works too for some magical reason
    #$idDropBox = "123456789";
    
    # string options
    my $u          = $opt_u;
       $PASSWORD   = $opt_p;
    my $idCourse   = $opt_c;
    my $idDropBox  = $opt_b;
    my $strDropBox = $opt_d;
    
    # booleans are capitalized, I just like it that way
    my $PRINT_VERSION   = $opt_v;
    my $PRINT_USAGE     = $opt_h;
    my $PP_ASSIGN       = $opt_o;
       $VERBOSE         = $opt_e;
    my $GET_ASSIGN      = $opt_a;
    my $ON_MCS          = $opt_m;
    my $GET_QUIZZES     = $opt_q;
    my $UNAMES_FILE     = $opt_f;
    my $DB_FILE         = $opt_z;

    # reflect change in verbosity
    if (! $VERBOSE >= 1) {
        # doesn't even print errors
        # $WGET = "$WGET --quiet"

        # still prints errors
        $WGET = "$WGET -nv" 
    }

    # print the options if verbose is set
    myprint "VERBOSE MODE\n";
    myprint "opt_u = $opt_u\n";
    myprint "opt_p = $opt_p\n";
    myprint "opt_c = $opt_c\n";
    myprint "opt_b = $opt_b\n";
    myprint "opt_a = $opt_a\n";
    myprint "opt_o = $opt_o\n";
    myprint "opt_e = $opt_e\n";
    myprint "opt_v = $opt_v\n";
    myprint "opt_m = $opt_m\n";
    myprint "opt_q = $opt_q\n";
    myprint "opt_f = $opt_f\n";
    myprint "opt_d = $opt_d\n";
    myprint "opt_z = $opt_z\n";

    ##########################################################################
    # DONE opts section - DO NOT use opts below this line
    ##########################################################################
    
    my $database; 
    
    ##########################################################################
    # DO some validation of passed in arguments, and APPLY certain options
    ##########################################################################
    
    # seeking information only
    if    ($PRINT_VERSION == 1) {
        print STDOUT "$PROGNAME v$VERSION\n";
#        print STDOUT "PRINT LICENSE HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
        exit (0);
    }
    elsif ($PRINT_USAGE == 1 || $NOARGS) {
        printUsage($PROGNAME, $VERSION);
        exit (0);
    }

#    if ($strDropBox eq "") {
#        exiterr ($PROGNAME, "must use -n option now\n");
#    }
    
    # determine mode
    if    ( $GET_ASSIGN == 1 || $PP_ASSIGN == 1 ) {
        if ($GET_QUIZZES == 1) {
            exiterr ($PROGNAME, 
                     "must select only one mode: only [-ao] or [-l], use -h for usage\n");
        }
        $MODE = $modes{'assignments'};
    }
    elsif ($GET_QUIZZES == 1) {
        $MODE = $modes{'quizzes'};
    }
    else {
        exiterr ($PROGNAME, "must select a mode using [-qao], use -h for usage\n");
    }

    if ($DB_FILE ne "") {
        if (! -f $DB_FILE) {
            #exiterr ($PROGNAME, "$DB_FILE does not exist.\n");
            exiterr ("The database file you specified ($DB_FILE), does not exist\n");
        }
        else {
            # now grab the database/ file if specified
            myprint "grabbing the database file $DB_FILE\n";
            open ('dbfile', "<$DB_FILE");

            my @liners;
            for (my $i = 0; $liners[$i] = <dbfile>; $i++){};
            $database = "@liners";
        }
    }
    else {
        if ($MODE == $modes{'quizzes'}) {
            print STDERR "You have not provided a database file, the grading UI will not work properly\nwithout this file...\n";
        }
    }
    
    if ( ( $u eq "" && $PP_ASSIGN == 0) ||
         ( $idCourse  eq "" && $PP_ASSIGN == 0) ||
         ($idDropBox eq "" && $strDropBox eq "") 
        ) {
    
        exiterr ($PROGNAME, "missing -u, -c, or [-bd]\n");
    }

    ###########################################################################
    # get PASSWORD, do this now b/c we might need to query webct for some info
    ###########################################################################

    # PROMPT FOR PASSWORD IF NOT GIVEN
    my $queryDomain = ($GET_ASSIGN == 1 || $GET_QUIZZES == 1);

    # this check: it won't prompt for password if just -h option is used, etc
    if ($PASSWORD eq "" && $queryDomain) {
        $PASSWORD = getPassword ($MAX_PASS_LEN);
    }

    ###########################################################################
    # get list of dropbox id and corresponding string, might do web query 
    ###########################################################################

    # check if used the strDropBox
    if ($idDropBox eq "" && $strDropBox ne "") {
        $idDropBox = &strDropBox2idDropBox ($PROGNAME, $WGET, $DOMAIN, $u, $PASSWORD, $idCourse, $strDropBox, $MODE, %modes);
    }

    ###########################################################################
    # get list of usernames -- $strUnameList, might do web query
    ###########################################################################
    
    my $strUnameList = &getUnameList ($PROGNAME, $WGET, $DOMAIN, $u, $PASSWORD, $idCourse, $idDropBox, $MODE, $queryDomain, $UNAMES_FILE, %modes);

    if ($strUnameList eq "") {
        if ($PP_ASSIGN == 1 && $GET_ASSIGN == 0 && $GET_QUIZZES == 0) {
        }
        else { 
            exiterr ($PROGNAME, "The unames list that I tried to generate is empty, please use\nthe -f option and specify a '\\n' delimited list of usernames.  It is also\npossible that you did not correctly enter your password.\n");
        }
    }
    else {
        myprint ("strUnameList = ->$strUnameList<-\n", 2);
    }

    ##########################################################################
    # REAL WORK
    #
    #     input is simply a single column file of usernames
    ##########################################################################
    
    # FRONT PAGE 
    # http://webct.drexel.edu/SCRIPT/$COURSENAME/scripts/serve_home.pl
    
    if    ($MODE == $modes{'assignments'}) {
        if ($GET_ASSIGN == 1) {
            myprint "START getAssignments() ...\n";
            getAssignments ($PROGNAME, $WGET, $DOMAIN, $u, $PASSWORD, $idCourse, $idDropBox, $strUnameList, $database, $strDropBox);
            myprint "DONE getAssignments()\n";
        }
        if ($PP_ASSIGN == 1) {
           myprint "START ppAssignments() [pp=post-processing] ...\n";
            ppAssignments ($PROGNAME, $idCourse, $idDropBox, $strSrcDir, $strUnameList, $database, $strDropBox);
           myprint "DONE ppAssignments() [pp=post-processing]\n";
        }
    }
    elsif ($MODE == $modes{'quizzes'}) {
        if ($GET_QUIZZES == 1) {
           myprint "START getQuizzes ...\n";
            getQuizzes ($PROGNAME, $WGET, $DOMAIN, $u, $PASSWORD, $idCourse, $idDropBox, $strUnameList, $MAX_LAB_ATTEMPTS, $database, $strDropBox);
           myprint "DONE getQuizzes\n";
        }
    }
    else {
        exiterr ($PROGNAME, "unknown mode: this error should be impossible");
    }
}
    
#############################################################################
# END MAIN FUNCTION
#############################################################################

sub getAssignments {

    my ($prog, $wget, $domain, $user, $pass, $idCourse, $idDropBox, $strUnameList, $database, $strDropBox) = @_;

    myprint "prog = $prog\n";
    myprint "wget = $wget\n";
    myprint "domain = $domain\n";
    myprint "user = $user\n";
    myprint "pass = $pass\n";
    myprint "idCourse = $idCourse\n";
    myprint "idDropBox = $idDropBox\n";
    myprint "strUnameList = $strUnameList\n";
    myprint "database = $database\n";


    if (-d "$idDropBox") {
        &myrmdir("$idDropBox");
    }
    
    testandcreate ("$idDropBox");

    my @database_array = split ('\n', $database);

    my $urlSubmitList = "http://$domain/SCRIPT/$idCourse/scripts/student/dropbox_files_view.pl?MENU+_side_nav++$idDropBox++";

    my @unames = split (' ', $strUnameList);

    for (my $i = 0; $i < @unames; $i++) {

        # blasts newline
        my $uname = $unames[$i];
    
        myprint "checking -->$uname<-- .... \n";
    
        # the href we are grabbing is given by a relative path
        # do a source dump of that address, but first authenticate
    
        #chomp ($data = `$LYNXSOURCE -auth=$u:$PASSWORD $urlSubmitList$uname | grep HREF`);
        #chomp (my $data = `$wget --http-user=$user --http-passwd=$pass \"$urlSubmitList$uname\" -O - | grep HREF`);

        # weird
        #chomp (my $data = mysystem ("$wget --http-user=$user --http-passwd=$pass \"$urlSubmitList$uname\" -O - | grep HREF"));

        my $htmlfile = mysystem ("$wget --http-user=$user --http-passwd=$pass \"$urlSubmitList$uname\" -O -");

        #my @htmllines = split ('\n', $htmlfile);

        # this worked, somewhere in this file
        # @newfiles = grep !/^\./, map "$dir/$_", readdir NEW;

        # submitted files are the only HREFs in the file
        #my @data = grep /HREF/, @htmllines;

        my @data = grep /HREF/, split ('\n', $htmlfile);

        #print "data = $data\n";

        # # TEMPORARY TEST
        # my $delme =  "echo : you can't code";
        # my $output = `$delme`;
        # print $output;
        # exit (1);

    
        # output will look like
        # <A HREF="/SCRIPT/CS999/scripts/student/dropbox_files_view.pl?FILE_VIEW+_side_nav++123456789+asmt2.tar.gz+abrader" TARGET="right" onClick="doSelect('asmt2.tar.gz')"> asmt2.tar.gz</A>
        #      ^^^^^^^^^^^^
        #
        #    which has form
        #    <a href="....?FILE_VIEW+_side_nav++[$idDropBox]++[FILE]++[$uname]</a>
    
        #    ... so we'll pick up everything after first '>' and before next '<'
    
        for (my $ind = 0; $ind < @data; $ind++) {

            myprint ("dataline = $data[$ind]\n", 2);

            my $pos = 0;
            my $switch = 1;
            while ( ($pos = index ($data[$ind],"HREF",$pos) ) > -1) {
        
                # check if first time in loop for this user
                if ($switch == 1) {
    
                    # create directory if DNE
                    testandcreate ("$idDropBox/$uname");
    
                    $switch = 0;
                }
        
                my $end_bracket = index ($data[$ind], ">", $pos);
                my $stt_bracket = index ($data[$ind], "<", $end_bracket);
        
                # +2 is b/c there is always a space after the end bracket.
                # +1 at the end just works, and didn't care enough to find out why :D
                my $len = ($stt_bracket - 1) - ($end_bracket + 2) + 1;
        
                #myprint "end=$end_bracket\nstt=$stt_bracket\npos=$pos\n";
                #myprint "len=$len\n";
        
                # rip out the filename from the url
                my $filename = substr ($data[$ind], $end_bracket+2, $len);
    
                # now grab the filenames extension.
                my @exten = split ('\.', $filename);
                my $extnum = (scalar @exten) - 1; # 1st last ext
                my $ex = $exten [$extnum];;
        
                myprint "    has submitted -->$filename<--\n";
        
                # this method is flawed
                # my $urlSubmission = "$domain/SCRIPT/$idCourse/scripts/student/dropbox_files_view.pl?FILE_VIEW+_side_nav++$idDropBox+$filename+$uname";
                  # was getting:
                    # Unknown format.:application/unknown
                    # <BR>WebCT cannot display the file:
                    # <BR>To view the file, please download it to your computer.
                  # mozilla says that the mime type is application/download
                  # which should really be text or whatever.  I don't know how it
                  # is generated and why, but it happens to files of all types
                  # *.exe, anything
                # switch FILE_VIEW to FILE_DOWNLOAD and it works :)
                # this also got rid of the evil <xmp> :) ... i think
                my $urlSubmission = "$domain/SCRIPT/$idCourse/scripts/student/dropbox_files_view.pl?FILE_DOWNLOAD+_side_nav++$idDropBox+$filename+$uname";
    
                # word docs blow up on the *.xmp thing
                if ($ex eq "doc") {
                    #print STDERR "GOT WORD DOC\n";
                    #mysystem ("$LYNXSOURCE -auth=$u:$PASSWORD $urlSubmission > $idDropBox/$uname/$filename");
                    #mysystem ("$wget --http-user=$user --http-passwd=$pass \"$urlSubmission\" -O - > $idDropBox/$uname/$filename");

                    #mysystem ("$wget --http-user=$user --http-passwd=$pass \"$urlSubmission\" -O $idDropBox/$uname/$filename");

                    my $tmp = removeDosChars (mysystem ("$wget --http-user=$user --http-passwd=$pass \"$urlSubmission\" -O -"));

                    open ("fdOne", "> $idDropBox/$uname/$filename");
                    #print STDOUT "*************************** ->$tmp";
                    print fdOne "$tmp";
                    close (fdOne);
                }
                else {
                    #print STDERR "NOT WORD DOC\n";
    
                    # does nothing
                    #mysystem ("$LYNXSOURCE -auth=$u:$PASSWORD $urlSubmission > $idDropBox/$uname/$filename");
    
                    # this way kills the whole line
                    #mysystem ("$LYNXSOURCE -auth=$u:$PASSWORD $urlSubmission | egrep -v \"<xmp>|</xmp>\"  > $idDropBox/$uname/$filename");
    
    
                    # should get only expression
                    #mysystem ("$LYNXSOURCE -auth=$u:$PASSWORD $urlSubmission | sed 's/<\/*xmp>//g' > $idDropBox/$uname/$filename");
                    # mysystem ("$LYNXSOURCE -auth=$u:$PASSWORD $urlSubmission | sed 's/<\\/*xmp>//g' > $idDropBox/$uname/$filename");
                    # myprint "HERE WE GO**************************************8\n";
                    #mysystem ("$wget --http-user=$user --http-passwd=$pass \"$urlSubmission\" -O - | sed 's/<\\/*xmp>//g' > $idDropBox/$uname/$filename");

                    my $tmp = removeDosChars (mysystem ("$wget --http-user=$user --http-passwd=$pass \"$urlSubmission\" -O -"));

                    open ("fdTwo", "> $idDropBox/$uname/$filename");
                    #print STDOUT "*************************** ->$tmp";
                    print fdTwo "$tmp";
                    close (fdTwo);
                }
        
                # might want to find a way use threads that way these calls could be
                # run in parallel.  That could be detrimental to webct though.  It
                # would be an interesting test.
        
                $pos = $end_bracket;
                $filename = "";
            }
            # if ($i >= 2) { exit (1); }
        }
    }
}

#############################################################################
# post processing
#############################################################################
# flatten dirs, put all files in one directory
# handles most types
#############################################################################
# n^2 ????
#
# yes its n^2, sorry.  I'll make this more efficient eventually.  That doesn't
# mean the whole program sucks, it just means it may take a little bit longer.
# It does make the code much simpler when you separate these two components.
# You can run the post-processor w/o if you've already downloaded everything 
# without ever stepping into a single line of the downloading code.  In this
# case: it runs in O(n) and the code is cleaner.
#############################################################################

sub ppAssignments {

    my ($prog, $idCourse, $idDropBox, $strSrcDir, $strUnameList, $database, $strDropBox) = @_;

    myprint "start post-processing, assumes dir structure is correct\n";

    open ("input", "<$strUnameList");

    #if (-e $strUnameList) {
    #    myprint "$prog will read in your list of usernames ...\n";
    #}
    #else {
    #    exiterr ($prog, "$strUnameList is NOT FOUND\n");
    #}

    my @database_array = split ('\n', $database);

    testandcreate ("$idDropBox.$strSrcDir/");

    my $switch = 1;
    my @allfiles;

    opendir UNAMES, "$idDropBox" or die "weird error";
    my @allunames = map "$_", readdir UNAMES;

    # grabs all of the directories which are already in the boxid directory
    BIG: while (my $uname = pop @allunames) {
    
        # check if it exists
        if (! -e "$idDropBox/$uname/") {
            next;
        }

        if ($uname eq "." || $uname eq "..") {
            next;
        }

        testandcreate ("$idDropBox.$strSrcDir/$uname");
    
        # the grep gets rid of . and ..
        
        opendir DROPBOX, "$idDropBox/$uname/" or die "weird error";

        # should move this declaration outwards???
        @allfiles = map "$idDropBox/$uname/$_", readdir DROPBOX;
        #@allfiles = grep !/^$idDropBox\/$uname/\./, map "$idDropBox/$uname/$_", readdir DROPBOX;
    
        if (@allfiles == 0) {
            next;
        }


        # allfiles is not prepended with absolute dir information
        SMALL: while (my $e = pop @allfiles) {

            myprint "pp:    $e\n";
            myprint "allfiles = @allfiles\n";

            #swap
            #$e is filename
            #$f is qualified filename

            my @temp;
            @temp  = split ('/', $e); # just to get arg count

            my $dir;
            if (! -d "$e") {
                $dir = "$temp[0]";
                for (my $i = 1; $i <= (scalar @temp) - 2; $i++) {
                    $dir = "$dir/$temp[$i]";
                }
            }
            else {
                $dir = $e;
            }

            myprint "dir: $dir\n"; 

            my $f = $e;
            $e = $temp [ (scalar @temp) - 1];

            if ($e eq "."  ||  $e eq "..") {
                next;
            }

            my $last_ext;
            if    (-f $f) {
                # file
                ###########
                #determine type
                #if its an encoding, unencode and put back for later
                #else its a source textfile, finish it


                # this depends on file extensions
                my @ext;
                @ext = split ('\.', $e);

                my $last_ext_num = (scalar @ext) - 1; # 1st last ext
                $last_ext = $ext [$last_ext_num];;

                # create same as $f, filename w/o last extension
                my $newguy = "$ext[0]";
                for (my $i = 1; $i <= (scalar @ext) - 2; $i++) {
                    $newguy = "$newguy.$ext[$i]";
                }

                myprint "eee:  $e\n";
                myprint "new:  $newguy\n";

                ##########################
                # begin file type handling
                ##########################

                myprint ("It is a *.$last_ext file\n");

                ## UUENCODE
                if    ( $last_ext eq "uue" || 
                        $last_ext eq "UUE" ||
                        $last_ext eq "uu"  ||
                        $last_ext eq "UU"      ) {

                    mysystem ("uudecode -o $dir/$newguy $f");
                    #mysystem ("rm $f");
                    unlink $f;
                    # prepend at beginning
                    unshift @allfiles, "$dir/$newguy";
                    next SMALL;
                }

                ## gzip or zcat
                elsif ($last_ext eq "gz" || 
                       $last_ext eq "GZ" ||
                       $last_ext eq "z"  ||
                       $last_ext eq "Z") {
                    mysystem ("gunzip $f");
                    # gunzip deletes it for you by default

                    # prepend at beginning
                    unshift @allfiles, "$dir/$newguy";
                    next SMALL;
                }

                ## weird extensions that are often used with tar.gz tar.Z files
                elsif ($last_ext eq "taz" || 
                       $last_ext eq "TAZ" ||
                       $last_ext eq "tgz" ||
                       $last_ext eq "TGZ") {
                    mysystem ("gunzip $f");
                    # gunzip deletes it for you by default

                    # prepend at beginning
                    unshift @allfiles, "$dir/$newguy";
                    next SMALL;
                }

                ## WINZIP
                elsif ($last_ext eq "zip" || $last_ext eq "ZIP") {
                    # mysystem ("cd $dir && unzip $e");
                    mysystem ("unzip $e -d $dir");
                    #mysystem ("rm $f");
                    unlink $f;
                    redo BIG;
                }

                ## TAPE ARCHIVE
                elsif ($last_ext eq "tar" || $last_ext eq "TAR") {
                    #mysystem ("cd $dir && tar xvf $e");
                    mysystem ("tar xvf $e -C $dir");
                    #mysystem ("rm $f");
                    unlink $f;
                    redo BIG;
                }

                elsif (  $last_ext eq "c"   || 
                         $last_ext eq "C"   ||
                         $last_ext eq "cpp" ||
                         $last_ext eq "CPP" ||
                         $last_ext eq "cc"  ||
                         $last_ext eq "CC"  ||
                         $last_ext eq "cxx" ||
                         $last_ext eq "CXX" ||
                         $last_ext eq "h"   ||
                         $last_ext eq "H"      ) {

                    myprint "$f: $last_ext is a source file, ADDING\n";
                    copy ("$f", "$idDropBox.$strSrcDir/$uname");
                    #mysystem ("g++ -E $f > $idDropBox.$strSrcDir/$uname/$e");
                }

                ## ELSE
                else {
                    # must not be encoded or source
                    #   its probably a doc, pdf
                    myprint "$f: $last_ext isn't encoded, IGNORING\n";
                }
            }
            elsif (-d $f) {
                # directory
                ###########
                #append it into @allfiles, and continue looping

                opendir NEW, "$f" or die "can't open $f";
                my @newfiles;
                @newfiles = grep !/^\./, map "$dir/$_", readdir NEW;

                myprint "checking dir $f:  appending @newfiles";

                push @allfiles, @newfiles;
            }
            else {
                exiterr ($prog, "$f not a file or directory?\n");
            }
        }
        closedir DROPBOX;
    }
    close ("input");
    #print STDERR "EXECUTE: moss -d -l cc -c "Your comment here" */*.[Cc][Xx][Xx] */*.[Hh] */*.[Cc] */*.[Cc][Pp][Pp] */*.[Cc][Cc]\n";

    #print STDERR "EXECUTE: moss -d -l cc -c "Your comment here" $idDropBox.$strSrcDir/*/*.[Cc][Xx][Xx] $idDropBox.$strSrcDir/*/*.[Hh] $idDropBox.$strSrcDir/*/*.[Cc] $idDropBox.$strSrcDir/*/*.[Cc][Pp][Pp] $idDropBox.$strSrcDir/*/*.[Cc][Cc]\n";
}


#############################################################################
# LABS SECTION
#############################################################################

sub getQuizzes {

    #check out gorski for that b/c he/she had submitted the thing twice.
    #attempt 1
    # http://webct.drexel.edu/SCRIPT/CS172_Summer2001/scripts/designer/serve_quiz.pl?ACTION=SUBMISSIONS&SUB_ACTION=ATTEMPT&ID=993752701&STUDENT=a.gorski&ATTEMPT=1&PAGE=0
    #attempt 2
    # http://webct.drexel.edu/SCRIPT/CS172_Summer2001/scripts/designer/serve_quiz.pl?ACTION=SUBMISSIONS&SUB_ACTION=ATTEMPT&ID=993752701&STUDENT=a.gorski&ATTEMPT=0&PAGE=0
    # this will get the listing of students, but we'll use unames.txt
    #$urlSubmitList = "http://$DOMAIN/SCRIPT/$idCourse/designer/serve_quiz.pl?ACTION=SUBMISSIONS&SUB_ACTION=LISTING&ID=$idDropBox";

    #getQuizzes ($PROGNAME, $WGET, $DOMAIN, $u, $PASSWORD, $idCourse, $idDropBox, $strUnameList, $MAX_LAB_ATTEMPTS);

    my ($prog, $wget, $domain, $user, $pass, $idCourse, $idDropBox, $strUnameList, $max, $database, $strDropBox) = @_;

    myprint "prog = $prog\n";
    myprint "wget = $wget\n";
    myprint "domain = $domain\n";
    myprint "user = $user\n";
    myprint "pass = $pass\n";
    myprint "idCourse = $idCourse\n";
    myprint "idDropBox = $idDropBox\n";
    myprint "strUnameList = $strUnameList\n";
    myprint "max = $max\n";
    myprint "database = $database\n";
    myprint "strDropBox = $strDropBox\n";

    if ($strDropBox eq "") {$strDropBox = $idDropBox};

    my @database_array = split ('\n', $database);

    if (-d "$idDropBox") {
        &myrmdir("$idDropBox");
    }

    testandcreate("$idDropBox");

    # make a little README file
    open ("README", ">$idDropBox/TA_README.txt");
    print README "The directory entitled \"unknown_section\" is for students recently added to the database, or students who have some other kind of problems.  Please check this directory for any of your students.  Also, please send email to webct\@mcs.drexel.edu if you have any problems.\n";
    close ("README");

    open ("index_sheet", ">$idDropBox/index.html");

    print index_sheet "<html><head><title>MCS Submission Grader for $idDropBox</title></head><body bgcolor=\"#FFFFFF\">\n";

    print index_sheet "<table><tr><td>idDropBox:<td><b>$idDropBox</b></td></tr></table><br><hr>\n";
    print index_sheet "Some students may have submitted multiple times, please grade only the assignment which you believe is the one that the student intended as the submission.  You may grade the same assignment multiple times, and only the last grade you enter for a particular assignment will actually count.  The only problem is that since grades are emailed, the student will receive several emails if you grade the assignment multiple times.  If you don't see any sections below, other than the unknown section, then you did not include a database file when you ran $prog.<br><hr>\n";

    my $urlSubmitList = "http://$domain/SCRIPT/$idCourse/scripts/designer/serve_quiz.pl?ACTION=SUBMISSIONS&SUB_ACTION=ATTEMPT&ID=$idDropBox&STUDENT=";
    my $urlSubmitListMid  = "&ATTEMPT=";
    my $urlSubmitListTail = "&PAGE=0";

    print index_sheet "<table>\n";
    print index_sheet "<tr><td>Section</td></tr>\n";

    my @unames = split (' ', $strUnameList);

    # create all section directories and beginning html content
    # get a sorted list of unique lab names
    my @lab_array;
    for (my $i = 0; $i < @database_array; $i++) {
        my @delme = split (',', $database_array[$i]);

        my $found = 0;

        for (my $j = 0; $j < @lab_array; $j++) {
            if ($delme[4] == $lab_array[$j]) {
                $found = 1;
                last;
            }
        }
        if ($found != 1) {
            $delme[4] =~ s/ //g;
            if ($delme[4] ne "LabSection" && $delme[4] ne "") {
                #print STDERR "ADDING $delme[4] b/c not in @lab_array\n";
                push (@lab_array, $delme[4]);
            }
        }
    }
    push (@lab_array, "unknown_section");
    sort @lab_array;
    for (my $i = 0; $i < @lab_array; $i++) {

        testandcreate ("$idDropBox/$lab_array[$i]");
        print index_sheet "<tr><td><a href=\"$lab_array[$i]/index.html\">$lab_array[$i]</a></td></tr>\n";

        open ('section_sheet', ">$idDropBox/$lab_array[$i]/index.html");

        print section_sheet "<html><head><title>MCS Submission Grader section $lab_array[$i] and dropbox $idDropBox</title></head><body bgcolor=\"#FFFFFF\">\n";

        print section_sheet "<table>\n";
        print section_sheet "<tr><td>idDropBox:<td><b>$idDropBox</b></tr>\n";
        print section_sheet "<tr><td>section:<td><b>$lab_array[$i]</b></tr></table><hr>\n";
        print section_sheet "Some students may have submitted multiple times, please grade only the assignment which you believe is the one that the student intended as the submission.  You may grade the same assignment multiple times, and only the last grade you enter for a particular assignment will actually count.  The only problem is that since grades are emailed, the student will receive several emails if you grade the assignment multiple times.<br><hr>\n";
        print section_sheet "<table>\n";
        print section_sheet "<tr><td>Username</td><td>Attempt</td></tr>\n";
        close (section_sheet);
        #print STDERR "lab = ->$lab_array[$i]<-\n";
    }

    for (my $i = 0; $i < @unames; $i++) {

        # blasts newline
        my $uname = $unames[$i];

        # assumes uniqueness among usernames, and lines themselves
        my @tmparray = (grep /$uname/, @database_array);
        my $tmpvar   = "@tmparray";
           $tmpvar   =~ s/ //g;
        (my $USERID, my $LN, my $FN, my $LEC, my $LAB, my $EMAIL) = split (',', $tmpvar);

        if ($LAB eq "") {
            $LAB = "unknown_section";
        }

        # should be done already, but just in case.
        testandcreate ("$idDropBox/$LAB");

        open ('section_sheet', ">>$idDropBox/$LAB/index.html");

        myprint "-------database file check---------\n";
        myprint "USERID = -->$USERID<--\n";
        myprint "LN     = -->$LN<--\n";
        myprint "FN     = -->$FN<--\n";
        myprint "LEC    = -->$LEC<--\n";
        myprint "LAB    = -->$LAB<--\n";
        myprint "EMAIL  = -->$EMAIL<--\n";
        myprint "-----done database file check------\n";

        myprint "checking -->$uname in section #$LAB<-- .... \n";

        # the href we are grabbing is given by a relative path
        # do a source dump of that address, but first authenticate

        for (my $ATTEMPT = 1; $ATTEMPT <= $max; $ATTEMPT++) {
            my $THE_URL = "$wget --http-user=$user --http-passwd=$pass \"$urlSubmitList$uname$urlSubmitListMid$ATTEMPT$urlSubmitListTail\"";

            #my $data = `$THE_URL -O - | grep Response`;
            #myprint "$THE_URL\n";
            #my $data = mysystem ("$THE_URL -O - | grep 'Student response'");
            #print "data = $data\n";

            my $data = removeDosChars (mysystem ("$THE_URL -O -"));

            #if (data ne "") 

            ##################################################
            # grepping for student response
            ##################################################

            my $query = "<p><b>Student response</b><pre>";
            my $udata = length ($query);

            if (index ($data, $query) > -1)
            {
                testandcreate("$idDropBox/$LAB/$uname");

                if ($ATTEMPT == 1) {
                    print section_sheet "<tr><td>$uname</td><td> <a href=\"$uname/lab.$ATTEMPT.html\"> #$ATTEMPT</a></td>\n";
                }
                else {
                    print section_sheet "<td><a href=\"$uname/lab.$ATTEMPT.html\">#$ATTEMPT</a></td>\n";
                }

                open ("tmp_output", ">$idDropBox/$LAB/$uname/lab.$ATTEMPT.html");
                print tmp_output "<html><head><title>$uname idDropBox=$idDropBox</title></head><body bgcolor=\"#FFFFFF\">\n";

                print tmp_output "<table>\n";
                print tmp_output "<tr><td>Username: <td><b><i>$uname</i></b><br>\n";
                print tmp_output "<tr><td>Lab Section: <td><b>$LAB</b><br>\n";
                print tmp_output "<tr><td>idDropBox: <td><b>$idDropBox</b><br>\n";
                print tmp_output "<tr><td>E-Mail: <td><b>$EMAIL</b><br>\n";
                print tmp_output "</table><hr>\n";

                print tmp_output "<b>Instructions</b>: This page includes an area for you to include a comment for every question if you wish (<b>THIS IS OPTIONAL</b>, but you should atleast try to give some feedback in the \"General Comments\" section at the bottom of this form..  You may also include a general comment which will apply to the entire quiz in general.  Do not forget to give the it a final score when you are finished.";

                if ($QUIZ_DEBUG == 1) {
                    print tmp_output "<p><hr><font size=+1><b>Note</b></font>: Your comments will <b>NOT</b> be sent via email to the student because this script is in the <i>debug</i> state.  If you are a grader expecting to grade this quiz, stop now and notify webct\@mcs.drexel.edu immediately.\n";
                }
                else {
                    print tmp_output "<p><hr><font size=+1><b>Note</b></font>: Your comments will be sent via email to the student ...\n";
                }

                print tmp_output "<hr>\n";
                close (tmp_output);

                open ("tmp_output", ">> $idDropBox/$LAB/$uname/lab.$ATTEMPT.html");

                # get the number of questions
                my $offset = 0;
                my $qnumb  = 0;
                while ( ($offset = index ($data, $query, $offset+$udata) ) > -1 ) {$qnumb++;}

                print tmp_output "<form method=\"POST\" ACTION=\"http://point.mcs.drexel.edu/cgi-bin/webct/quizgrader/quizgrader.pl?1:$qnumb:$uname:$idDropBox:$LAB:$EMAIL\">\n";

                $offset = 0;
                $qnumb  = 1;
                while ( ($offset = index ($data, $query, $offset+$udata) ) > -1 ) {
                    my $start    = index  ($data, $query, $offset);
                    my $end      = index  ($data, "</pre>", $start);
                    my $response = substr ($data, $start+$udata, $end - ($start+$udata));

                    if ($response eq "" || $response eq " ") {
                        $response = "<b>THE STUDENT LEFT THIS ANSWER BLANK</b>";
                    }

                    print tmp_output "<p>Question #$qnumb:<br><table> <tr><td bgcolor=c0c0c0> <pre>$response</pre></table>";
                    print tmp_output "<b>Grader Response</b><input type=\"text\" name=\"r$qnumb\" SIZE=512>\n";
                    $qnumb++;
                }
                print tmp_output "<p><h3>General comments for $uname</h3>\n";
                print tmp_output "<input type=\"text\" name=\"gen\" SIZE=512>\n";
                print tmp_output "<p><h3>Score for $uname</h3>\n";
                print tmp_output "<input type=\"text\" name=\"score\" SIZE=4>\n";
                print tmp_output "<p><input type=\"submit\" value=\"Submit Responses\">\n";
                print tmp_output "</body></html>\n";
                close (tmp_output);
            }
        }
        close (section_sheet);
    }
    for (my $i = 0; $i < @lab_array; $i++) {
        open ('section_sheet', ">>$idDropBox/$lab_array[$i]/index.html");
        print section_sheet "</table></body></html>\n";
        close (section_sheet);
    }
    print index_sheet "</table></body></html>\n";
    close (index_sheet);
    myprint "The directory \"$idDropBox\" has been created for you.  Examine the index.html\nfile in that directory for a browsable version of the submissions.  If you\nare a Drexel TA and you supplied a database file with the -z option, then\nyou can use this to submit graded responses to students, webct, and store\nthe information on King/Queen.  If you forgot about the -z option, then you will\n need to rerun $prog again with this option.\n";
}


sub getPassword {

    my ($max) = @_;

    print STDOUT "password: (press CNTL-D (maybe twice) when finished, ^D is okay)\n";
    #if ($NEW_PASSWAY) {
    #    ReadMode('noecho');
    #    $password = ReadLine(0);
    #}
    #$0 = "bunky";
    system ("stty -echo");
    read (STDIN, my $password, $max);
    #$0 = "";
    system ("stty echo");
    chomp ($password);          # just in case they pressed enter once
    print STDOUT "\n";   # puts the echo'ed password on a line by itself

    return $password;
}


# weird
sub mysystem {

    my ($arg, $verbose) = @_;

    # verbose check
    if (! defined $_[1]) {
        # get global if not defined here
        $verbose = $VERBOSE;
    }

    # print STDOUT "$arg\n";
    myprint "$arg\n";

    #my $output = `$arg`;
    #return $output;
    return `$arg`;
}


sub exiterr {
    my ($prog, $arg) = @_;

    print STDOUT "$prog (error): $arg";
    exit(1);
}

sub testandcreate {

    my ($arg) = @_;

    if (-e "$arg") {
        myprint "$arg/ exists, not created.\n";
    }
    else {
        myprint "$arg/ doesn't exist, created.\n";
        #mysystem ("mkdir $arg");
        mkdir $arg;
    }
}


# prints if at verbose depth
sub myprint {

    my ($arg, $depth, $verbose) = @_;

    # depth check
    if (! defined $_[1]) {
        $depth = 1;
    }

    # if this contains the persons password, suppress it
    if ( index ($arg, $PASSWORD) > -1 ) {

        my $stars = "";
        for (my $i = 0; $i < length ($PASSWORD); $i++) {
            $stars = "$stars*";
        }
        $arg =~ s/$PASSWORD/$stars/g;
        #$arg =~ s/$PASSWORD/SUPPRESSED/g;
    }

    # verbose check
    if (! defined $_[2]) {
        # get global if not defined here
        $verbose = $VERBOSE;
    }

    if ($verbose >= $depth) {
        #print STDERR "$arg";
        print STDERR "$arg";
    }
}

sub removeDosChars {

    # myprint "should make sure it isn't binary before doing this!!!\n";

    my $arg = @_[0];
    for (my $ind = 1; $ind < @_; $ind++) {
        $arg = "$arg $_[$ind]";
    }

    # cheesy binary file detection, i see ^V-10 (^@) in every binary file
    if (index($arg, '') > -1) {
        return $arg;
    }

    #print "b4: $arg, has @_ members\n";

    # removes the ^M that vim suppresses, doesn't get the one below though
    $arg =~ tr///s;

    # removes the ^M\n combination, since it has a newline just oust ^M
    $arg =~ s/\n/\n/g;

    # removes the ^M that vim does not suppress, gets the !^M\n combinations
    $arg =~ s//\n/g;

    #$arg =~ tr/\^M//s;
    #$arg =~ tr/^M//s;

    #print "af: $arg\n";

    return $arg;
}


###############################################################################
# sub uniquify - takes an array as an argument
#              - uniquifies an array of strings 
###############################################################################

sub uniquify {
    
    my (@arr) = (@_);                      
    
    #print "INITIALLY @arr ELEMENTS = @arr\n";
    
    for (my $i = 0; $i < @arr; $i++) {
        for (my $j = ($i+1); $j < @arr; $j++) {
            #print "arr[i] eq arr[j] ------ arr[$i] eq arr[$j] ----- $arr[$i] eq $arr[$j]\n";
            if ($arr[$i] eq $arr[$j]) {
                #print "    ----------\n";
                #print "    INITIALLY       = @arr\n";
                
                # swap with last position
                my $tmp     = $arr[(@arr-1)];
                   $arr[(@arr-1)]  = $arr[$j];
                   $arr[$j] = $tmp;
                 
                #print "    MID-JUSTSWAPPED = @arr\n";
                # then shift to oust the duplicate
                pop @arr;
                #print "    AFT=POP        = @arr\n";
                
                # need this in case the last position guy was the same as the one you swapped
                --$j;
            }
        }      
    }
    return (@arr);
}

sub myrmdir {
    my ($idDropBox) = @_;                      

    # windows gets weird with the STDIN reading, so we'll bag this for now
    #my $ok = "";
    my $ok = "y";
    while ($ok ne "y" && $ok ne "n") {
        #myprint ("\nlast ok = ->$ok<-\n", 2);
        print "\nThe directory $idDropBox already exists, shall I remove it for you (y/n)?: ";
        $ok = "";
        read (STDIN, $ok, 1);
        chomp($ok);
    }
    if ($ok eq 'y') {

        my @dirstack;
        push (@dirstack, $idDropBox);

        while ( (my $dir = pop (@dirstack) ) ne "") {

            opendir CWD, "$dir" or die "weird error";
            my @allfiles = map "$dir/$_", readdir CWD;

            # gets rid of first two args, current and parent directory
            shift (@allfiles);
            shift (@allfiles);

            #push parent first, before get into next depths of the tree

            #print STDERR "traversing $dir\n";

            my $hasSubDirs = 0;
            for (my $i = 0; $i < @allfiles; $i++) {
    
                if (-f $allfiles[$i]) {
                    #print STDERR "deleting $allfiles[$i]\n";
                    unlink $allfiles[$i];
                }
                elsif (-d $allfiles[$i]) {

                    # only push CWD again if it has embedded subdirectories
                    # thats b/c we can't delete it unless it has nothing in it
                    if ($hasSubDirs == 0) {
                        push (@dirstack, $dir);
                        $hasSubDirs = 1;
                    }
                    push (@dirstack, $allfiles[$i]);
                }
                else {
                    myprint "$allfiles[$i]: This isn't a file or directory?????\n";
                }
            }
            closedir CWD;
            if ($hasSubDirs == 0) {
                rmdir $dir;
            }
        }
    }
    #myprint ("\nlast ok = ->$ok<-\n", 2);
}

sub getUnameList {

    my ($prog, $wget, $domain, $user, $pass, $idCourse, $idDropBox, $mode, $queryDomain, $strUnameFile, %modes) = @_;

    # PROMPT FOR PASSWORD IF NOT GIVEN
    # get usernames
    # pack them into a ' ' delimited string b/c I don't know how to pass arrays

    my $strUnameList = "";

    # doing a web query
    if ($queryDomain) {

        # if none specified at command line, get from the web
        if ($strUnameFile eq "") {
            myprint "$prog will get your list of usernames from WebCT...\n";
            # get the submission list, now check options
            # get non-graded, get new, etc
            # skip downloads of people who submit nothing, handle all attempts

            #my $url = "http://webct.drexel.edu/SCRIPT/CS172_Summer2001/scripts/designer/serve_quiz.pl?ACTION=SUBMISSIONS_SM&SUB_ACTION=START&ID=996167956&STUDENT=";
            my $url;
            my @html_data;
            if ($mode == $modes{'quizzes'}) {
                $url = "http://$domain/SCRIPT/$idCourse/scripts/designer/serve_quiz.pl?ACTION=SUBMISSIONS_SM&SUB_ACTION=START&ID=$idDropBox&STUDENT=";

                #my @html_data = grep !/\+student\+/, (grep /(STUDENT|head2 col8)/, split ('\n', mysystem ("$wget --http-user=$u --http-passwd=$PASSWORD -O - \"$url\"")));
                @html_data = grep !/\+student\+/, (grep /STUDENT/, split ('\n', mysystem ("$wget --http-user=$user --http-passwd=$pass -O - \"$url\"")));

                #myprint "----------->@html_data<--------------\n";

                for (my $ind = 0; $ind < @html_data; $ind++) {
                    #print "->$html_data[$ind]<-\n";
                    my $query = "STUDENT=";
                    my $udata = length ($query);
                    my $start = index ($html_data[$ind], $query);
                    my $end   = index ($html_data[$ind], "&", $start);
                    my $uname = substr ($html_data[$ind], $start+$udata, $end - ($start+$udata));
                    $strUnameList = "$uname $strUnameList"
                }
            }
            elsif    ($mode == $modes{'assignments'}) {
                $url = "http://$domain/SCRIPT/$idCourse/scripts/marker/dropbox_marks.pl?LIST1+_side_nav++$idDropBox+++";

                @html_data = grep /CHECKBOX NAME=/, split ('\n', mysystem ("$wget --http-user=$user --http-passwd=$pass -O - \"$url\""));
                for (my $ind = 0; $ind < @html_data; $ind++) {
                    #print "->$html_data[$ind]<-\n";
                    my $query = "CHECKBOX NAME=";
                    my $udata = length ($query);
                    my $start = index ($html_data[$ind], $query);
                    my $end   = index ($html_data[$ind], "\" ", $start);
                    my $uname = substr ($html_data[$ind], $start+$udata+1, $end - ($start+$udata+1));   
                    $strUnameList = "$uname $strUnameList";
                }
            }
        }
        else {
            open ("input", "<$strUnameFile");
            if (-e $strUnameFile) {
               myprint "$prog will read in your list of usernames ...\n";
            }
            else {
                exiterr ($prog, "$strUnameFile is NOT FOUND\n");
            }

            # makes just a big ' ' separated string between each username
            chomp (my $line = <input>);
            $strUnameList = "$line";
            while (<input>) {
                chomp ($line = $_);
                $strUnameList = "$strUnameList $line";
            }

            close (input);
            #myprint "unames = ->$strUnameList<-\n";
        }
    }

    # uniquify the strUnameList

    #myprint "BEFORE $strUnameList\n";
    return (join (' ', uniquify (split (' ', $strUnameList) ) ) );
    # my @tmpArray     = uniquify (split (' ', $strUnameList) );
    # my $strUnameList = join (' ', @tmpArray);
    #myprint "AFTER $strUnameList\n";
}

sub strDropBox2idDropBox {
    my ($prog, $wget, $domain, $user, $pass, $idCourse, $strDropBox, $mode, %modes) = @_;

    my $idDropBox;

    # quiz listing
    #http://webct.drexel.edu/SCRIPT/CS172_Summer2001/scripts/student/quiz_home.pl?LISTING_LOWER+_side_nav+

    # assignment listing
    #http://webct.drexel.edu/SCRIPT/CS172_Summer2001/scripts/student/dropbox_stud_home.pl?START+_side_nav

    if    ($mode == $modes{'quizzes'}) {
        my $urltmp = "http://$domain/SCRIPT/$idCourse/scripts/student/quiz_home.pl?LISTING_LOWER+_side_nav+";

        my @url_data = grep /SHOW_QUIZ2/, grep /$strDropBox/, mysystem ("$wget --http-user=$user --http-passwd=$pass -O - \"$urltmp\"");

        if (@url_data > 1) {
            my $tmp = scalar(@url_data);
            exiterr ("$prog", "$tmp items were returned, please narrow your search by bounding\nthe -d string in something like \">$strDropBox<\", this will eliminate things which\ncontain all the characters of $strDropBox plus more.  If you want something larger\nthan just \">$strDropBox<\" (ie. \"$strDropBox Updated Version\", then you only need a\nsubstring of this ... and the '>' '<' mark the definite bounds.  So for that\nstring,something like \">$strDropBox Updated\" will work.  Notice how this does not\ninclude the definitive end character ('<').\n");
        }
        #print "---------->@url_data<---------\n";

        my $query  = "SHOW_QUIZ2";
        my $udata  = length ($query);
        my $start  = index ($url_data[0], $query);
        my $end    = index ($url_data[0], "'", $start);
        $idDropBox = substr ($url_data[0], $start+$udata+1, $end - ($start+$udata+1));

        #myprint "---------->$start<---------\n";
        #myprint "---------->$end<---------\n";
        #myprint "---------->$idDropBox<---------\n";
    }
    elsif ($mode == $modes{'assignments'}) {
        my $urltmp = "http://$domain/SCRIPT/$idCourse/scripts/student/dropbox_stud_home.pl?START+_side_nav";

        my @url_data = grep /$strDropBox/, mysystem ("$wget --http-user=$user --http-passwd=$pass -O - \"$urltmp\"");

        #myprint print "---------->@url_data<--------------\n";

        # just grepped out (ie. "Assignment #1"), now get index of it
        # looks like:
        # ----------><TD VALIGN="TOP"><FONT SIZE="-1"><label for='webct1'><A TARGET="main_win" HREF="javascript:top.update_loc_to_new_tool('Assignment #1', 'Assignment #1', '/SCRIPT/CS172_Summer2001/scripts/student/dropbox_view.pl?START+_side_nav++993576040', '/SCRIPT/CS172_Summer2001/scripts/designer/dropbox_edit.pl?START+_side_nav++993576040', 2, 0)" ONMOUSEOVER="window.status='Assignment #1'; return true"  ONMOUSEOUT="window.status=''; return true">Assignment #1</A></label></FONT></TD>
        #index ($html_data[$ind], $strDropBox);

        if (@url_data > 1) {
            my $tmp = scalar(@url_data);
            exiterr ("$prog", "$tmp items were returned, please narrow your search by bounding\nthe -d string in something like \">$strDropBox<\", this will eliminate things which\ncontain all the characters of $strDropBox plus more.\n");
        }

        my $query  = "START+_side_nav++";
        my $udata  = length ($query);
        my $start  = index  ($url_data[0], $query);
        my $end    = index  ($url_data[0], "'", $start);
        $idDropBox = substr ($url_data[0], $start+$udata, $end - ($start+$udata+1)+1);  
        myprint "The dropbox is ----->$idDropBox<-----\n";
    }

    #### we know that atleast one of these two was already sanity checked
    #### final check just to make sure we have the dropbox
    if ($idDropBox eq "") {
        exiterr ("The idDropBox ($idDropBox) is invalid, it might not exist.  Use the -b option if all else\nfails.  It is also possible that your password is incorrect.\n");
    }
    # this isn't mandatory just yet
    #if ($strDropBox eq "") {
    #}
    return $idDropBox;
}






sub printUsage {

    my ($prog, $version) = @_;

    #print STDOUT "MUST HANDLE CASES WITH PASS BY REFERENCE AND LOCAL\n";
    print STDOUT "-------------------------------------------------------------------------------\n";
    print STDOUT "1-ASSIGNMENTS \$  $prog -u uid -c course (-b boxid | -d strbox) (-a|-o)\n";
    print STDOUT "2-QUIZZES     \$  $prog -u uid -c course (-b boxid | -d strbox) -q\n";
    print STDOUT "3-USAGE       \$  $prog -h\n";
    print STDOUT "4-VERSION     \$  $prog -v\n";
    print STDOUT "X-OPTIONS        [-f unamesfile]  [-p passwd]  [-e]  [-z] (combine with 1 or 2)\n";
    print STDOUT "-------------------------------------------------------------------------------\n";
    print STDOUT "\nStandard Options\n";
    print STDOUT "        -u user id\n";
    print STDOUT "        -c coursename\n";
    print STDOUT "        -b boxid (dropbox of the submission)\n";
    print STDOUT "        -z database (give it a database file, for quiz grading)\n";
    print STDOUT "        -d submission (name as it appears in WebCT, \"Lab 2\")\n";
    print STDOUT "        -p passwd (leave blank if you want to be prompted)\n";
    print STDOUT "        -f filename (specify a '\\n' delimited list of usernames)\n";
    print STDOUT "\nMode Options\n";
    print STDOUT "        -a download assignments\n";
    print STDOUT "        -o post processing of assignments, for Plagiarism Detection System\n";
    print STDOUT "        -q download quizzes (labs)\n";
    print STDOUT "\nInformative Options\n";
    print STDOUT "        -e verbose printing (*IMPORTANT*: for $prog newbies)\n";
    print STDOUT "        -v version info\n";
#    print STDOUT "        -n give a name to this job for friendly handling\n";
    print STDOUT "        -h help info (this message)\n";
#    print STDOUT "\nMCS options\n";
#    print STDOUT "        -m should say when you are on mcs machines\n";
    print STDOUT "-------------------------------------------------------------------------------\n";
    print STDOUT "Thank you for using *$prog* v$version\n\nChris Cera (cera\@drexel.edu)\nMath \& Computer Science, Drexel University (http://www.mcs.drexel.edu)\nPhiladelphia, Pennsylvania\n";
    print STDOUT "-------------------------------------------------------------------------------\n";
}
