#!/usr/local/bin/perl
# build.cgi
# Build a new sendmail.cf, after first confirming the changes

require './sendmail-lib.pl';
require './features-lib.pl';
$features_access || &error($text{'features_ecannot'});
&ReadParse();

if ($in{'confirm'}) {
	# Replace sendmail.cf with new version
	&lock_file($config{'sendmail_cf'});
	system("cd $config{'sendmail_features'}/m4 ; m4 $config{'sendmail_features'}/m4/cf.m4 $config{'sendmail_mc'} 2>/dev/null >$config{'sendmail_cf'} </dev/null");
	&unlock_file($config{'sendmail_cf'});
	&restart_sendmail();
	&webmin_log("build");
	&redirect("");
	}
else {
	# Just show user what would be done
	&header($text{'build_title'}, "");
	print "<hr>\n";

	if (!&has_command("m4")) {
		print "<p>",&text('build_em4', "<tt>m4</tt>"),"<p>\n";
		print "<hr>\n";
		&footer("list_features.cgi", $text{'features_return'});
		exit;
		}

	$temp = &tempname();
	$out = `cd $config{'sendmail_features'}/m4 ; m4 $config{'sendmail_features'}/m4/cf.m4 $config{'sendmail_mc'} 2>&1 >$temp`;
	if ($?) {
		print "<p>",&text('build_ebuild', "<pre>$out</pre>"),"<p>\n";
		print "<hr>\n";
		&footer("list_features.cgi", $text{'features_return'});
		exit;
		}
	if (&has_command("diff")) {
		$diff = `diff $config{'sendmail_cf'} $temp 2>/dev/null`;
		if (!$diff) {
			print "<p>$text{'build_nodiff'}<p>\n";
			print "<hr>\n";
			&footer("list_features.cgi", $text{'features_return'});
			exit;
			}
		}
	
	print "<center><form action=build.cgi>\n";
	print "<input type=hidden name=confirm value=1>\n";
	print &text('build_rusure', "<tt>$config{'sendmail_cf'}</tt>",
		    "<tt>$config{'sendmail_mc'}</tt>"),"<p>\n";
	print $text{'build_rusure2'},"<p>\n";
	print "<input type=submit value='$text{'build_ok'}'>\n";
	print "</form></center>\n";

	if ($diff) {
		print "<b>$text{'build_diff'}</b><p>\n";
		print "<table border><tr $cb><td><pre>",
		      $diff,"</pre></td></tr></table><p>\n";
		}
	unlink($temp);

	print "<hr>\n";
	&footer("list_features.cgi", $text{'features_return'});
	}

