#!/usr/local/bin/perl
# index.cgi
# List all at jobs and display a form for creating a new one

require './at-lib.pl';
use POSIX;
&header($text{'index_title'}, "", undef, 1, 1);
print "<hr>\n";
%access = &get_module_acl();

@jobs = &list_atjobs();
@jobs = grep { &can_edit_user(\%access, $_->{'user'}) } @jobs;
if (@jobs) {
	@jobs = sort { $a->{'id'} <=> $b->{'id'} } @jobs;
	print "<table border width=100%>\n";
	print "<tr $tb> <td><b>$text{'index_id'}</b></td>\n";
	print "<td><b>$text{'index_user'}</b></td>\n";
	print "<td><b>$text{'index_exec'}</b></td>\n";
	print "<td><b>$text{'index_created'}</b></td>\n";
	print "<td><b>$text{'index_cmd'}</b></td> </tr>\n";
	foreach $j (@jobs) {
		print "<tr $cb>\n";
		print "<td><a href='edit_job.cgi?id=$j->{'id'}'>",
		      "$j->{'id'}</td>\n";
		print "<td>",&html_escape($j->{'user'}),"</b></td>\n";
		$date = localtime($j->{'date'});
		print "<td><tt>$date</tt></td>\n";
		$created = localtime($j->{'created'});
		print "<td><tt>$created</tt></td>\n";
		print "<td>",join("<br>", split(/\n/, &html_escape($j->{'realcmd'}))),"</td>\n";
		print "</tr>\n";
		}
	print "</table><br><hr>\n";
	}

print "<form action=create_job.cgi>\n";
print "<table border>\n";
print "<tr $tb> <td><b>$text{'index_header'}</b></td> </tr>\n";
print "<tr $cb> <td><table>\n";

print "<tr> <td><b>$text{'index_user'}</b></td>\n";
$dir = "/";
if ($access{'mode'} == 1) {
	print "<td><select name=user>\n";
	foreach $u (split(/\s+/, $access{'users'})) {
		print "<option>$u\n";
		}
	print "</select></td>\n";
	}
elsif ($access{'mode'} == 3) {
	print "<td><tt>$remote_user</tt></td>\n";
	print "<input type=hidden name=user value='$remote_user'>\n";
	@uinfo = getpwnam($remote_user);
	$dir = $uinfo[7];
	}
else {
	print "<td><input name=user size=8>",
		&user_chooser_button("user", 0),"</td>\n";
	}



@now = localtime(time());
print "<tr> <td><b>$text{'index_date'}</b></td>\n";
printf "<td><input name=day size=2 value='%d'>/", $now[3];
print "<select name=month>\n";
for($i=0; $i<12; $i++) {
	printf "<option value=%s %s>%s\n",
		$i, $now[4] == $i ? 'selected' : '', $text{"smonth_".($i+1)};
	}
print "</select>/";
printf "<input name=year size=4 value='%d'>\n", $now[5] + 1900;
print &date_chooser_button("day", "month", "year"),"</td>\n";

print "<td><b>$text{'index_time'}</b></td>\n";
print "<td><input name=hour size=2>:<input name=min size=2></td> </tr>\n";

($date, $time) = split(/\s+/, &make_date(time()));

print "<tr> <td><b>$text{'index_cdate'}</b></td>\n";
print "<td>$date</td>\n";

print "<td><b>$text{'index_ctime'}</b></td>\n";
print "<td>$time</td> </tr>\n";

print "<tr> <td><b>$text{'index_dir'}</b></td>\n";
print "<td colspan=3><input name=dir size=40 value='$dir'></td> </tr>\n";

print "<tr> <td valign=top><b>$text{'index_cmd'}</b></td>\n";
print "<td colspan=3><textarea rows=5 cols=40 name=cmd></textarea></td></tr>\n";

print "<tr> <td colspan=4 align=right>",
      "<input type=submit value='$text{'create'}'></td> </tr>\n";

print "</table></td></tr></table></form>\n";
print "<hr>\n";
&footer("/", $text{'index'});

