#!/usr/local/bin/perl
# index.cgi
# Display heartbeat option categories

require './heartbeat-lib.pl';
&header($text{'index_title'}, "", undef, 1, 1, 0,
	&help_search_link("heartbeat", "man", "doc"));
print "<hr>\n";

# Check if heartbeat is installed
if (!-d $config{'ha_dir'}) {
	print "<p>",&text('index_edir', "<tt>$config{'ha_dir'}</tt>",
			  "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
	print "<hr>\n";
	&footer("/", $text{'index'});
	exit;
	}

# Check if the config files exist, or if they can be copied
if (!-r $ha_cf && -r $config{'alt_ha_cf'}) {
	system("cp '$config{'alt_ha_cf'}' $ha_cf");
	}
if (!-r $ha_cf) {
	print "<p>",&text('index_eha_cf', "<tt>$ha_cf</tt>",
		  "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
	print "<hr>\n";
	&footer("/", $text{'index'});
	exit;
	}

if (!-r $haresources && -r $config{'alt_haresources'}) {
	system("cp '$config{'alt_haresources'}' '$haresources'");
	}
if (!-r $haresources) {
	print "<p>",&text('index_eharesources', "<tt>$haresources</tt>",
		  "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
	print "<hr>\n";
	&footer("/", $text{'index'});
	exit;
	}

if (!-r $authkeys && -r $config{'alt_authkeys'}) {
	system("cp '$config{'alt_authkeys'}' '$authkeys'");
	}
if (!-r $authkeys) {
	print "<p>",&text('index_eauthkeys', "<tt>$authkeys</tt>",
		  "$gconfig{'webprefix'}/config.cgi?$module_name"),"<p>\n";
	print "<hr>\n";
	&footer("/", $text{'index'});
	exit;
	}

@opts = ( 'conf', 'res', 'auth' );
@links = map { "edit_${_}.cgi" } @opts;
@titles = map { $text{"${_}_title"} } @opts;
@icons = map { "images/${_}.gif" } @opts;
&icons_table(\@links, \@titles, \@icons);

print "<hr>\n";
print "<table width=100%><tr>\n";
if (open(PID, $config{'pid_file'})) {
	chop($pid = <PID>);
	close(PID);
	}
if ($pid && kill(0, $pid)) {
	print "<form action=apply.cgi>\n";
	print "<input type=hidden name=pid value='$pid'>\n";
	print "<td><input type=submit value='$text{'index_apply'}'></td>\n";
	print "<td>$text{'index_applymsg'}</td>\n";
	print "</form>\n";
	}
else {
	print "<form action=start.cgi>\n";
	print "<td><input type=submit value='$text{'index_start'}'></td>\n";
	print "<td>$text{'index_startmsg'}</td>\n";
	print "</form>\n";
	}
print "</tr></table>\n";

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