#!/usr/bin/perl -w

  use strict;
  use lib '/home/httpd/lib/perl';
  use localenroll;
  use LONCAPA::Configuration;
  use LONCAPA::Enrollment;
  use Apache::lonnet;
  use Apache::loncoursedata;

# Determine the library server's domain
  my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
  my $dom = $$perlvarref{'lonDefDomain'};
  print "Domain is $dom\n";

# Determine the present time;
  my $timenow = time();

# Determine the courses
  my %courses = &Apache::lonnet::courseiddump($dom,'.',1); 
  my %affiliates = ();
  my %enrollvar = ();
  my %LC_code = ();
  foreach my $key (sort keys %courses) {
      my $crs;
      if ($key =~ m/^($dom)_(\w+)$/) {
          $crs = $2;
      }

# Get course settings
      my %settings = &Apache::lonnet::dump('environment',$dom,$crs);
      %{$enrollvar{$crs}} = ();
      @{$affiliates{$crs}} = ();
      %{$LC_code{$crs}} = ();
      foreach my $item (keys %settings) {
          if ($item =~ m/^internal\.(.+)$/) {
              $enrollvar{$crs}{$1} = $settings{$item};
          }
      }
      if (($enrollvar{$crs}{autostart} >= $timenow) && ($enrollvar{$crs}{autoend} < $timenow)) {
          if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1) {
# Add to list of classes for retrieval
              $enrollvar{$crs}{sectionnums} =~ s/ //g;
              $enrollvar{$crs}{crosslistings} =~ s/ //g;
              my @sections = ();
              my @crosslistings = ();
              if ($enrollvar{$crs}{sectionnums} =~ m/,/) {
                  @sections = split/,/,$enrollvar{$crs}{sectionnums};
              } else {
                  @$sections = $enrollvar{$crs}{sectionnums};
              }
              if ($enrollvar{$crs}{crosslistings} =~ m/,/) {
                  @crosslistings = split/,/,$enrollvar{$crs}{crosslistings}
              } else {
                  @crosslistings = $enrollvar{$crs}{crosslistings};
              }
              foreach my $sec (@sections) {  
                  if ($sec =~ m/^(\w+):/(\w*)$) {
                      my $course_id = $enrollvar{$crs}{coursecode}.$1;
                      my $gp = $2;
                      if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
                          push @{$affiliates{$crs}}, $course_id;
                          $LC_code{$crs}{$course_id} = $gp; 
                      }
                  }
              }
              foreach my $xlist (@crosslistings) {
                  if ($xlist =~ m/^(\w+):(\w*)$/) {
                      my $course_id = $1;
                      my $gp = $2;
                      if (!grep/^$course_id$/,@{$affiliates{$crs}}) {
                          push @{$affiliates{$crs}}, $course_id;
                          $LC_code{$crs}{$course_id} = $gp;
                      }
                  }
              }
          }
      }
      print "Key is $key, Value is $courses{$key}\n";
  }
  &localenroll::fetch_enrollment($dom,\%affiliates,\%reply);

# Now go through classes and perform required enrollment changes.
  foreach my $crs (sort keys %enrollvar) {
      if (($enrollvar{$crs}{autostart} >= $timenow) && ($enrollvar{$crs}{autoend} < $timenow)) {
          if (($enrollvar{$crs}{autoadds} == 1) || ($enrollvar{$crs}{autodrops} == 1)) {
              my $response = &LONCAPA::Enrollment::update_LC($dom,$crs,$enrollvar{$crs}{autoadds},$enrollvar{$crs}{autodrops},$enrollvar{$crs}{startdate},$enrollvar{$crs}{enddate},$enrollvar{$crs}{authtype},$enrollvar{$crs}{autharg},\@{$affiliates{$crs}},\%{$LC_code{$crs}},"automated");
              unless ($enrollvar{$crs}{notifylist}  eq '') {
# Send message about enrollment changes to notifylist.              
              }
          }
      }
  }

# Check for photos


1;
