#!/usr/bin/perl 

# Copyright 1999 (C) Paolo Molaro
# This is GPL'ed code.

# This paths match the debian installation, you may need to change them.
# where to put cgi programs
$cgidir = "/usr/lib/cgi-bin";
# where to put configuration files ($etcdir/jitterbug)
$etcdir = "/etc";
# where are the config dir with the sample html files
$configdir = "/usr/share/doc/jitterbug";
# the path to the new_message program
$message_path = "/usr/lib/jitterbug/new_message";

# default values.
$user = 'bugs';
$guest = 'nobody';
$www = 'www-data';
$package = 'Package_name';

require 'dialog.pl';

sub my_die ($){
	rhs_msgbox("Error", shift, 50);
	exit(0);
}

sub my_input ($$$) {
	my ($title, $text, $var) = @_;
	exit(0) unless rhs_inputbox($title, $text, 60, $$var);
	$$var = $dialog_result;
}

rhs_msgbox("Jitterbug configuration", "
	This configuration program is experimental:
	some things may not work right: check the
	documentation in /usr/share/doc/jitterbug.
	", 60);

exit(0) unless rhs_menua("Jitterbug configuration", 
	"Select the Jitterbug configuration option.\n".
	"You need a user that will own the jitterbug files ".
	"it can be the user the web server runs in.", 60,
	#'C' => "Chroot enviroment",
	#'R' => "Binary is setuid root",
	'U' => "Suid bugs user (preferred)",
	'W' => "Bugs user is the user the web server runs in");

$mode = $dialog_result;

if ( $mode eq 'C' ) {
	my_die("Chroot not supported yet");
} elsif ( $mode eq 'U' ) {
	my_input("User configuration", "Insert bugs admin user name", \$user);
} elsif ( $mode eq 'R' ) {
	$user = "root";
} elsif ( $mode eq 'W' ) {
	$user = 'www-data';
} else {
	die "Not reached";
}

$uiduser = $user;
if ( $user =~ /^\d+$/ ) { # uid given
	@user_info = getpwuid($user);
	$user = $user_info[0];
} else {
	@user_info = getpwnam($user);
}

unless (defined($user_info[0])) {
	exit(0) unless rhs_yesno("Error", 
		"User '$uiduser' doesn't exist.\nShould I create it?", 50);
	exit(0) if system("adduser --disabled-password --gecos 'Bug user' $user");
	@user_info = getpwnam($user);
	exit(1) unless @user_info;
}

my_input("Guest user", "Insert the guest username", \$guest);

@guest_info = $guest =~ /^\d+$/ ? getpwuid($guest): getpwnam($guest);
unless (scalar(@guest_info)) {
	my_die("Guest user $guest doesn't exist!");
}

my_input("Web user", "Insert the user the web server runs in", \$www);

@www_info = $www =~ /^\d+$/ ? getpwuid($www): getpwnam($www);
unless (scalar(@www_info)) {
	my_die("Web user $www doesn't exist!");
}

my_input("Package name", 
	"Insert the package name (don't use whitespace in the name)", \$package);

$email = "$user\@`cat /etc/mailname`";
my_input("Email address", 
	"Insert the email address for reporting bugs. ".
	"Mail sent to this address will need to be piped through ".
	"the new_message program. It should be enough to put:\n".
	"|$message_path $package\n".
	"in the .forward or .qmail of $user's home directory or use ".
	"a global alias.\nNote that you have to do this yourself.", \$email);

if ($mode eq "W") {
	$basedir = "/var/jitterbug/$package";
} else {
	$basedir = "$user_info[7]/$package";
}

my_input("Base directory",
	"Enter the directory name to use to store bug reports ".
	"and other info.", \$basedir);

print  "Working...\n", "Creating configuration files for $package in $etcdir/jitterbug...\n";

open(F, ">$etcdir/jitterbug/$package") || my_die( $!);
print F "from address = $email\n";
print F "chroot directory = $chroot\n" if $chroot;
print F "base directory = $basedir\n";
print F "guest uid = $www_info[2]\n";
print F "guest gid = $www_info[3]\n";
print F "uid = $user_info[2]\n";
print F "gid = $user_info[3]\n";
close(F) || my_die ($!);

symlink("$package", "$etcdir/jitterbug/$package.private");

print "Copying default config files (you should edit them) ...\n";
system("mkdir -p $basedir");
system("cp $configdir/config/* '$basedir'");
system("chown -R $user_info[2].$user_info[3] '$basedir'");

print "Copying the binaries in $cgidir...\n";
system("cp $cgidir/jitterbug $cgidir/$package");
system("chown $user_info[2].$www_info[3] $cgidir/$package");
system("chmod 4750 $cgidir/$package");
symlink("$package", "$cgidir/$package.private");

print("Done.\nPress ENTER to continue\n");

<>;

rhs_msgbox("Jitterbug configuration", 
	"You may need to setup your web server to restrict access to
	the private handler. Add something like this to your apache
	configuration:
	<Location /cgi-bin/$package.private>
        AuthType Basic
        AuthName $package
        AuthUserFile /somewhere/apache.auth
        <Limit GET POST>
        require user $package
        </Limit>
        </Location>
	Also, don't forget to add a mail alias to forward bugs to
	/usr/lib/jitterbug/new_message.
	Review also the config files in $basedir.
	", 60);

rhs_msgbox("Jitterbug configuration", "Done", 60);

