#!/usr/bin/perl

$perlpath = "/usr/bin/perl";

# Do not run from web browser
if ($ENV{'REMOTE_ADDR'}) { print "Content-type: text/html\n\n"; die; }

# Check perl version
if ( $] < 5 ) {
    print "\n=======================================\n";
    print "\nYour perl version is below 5\n";
    print "Sorry, but you cannot install meep!Board.\n";
    print "\n=======================================\n";
    exit;
}
else {
    print "perl version looks good...\n\n";
}

print "meep!Board Configuration utility\n--------------------------------\n\n";

# Get present working directory
$path = `pwd`; # full path
chop $path;
$dirname = $path;
@dirname = split(/\//,$dirname);
$dirname = $dirname[$#dirname]; # directory name (use for data dir)

# Get username and password for accessing meep!Board administration tools

print "Enter a username for the meep!Board administration tools: \n";
$username = <STDIN>;
chop $username;

system 'stty', '-echo';

print "Enter a password for the adminstration tools: \n";
$password = <STDIN>;
chop $password;

print "Enter the password again for verification: \n";
$password2 = <STDIN>;
chop $password2;

system 'stty', 'echo';

if ($password ne $password2) {
    print "Passwords do not match\n";
    exit;
}

# create config file
open(CONFIG,">ADMIN/config");

print CONFIG qq(
\$base_url = "baseurl";

\$moderated = 0;

\$main_thread_file = "$path/DATA/main.th";

\$listlength = 20;

\$new_days = 7;
        
sub date_format {
    local(\$time) = \@_;

    (\$sec,\$min,\$hour,\$mday,\$mon,\$year,\$wday,\$yday,\$isdst) = localtime(\$time);
    \$mon++;
    if (\$mon < 10) { \$mon = "0\$mon"; }
    if (\$mday < 10) { \$mday = "0\$mday"; }
    if (\$hour < 10) { \$hour = "0\$hour"; }
    if (\$min < 10) { \$min = "0\$min"; }
    if (\$sec < 10) { \$sec = "0\$sec"; }
    if (\$year > 99) { \$year = \$year - 100; }
    if (\$year < 10) { \$year = "0\$year"; }

    return "\$mday/\$mon/\$year  \$hour:\$min:\$sec";
}

sub is_new {
    local(\$time) = \@_;
    \$now = time;
    if (\$time > \$now - (\$new_days * 24 * 60 * 60)) {
        return 1;
    }
    else {
        return 0;
    }
}

1;
);

close CONFIG;

# create .htaccess file
open(HTACCESS,">ADMIN/.htaccess");

print HTACCESS qq(
AuthUserFile $path/ADMIN/.htpasswd
AuthGroupFile /dev/null
AuthName meep!BoardAdminstration
AuthType Basic

<LIMIT GET>
require user $username
</LIMIT>
		  );

close HTACCESS;

# create .htpasswd file
srand(time|$$);
$r1 = int(rand(26)) + 97;
$r2 = int(rand(26)) + 97;
$salt = pack("cc", $r1, $r2);

open(HTPASSWD,">ADMIN/.htpasswd");
print HTPASSWD "$username:", crypt($password,$salt), "\n";
close HTPASSWD;

# change file permissions
chmod 0755, 'add.cgi', 'list.cgi', 'reply.cgi', 'thread.cgi', 'ADMIN/removepost.cgi', 'ADMIN/trimposts.cgi', 'ADMIN/admin.cgi', 'ADMIN/configure.cgi', 'ADMIN/editpost.cgi', 'ADMIN/redirect.cgi', 'ADMIN/removethread.cgi', 'ADMIN/moderate.cgi', 'ADMIN/firsttime.cgi';
chmod 0711, '.', 'ADMIN', 'TEMPLATES';
chmod 0733, 'DATA';
chmod 0700, 'Configure';
chmod 0644, 'CGI.pm', 'TEMPLATES/add.html', 'TEMPLATES/list.html', 'TEMPLATES/view.html', 'list.lock', 'ADMIN/config.lock';
chmod 0666, 'ADMIN/config', 'DATA/main.th', 'DATA/main.th.bak', 'ADMIN/config.bak';

# change path to perl if necessary
if ($perlpath ne "/usr/bin/perl") {

    $esc_perlpath = $perlpath;
    $esc_perlpath =~ s/\//\\\\\//g;

    $call = "$perlpath -p -i -e \"s/^#!.*/#!$esc_perlpath/;\" *.cgi ADMIN/*.cgi";

    system $call;
}	

print "\n\nConfiguration of your meep!Board is almost complete.\n";
print "Now, open up a web browser and go to the URL corresponding to\n";
print "ADMIN\/firsttime.cgi\n";
