#!/usr/bin/perl
###############################################################################
# getsub.pl   (GET SUBMITTED x) (only works on assignments)
# Chris Cera, chris@cera.ws
# Created :  1/31/2001
# Modified: 4/8/2001
# Version: 1.1
###############################################################################
# YADPP - Yet Another Dirty Perl Program
# tested on WebCT version 3.1.3, probably incompatible with any other version
# uses http authentication at each step
# 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 lynx on your system in this version, cURL could also be used
# this was developed on Linux, so I don't know if it will run from windows.
#     there is no reason why it shouldn't be able to, except for the slash
#     issues that exist b/w windows and unices.  Hacking it would be minimal.
###############################################################################
# WISHLIST
#
# automatically change modification dates to match those submitted
# get this to work for quizzes
###############################################################################
# LYNX INFO USED
###############################################################################
#[0231] [chris@localhost:~/edu/cs999/hw2/submissions]$ lynx --version 
#
#Lynx Version 2.8.4dev.7 (03 Aug 2000)
#Built on linux-gnu Aug  4 2000 05:38:40
#
#Copyrights held by the University of Kansas, CERN, and other contributors.
#Distributed under the GNU General Public License.
#See http://lynx.browser.org/ and the online help for more information.
###############################################################################
# CONSTANTS
###############################################################################

$PROGNAME = "getsub";
$DOMAIN = "webct.drexel.edu";
$strUnameFile="./unames.txt";

###############################################################################
# CHECK command line usage
###############################################################################

# this is the uniquely assigned id number for each assignment
#$idCourse  = "CS999";  # cs999 works too for some magical reason
#$idDropBox = "123456789";

if (@ARGV == 4) {
    $u         = $ARGV[0];
    $p         = $ARGV[1];
    $idCourse  = $ARGV[2];
    $idDropBox = $ARGV[3];
}
else {
    print STDERR "Usage:  ./$PROGNAME u p c d\n";
    print STDERR "        u userid\n";
    print STDERR "        p passwd\n";
    print STDERR "        c coursename\n";
    print STDERR "        d dropid\n";
    exit (1);
}

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

###############################################################################
# LAME I/O
###############################################################################

open (input, "<$strUnameFile");

if (-e $strUnameFile) {
    print STDOUT "$PROGNAME will read in your list of usernames ...\n";
}
else {
    print STDOUT "$strUnameFile is NOT FOUND\n";
    exit (1);
}

# create assignment id directory if DNE
if (! -e "$idDropBox") {
    print STDOUT "assignment id directory -->$idDropBox<-- created\n";
    system ("mkdir $idDropBox");
}
else {
    print STDERR "directory $idDropBox exists, not created\n";
}


###############################################################################
# REAL WORK
#
#     input is simply a single column file of usernames
###############################################################################

$i = 0;
while ( <input> ) {

    # blasts newline
    chop ($uname = $_);

    $unames[$i++] = $uname;
    $SRCFILE = "test/$_";

    print STDOUT "checking -->$uname<-- .... \n";

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

    chop ($data = `lynx --source -auth=$u:$p $urlSubmitList$uname | grep HREF`);

    # 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 '<'

    $pos = 0;
    $switch = 1;
    while ( ($pos = index ($data,"HREF",$pos) ) > -1) {

        # check if first time in loop for this user
        if ($switch == 1) {
            # create directory if DNE
            if (-e "$idDropBox/$uname") {
                print STDOUT "directory $idDropBox/$uname exists, not created";
            }
            else {
                print STDOUT "    directory $idDropBox/$uname created.\n";
                system ("mkdir $idDropBox/$uname");
            }
            $switch = 0;
        }

        $end_bracket = index ($data, ">", $pos);
        $stt_bracket = index ($data, "<", $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
        $len = ($stt_bracket - 1) - ($end_bracket + 2) + 1;

        # print STDERR "end=$end_bracket\nstt=$stt_bracket\npos=$pos\n";
        # print STDERR "len=$len\n";

        # rip out the filename from the url
        $filename = substr ($data, $end_bracket+2, $len);

        print STDOUT "    has submitted -->$filename<--\n";

        $urlSubmission = "$DOMAIN/SCRIPT/CS999/scripts/student/dropbox_files_view.pl?FILE_VIEW+_side_nav++$idDropBox+$filename+$uname";

        system ("lynx --source -auth=$u:$p $urlSubmission | grep -v xmp > $idDropBox/$uname/$filename");

        # 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); }
}
