#!/usr/local/bin/perl
# Jim Dutton  - 10/12/1998; 11/12/1998
#
$ldapcmd = "/usr/local/bin/ldapsearch -h <LDAP server> -b o=<org>,c=US";
$expectcmd = "/usr/local/libexec/submit.exp";
$basedir = "/var/samba/MFtransfer";
$system_joblog = "/var/log/samba/submitjob.log";
@userlist = `ls $basedir`; chop @userlist;
$result = `date >> $system_joblog`; $RCstring = "***  Submit [RC]:";
$LDAPerr = "***  Error retrieving MVS password from LDAP database [RC]:";
$currdate = `date "+19%y%m%d"`; chop $currdate;

foreach $user (@userlist) {
# $result = `echo "user: [$user]" >> $system_joblog`;
  $user_joblog = "$basedir/$user/submit/submit.log.$currdate";
  @joblist = `ls $basedir/$user/submit/*.jcl 2>/dev/null`; chop @joblist;
  $jobcount = scalar @joblist;
  $result = `echo "user: [$user:$jobcount]" >> $system_joblog`;
# print "jobcount = $jobcount\n";

  if ($jobcount > 0) {
#    $result = `/bin/cat /dev/null > $user_joblog`;
    $result = `echo "= = = = = = = = = =" >> $user_joblog`;
    $LDAPfilter = "MVSuserid=$user";
    ($DN,$MVSpasswd) = `$ldapcmd "$LDAPfilter" "MVSpasswd"`; $ldapRC = $?;

    if ($ldapRC == 0) {
      chomp $DN; chomp $MVSpasswd;
      ($dontcare,$MVSpasswd) = split("=",$MVSpasswd);

      foreach $job (@joblist) {
        $jclfile = `/usr/bin/basename $job`; chomp $jclfile;
        $result = `echo "  job: [$jclfile]" >> $system_joblog`;
        $result = `$expectcmd "$user" "$MVSpasswd" "$jclfile" "$currdate"`;
        $RC = $?; $result = `rm $job`;
        $result = `echo "$RCstring [$RC]" >> $user_joblog`;
        $result = `echo "  $RCstring [$RC]" >> $system_joblog`;
      }
    }
    else {
      $result = `date >> $user_joblog`;
      $result = `echo "$LDAPerr [$ldapRC]" >> $user_joblog`;
      $result = `echo "  $LDAPerr [$ldapRC]" >> $system_joblog`;
    }
  }

  @user_info = getpwnam $user;
  $result = chown(@user_info[2],@user_info[3],$user_joblog);
}
exit;
