#!/usr/local/bin/perl -w 

# Copyright (C) 2002 Michael Gage 

###############################################################################
# Web service which fetches WeBWorK problems from a library.
###############################################################################
package FetchLibraryProblems;
use strict;
use sigtrap;
use Carp;
use Safe;
use WeBWorK::PG::Translator;
use WeBWorK::PG::IO;
use Benchmark;
use MIME::Base64 qw( encode_base64 decode_base64);

my %AVAILABLE_PROBLEM_LIBRARIES	= 	(	ww_prob_lib		=> 	'/u/gage/webwork/rochester_problib/',
										indiana_prob_lib		=> 	'/u/gage/webwork/Indiana_prob_lib/',
										capaOK_lib		=>	'/ww/webwork/courses1/capaOK/templates/',
										capa_lib		=>	'/ww/webwork/courses/capa/templates/',
										prob_lib_cvs	=>	'/ww/webwork/courses/WW_Prob_Lib_CVS/templates/',
										maa_100			=>	'/ww/webwork/courses/maa100/templates/',
										teitel_physics121			=>	'/ww/webwork/courses/teitel-phy121/templates/',
);
my $PASSWORD = 'geometry';

use File::stat;
sub readFile {
	my $rh = shift;
	local($|)=1;
	my $out = {};
	my $filePath = $rh->{filePath};
	unless ($rh->{pw} eq $PASSWORD ) {
		$out->{error}=404;
		return($out);
	}
	if (  defined($AVAILABLE_PROBLEM_LIBRARIES{$rh->{library_name}} )   ) {
		$filePath = $AVAILABLE_PROBLEM_LIBRARIES{$rh->{library_name}} . $filePath;
	} else {
		$out->{error} = "Could not find library:".$rh->{library_name}.":";
		return($out);
	}
	
	if (-r $filePath) {
		open IN, "<$filePath";
		local($/)=undef;
		my $text = <IN>;
		$out->{text}= encode_base64($text);
		my $sb=stat($filePath);
		$out->{size}=$sb->size;
		$out->{path}=$filePath;
		$out->{permissions}=$sb->mode&07777;
		$out->{modTime}=scalar localtime $sb->mtime;
		close(IN);
	} else {
		$out->{error} = "Could not read file at |$filePath|";
	}
	return($out);
}



use File::Find;	
sub listLib {
	my $rh = shift;
	my $out = {};
	my $dirPath;
	unless ($rh->{pw} eq $PASSWORD ) {
		$out->{error}=404;
		return($out);
	}
	
	if (  defined($AVAILABLE_PROBLEM_LIBRARIES{$rh->{library_name}} )   ) {
		$dirPath = $AVAILABLE_PROBLEM_LIBRARIES{$rh->{library_name}} ;
	} else {
		$out->{error} = "Could not find library:".$rh->{library_name}.":";
		return($out);
	}

	my @outListLib;
	my $wanted = sub {
		my $name = $File::Find::name;
		my @out=();
		if ($name =~/\S/ ) {
			$name =~ s|^$dirPath||o;  # cut the first directory
			push(@outListLib, "$name\n") if $name =~/\.pg/;
		}
	};
	my $command = $rh->{command};
	$command = 'all' unless defined($command);
			$command eq 'all' &&    do {print "$dirPath\n\n";
										find($wanted, $dirPath);
										@outListLib = sort @outListLib;
										$out->{ra_out} = \@outListLib;
										$out->{text} = join("", sort @outListLib);
										return($out);
			};
			$command eq 'setsOnly' &&   do {
											if ( opendir(DIR, $dirPath) ) {  
											    my @fileList=();
												while (defined(my $file = readdir(DIR))) {
													push(@fileList,$file) if -d "$dirPath/$file";
													
												}
												$out->{text} = join("\n",sort @fileList);
												closedir(DIR);
											} else {
												$out->{error}= "Can't open directory $dirPath";
											}
											return($out);
			};
			$command eq 'listSet' &&   do { my $dirPath2 = $dirPath . $rh->{set};
			
											if ( opendir(DIR, $dirPath2) ) { 
											    my @fileList =(); 
												while (defined(my $file = readdir(DIR))) {
													if (-d "$dirPath2/$file") {
														push(@fileList, "$file/${file}.pg");
													
													} elsif ($file =~ /.pg$/ ) { # file ends in .pg
														push(@fileList, $file);
													
													}
													
													
												}
												$out->{text} = join("\n",sort @fileList);
												closedir(DIR);
											} else {
												$out->{error}= "Can't open directory $dirPath2";
											}
											
											return($out);
			};
			# else
			$out->{error}="Unrecognized command $command";
			$out;
}


sub pretty_print_rh {
	my $rh = shift;
	my $out = "";
	my $type = ref($rh);
	if ( ref($rh) =~/HASH/ ) {
 		foreach my $key (sort keys %{$rh})  {
 			$out .= "  $key => " . pretty_print_rh( $rh->{$key} ) . "\n";
 		}
	} elsif ( ref($rh) =~ /SCALAR/ ) {
		$out = "scalar reference ". ${$rh};
	} elsif ( ref($rh) =~/Base64/ ) {
		$out .= "base64 reference " .$$rh;
	} else {
		$out =  $rh;
	}
	if (defined($type) ) {
		$out .= "type = $type \n";
	}
	return $out;
}

1;