#!/usr/bin/perl

# Unix install script for Lyris
# Copyright 1997-1999 Walter Shelby Group Ltd.

# $Log: install_lyris,v $
# Revision 1.30  2000/11/13 23:39:01  jeffrey
# fixed startup script names in installer for FreeBSD
#
# Revision 1.29  2000/11/13 22:55:13  jeffrey
# Added startup scripts for FreeBSD, changed Lyris to ListManager throughout script
#
# Revision 1.28  2000/11/03 21:22:03  jeffrey
# added FREEBSD to installer, removed obsolete OSes
#
# Revision 1.27  2000/11/03 21:10:55  jeffrey
# fixed error in lyris.pid message
#
# Revision 1.26  2000/10/17 00:42:19  jeffrey
# added espanol and portugues to installer
#
# Revision 1.25  2000/07/10 18:57:53  jeffrey
# Fixed spelling and grammar
#
# Revision 1.24  2000/06/30 01:00:44  jeffrey
# ended the clobbering of index.html, lyris.plc and conf files
#
# Revision 1.23  2000/06/28 05:51:42  jeffrey
# added log statement in an attempt to get comments added to the file
#

select STDOUT;
$| = 1;

MAIN {
    # uncomment if you want to run this script in test mode, where the actions are echoed to the
    # screen, and not actually done
    #$test = 1;
    $failures = 0;
    $warnings = 0;
    $platform = "unix";

    $uname_text = uc &shellout("uname -s","uname failure","");
    if ($uname_text =~ /FREEBSD/) {
        $unix_platform = "FREEBSD";
    }
    elsif ($uname_text =~ /SUNOS/) {
        $unix_platform = "SOLARIS";
    }
    elsif ($uname_text =~ /LINUX/) {
        $unix_platform = "LINUX";
    }
    else {
        print "Sorry, but the command 'uname -s' reported an unknown platform type: $uname_text\n\nContact Lyris technical support at support\@shelby.com for help.\n\n";
        exit;
    }

    $lyr_dir = '/opt/lyris';
    $lyr_dir = '/usr/local/lyris' if ($unix_platform =~ /LINUX|FREEBSD/);
    

    $installation_log = "#########################\n# ListManager installation log\n#########################\n\n";
    $un_installer = "#!/bin/sh\n\n##############################\n# ListManager uninstallation program\n##############################\n\ncd ../..\necho Removing ListManager from your system...\n\n";

    &DefinePlatformSpecifics;
    &DefineHelp;
    &CheckRunningAsRoot;

    &DoTheyWantToStart;
    &SystemChecks;
    
    &CheckUpgrade;
    &CopyLyrisFiles; # do dbupgrade here
    if ($is_upgrade == 0) {
	&CreateOrUpgradeLyrisDb;
    }
    if ($unix_platform =~ /FREEBSD/) {
       &AddFreeBSDStartupScript($lyris_auto_startup_freebsd, 
                      $lyris_freebsd_start_stop_script, 
                      "lyris");
    } else {
      &AddStartupScript($lyris_auto_startup1, 
                      $lyris_start_stop_script, 
                      "lyris");
    }
    &InstallWebInterface;
    &WriteInstallationLog;
    
    if ($is_upgrade != 0 ) {
        &RunLyrisCommand("checkserial"); # check whether the serial number is valid, or needs to be converted into an activation code
        &PageBreak;
        print $lyris_setup_done1;

        $reminder_readme1 =~ s/LYR_DIR/$lyr_dir/g;
        $reminder_readme1 =~ s/HOSTNAME/$HOSTNAME/g;
        $reminder_readme1 =~ s/ADMIN_PASS/$admin_pass/g;
        if ($unix_platform =~ /FREEBSD/) {
           $reminder_readme1 =~ s/S96lyris/lyris.sh/g;
           $reminder_readme1 =~ s/S96apache-lyris/apache-lyris.sh/g;
        }
        $reminder_readme2 =~ s/LYR_DIR/$lyr_dir/g;
        $reminder_readme2 =~ s/HOSTNAME/$HOSTNAME/g;
        $reminder_readme2 =~ s/ADMIN_PASS/$admin_pass/g;
        $reminder_readme2 =~ s/README/UPGRADE_NOTES/g;
        open(README, ">$lyr_dir/UPGRADE_NOTES");
        print README $reminder_readme1, $reminder_readme2;
        close README;
        # set the readme file to 600, so only root can read it.
        chmod 0600, "$lyr_dir/UPGRADE_NOTES";

        # Show the readme info
        print $reminder_readme1;
        &ReturnToContinue;
        print $reminder_readme2;
        &ReturnToContinue;
    }
    else {
        &WriteUnInstallationProgram;
        &PageBreak;
        &PageBreak;
        print $lyris_setup_done2;

        # fetch the hostname
        my $HOSTNAME = get_hostname();

        # Modify the README info, and dump it to a file
        $reminder_readme1 =~ s/LYR_DIR/$lyr_dir/g;
        $reminder_readme1 =~ s/HOSTNAME/$HOSTNAME/g;
        $reminder_readme1 =~ s/ADMIN_PASS/$admin_pass/g;      
        if ($unix_platform =~ /FREEBSD/) {
           $reminder_readme1 =~ s/S96lyris/lyris.sh/g;
           $reminder_readme1 =~ s/S96apache-lyris/apache-lyris.sh/g;
        }
        $reminder_readme2 =~ s/LYR_DIR/$lyr_dir/g;
        $reminder_readme2 =~ s/HOSTNAME/$HOSTNAME/g;
        $reminder_readme2 =~ s/ADMIN_PASS/$admin_pass/g;
        open(README, ">$lyr_dir/INSTALLATION_NOTES");
        print README $reminder_readme1, $reminder_readme2;
        close README;

        # set the readme file to 600, so only root can read it.
        chmod 0600, "$lyr_dir/INSTALLATION_NOTES";

        # Show the readme info
        print $reminder_readme1;
        &ReturnToContinue;
        print $reminder_readme2;
        &ReturnToContinue;
    }

    # do language setting if new installation
    if ($is_upgrade == 0) {
	&SetLanguage;
    }

    # check if the user would like to 
    if( &YesOrNo($start_lyris_now) eq 'y' ) {
        if ($unix_platform =~ /FREEBSD/) {
           &Execute("$lyr_dir/bin/lyris.sh start");
           &Execute("$lyr_dir/bin/apache-lyris.sh start");
        } else {
           &Execute("$lyr_dir/bin/S96lyris start");
           &Execute("$lyr_dir/bin/S96apache-lyris start");
        }
    }

    print $lyris_setup_done3;
    <STDIN>;
    exit;
};


sub WriteInstallationLog {
    open (INSTALL_LOG, ">$lyr_dir/install.log");
    print INSTALL_LOG $installation_log;
    close(INSTALL_LOG);

}

sub WriteUnInstallationProgram {
    &UnInstaller("");
    &UnInstaller("echo ListManager has been removed from this system");
    open (UNINSTALLER, ">$lyr_dir/uninstall_lyris_now");
    print UNINSTALLER $un_installer;
    close(UNINSTALLER);
    chmod 0755, "$lyr_dir/uninstall_lyris_now";
}

sub UnInstaller {
    $un_installer .= $_[0]."\n";
}

sub CopyLyrisFiles {
    &PageBreak;
    if ($is_upgrade == 0) { print $dir_lyris_new; }
    else { print $dir_lyris_up; }

    my $good_choice = 0;
    my $default_dir = $lyr_dir;
    my $cur_dir = `pwd`;
    chomp($cur_dir);

    while($good_choice == 0) {
	$lyr_dir = &PickDir($lyr_dir);
	if ($cur_dir eq $lyr_dir) {
	    print "\nYou must choose a directory other than the current directory\n\n";
	    $lyr_dir = $default_dir;
	} else {
	    $good_choice = 1;
	}
    }

    if ($is_upgrade == 1) {
	# check if they have upgrade code and so on
	&shellout("$cp_program{$platform} ./bin/lyris $lyr_dir/bin/lyristmp");
	my $uptestresults = &shellout("$lyr_dir/bin/lyristmp dbupgradetest");
	my $upcode = ""; # set this later by user

	# 3 possibilities, no act, no upgrade, or upgrade ok
	if ($uptestresults =~ /activation/) {
	    print $needs_activation_code;
	    &shellout("rm $lyr_dir/bin/lyristmp");
	    &ExitInstallation;
	} elsif ($uptestresults !~ /ok/) {

	    print $needs_upgrade_code;
		my $validcode = 0;
		while (!$validcode) {
		    print "Please enter your upgrade code, or Q to exit\n--->";
		    $upcode = <STDIN>;
		    chomp($upcode);
		    if ($upcode eq "Q" || $upcode eq "q") {
			&shellout("rm $lyr_dir/bin/lyristmp");
			&ExitInstallation;
		    } else {
			my $retcode = &shellout("$lyr_dir/bin/lyristmp dbupgradetest $upcode");
			if ($retcode =~ /invalid/) {
			    # Lyris did not like the upgrade code. 
	    		    print $invalid_upgrade_code;
			} else {
			    $validcode = 1;
			}
		    }
		} # while not a valid upgrade code
	} # tested dbupgrade test with no code

	# now have valid upcode, or don't need one
	print "\nNow upgrading your database\n";
	my $retcode = &shellout("$lyr_dir/bin/lyristmp dbupgrade $upcode");
	if ($retcode !~ /Upgraded/) {
	    print "Dbupgrade returned the following error message: \n\n$retcode\n";
	    &shellout("rm $lyr_dir/bin/lyristmp");
	    &ExitInstallation;
	}
	&shellout("rm $lyr_dir/bin/lyristmp");
	print "\nDatabase has been upgraded\n";
    }

# back to normal path
    # save files that we don't want overwritten: e.g. doc/index.html, lyris.plc, apache/conf
    my $confsave = 0;
    my $plcsave = 0;
    my $indexsave = 0;

    # backup the conf files
    if ( -e "$lyr_dir/apache/conf") {
	$confsave = 1;
	$run_this = $mkdir_program{$platform}."  /tmp/lyristmpfiles";
	&Execute($run_this);
	$run_this = $cp_program{$platform}." $lyr_dir/apache/conf /tmp/lyristmpfiles";
	&Execute($run_this);
    }

    # backup index.html
    if ( -e "$lyr_dir/apache/docs/index.html") {
	$indexsave = 1;
	if (!$confsave) {
	    $run_this = $mkdir_program{$platform}."  /tmp/lyristmpfiles";
	    &Execute($run_this);
	}
	$run_this = $cp_program{$platform}." $lyr_dir/apache/docs/index.html /tmp/lyristmpfiles";
	&Execute($run_this);
    }

    # backup lyris.plc
    if ( -e "$lyr_dir/apache/lib/lyris.plc") {
	$plcsave = 1;
	if (!$confsave && !$indexsave) {
	    $run_this = $mkdir_program{$platform}."  /tmp/lyristmpfiles";
	    &Execute($run_this);
	}
	$run_this = $cp_program{$platform}." $lyr_dir/apache/lib/lyris.plc /tmp/lyristmpfiles";
	&Execute($run_this);
    }

    # copy the files
    $run_this = $cp_program{$platform}." . $lyr_dir";
    &Execute($run_this, 1);

    # restore existing files, if necessary
    if ($confsave == 1) {
	$run_this = $cp_program{$platform}." /tmp/lyristmpfiles/conf $lyr_dir/apache";
	&Execute($run_this);
    }
    if ($indexsave == 1) {
	$run_this = $cp_program{$platform}." /tmp/lyristmpfiles/index.html $lyr_dir/apache/docs";
	&Execute($run_this);
    }
    if ($plcsave == 1) {
	$run_this = $cp_program{$platform}." /tmp/lyristmpfiles/lyris.plc $lyr_dir/apache/lib";
	&Execute($run_this);
    }

    if ($confsave || $indexsave || $plcsave) {
	$rm_program = &find_prog("rm");
	$run_this = $rm_program." -rf /tmp/lyristmpfiles";
	&Execute($run_this);
    }

    # make symlinks for the lyris perl modules
    $run_this = "ln -fs $lyr_dir/apache/cgi-bin/lyrispath.pm $lyr_dir/scripts/";
    &Execute($run_this, 1);

    # make sure lyris.pl is executable
    $run_this = "chmod 755 $lyr_dir/apache/cgi-bin/lyris.pl";
    &Execute($run_this, 1);

    # modify lyrispath.pm to have correct info
    if (!$test) {
	my $lyrpath = "$lyr_dir/apache/cgi-bin/lyrispath.pm"; 
	open(LYRPATH,$lyrpath) || die("can't open file $lyrpath for reading");
	my @lyrpathdata = <LYRPATH>;
	close(LYRPATH);
	open(LYRPATH,">$lyrpath") || die("can't open file $lyrpath for writing");
	my $lyrline;
	my $lyrlibdir = "$lyr_dir/apache/lib";
	foreach $lyrline (@lyrpathdata) {
	    $lyrline =~ s/#unshift\(\@INC,\"Insert Lyris Lib directory here/unshift(\@INC,\"$lyrlibdir/;
	    print LYRPATH $lyrline;
	}
	close(LYRPATH);
    }

    &UnInstaller("# Remove Lyris ListManager directory");
    &UnInstaller("echo I will now remove the ListManager installation directory.  This will remove");
    &UnInstaller("echo the '$lyr_dir' directory, and everything below it.");
    &UnInstaller("echo ''");
    &UnInstaller('echo "Do you want to remove the directory? (y/n)"');
    &UnInstaller('read x');
    &UnInstaller('while [ "x$x" != "xy" -a "x$x" != "xn" ] ; do');
    &UnInstaller('      echo "remove the directory? (y/n)"');
    &UnInstaller('      read x');
    &UnInstaller('done');
    &UnInstaller('if [ "x$x" = "xy" ]; then ');
    &UnInstaller("    echo 'removing $lyr_dir'");
    &UnInstaller("    rm -rf $lyr_dir");
    &UnInstaller('else ');
    &UnInstaller('    echo "not removing $lyr_dir"; ');
    &UnInstaller('fi');
    &UnInstaller("");
}

# create blank Lyris databases, or upgrade existing databases
sub CreateOrUpgradeLyrisDb {

    # ask if they want to create databases
    &PageBreak;
    if (&YesOrNoOrHelp($lyr_db1, $lyr_db_help) eq 'n') {
    	$admin_pass = "lyris";
        &ReturnToContinue($lyr_db2);
        return;
    }


    # ask them for an administrator password
    &PageBreak;
    print $admin_pass1;
    $admin_pass = "";
    $admin_pass = &DefaultEntry("");
    while ( $admin_pass eq "") {
        print "\nPlease specify an administrator password.\n";
        $admin_pass = &DefaultEntry("");
    }

    # create Lyris databases with admin password
    print "Creating ListManager databases in directory: ".$lyr_dir.$dir_sep{$platform}."bin".$dir_sep{$platform}."db\n";
    &RunLyrisCommand("dbcreate $admin_pass");


    # set the DNS server for Lyris.  (NOTE: the ip addresses were determined above)
    &RunLyrisCommand("dns $dns_servers");


    # save TCP/IP addresses to listen to
    &PageBreak;
    print $lyr_ip_only1;
    $ip_only = &DefaultEntry("*");
    if ($ip_only > 0) {
        # add localhost if they forgot it
        if (index($ip_only, "127.0.0.1") == -1) {
            $ip_only .= " 127.0.0.1";
        }
        &RunLyrisCommand("tcpip $ip_only");
    }

    # save SMTP port setting
    &PageBreak;
    print $smtp_port_setting2;
    $smtp_port_setting = &DefaultEntry("25");
    if (($smtp_port_setting > 0) && ($smtp_port_setting != 25)) {
        &RunLyrisCommand("smtpport $smtp_port_setting");
    }

    # save admin's email address
    &PageBreak;
    print $lyr_admin_email1;
    $admin_email = &DefaultEntry("");
    if (length($admin_email) > 0) {
        &RunLyrisCommand("email $admin_email");
    }

    &PageBreak;

    return;
}

# the FreeBSD handles the startup/shutdown scripts in a sufficiently
# different manner as to justify having its own procedure for 
# installing them

sub AddFreeBSDStartupScript {
   my ($query, $script, $scriptname) = @_;

    # Write out the customized startup script
    my $startup_script = $script;
    $startup_script =~ s/LYR_DIR/$lyr_dir/g;
    open(STARTUP, ">$lyr_dir/bin/$scriptname.sh");
    print STARTUP $startup_script;
    close(STARTUP);
    chmod 0755, "$lyr_dir/bin/$scriptname.sh";

    my $rc_start_dir = "";
    $rc_start_dir = "/usr/local/etc/rc.d" if (-d "/usr/local/etc/rc.d");

        # return if no directory could be found
    if( $rc_start_dir eq "") {
        # let the user know they will have to copy the script themselves.
        my $message = $no_rc_dir;
        $message =~ s/SCRIPTNAME/$scriptname.sh/g;
        $message =~ s/LYR_DIR/$lyr_dir/g;
        ReturnToContinue($message);
        return;
    }

    # modify the query, to list the actual startup dir
    my $newquery = $query;
    $newquery =~ s/RC_START_DIR/$rc_start_dir/g;
    $newquery =~ s/SCRIPTNAME/$scriptname.sh/g;

    # copy the startup script to the appropriate directory
    if(&YesOrNo($newquery) eq 'y') {

       # remove old copy of startup script
       my $run_this="rm -f $rc_start_dir/$scriptname.sh";
       &Execute($run_this, 1);

       # copy new script into place
       $run_this = $cp_program{$platform}.
            " $lyr_dir/bin/$scriptname.sh $rc_start_dir/$scriptname.sh";
        &Execute($run_this, 1);

        # add instructions to the uninstaller
        &UnInstaller("# Remove ListManager startup script");
        &UnInstaller("rm -f $rc_start_dir/$scriptname.sh");

        &UnInstaller("");
    }

}

sub AddStartupScript {
    my ($query, $script, $scriptname) = @_;

    # Write out the customized startup script
    my $startup_script = $script;
    $startup_script =~ s/LYR_DIR/$lyr_dir/g;
    open(STARTUP, ">$lyr_dir/bin/S96$scriptname");
    print STARTUP $startup_script;
    close(STARTUP);
    chmod 0755, "$lyr_dir/bin/S96$scriptname";

    # figure out the current run level
    my $level = ""; 
    if( $unix_platform =~ /LINUX/ ) {
        my $foo;
        ($foo,$level) = split(/\s+/, `runlevel`);
    } elsif($unix_platform =~ /SOLARIS/) {
        $level = 2;
    } elsif($unix_platform =~ //) {
    }



    # Figure out where to put the startup script
    my $rc_start_dir = "";
    $rc_start_dir = "/etc/rc${level}.d" if (-d "/etc/rc${level}.d");
    $rc_start_dir = "/etc/rc.d/rc${level}.d" if (-d "/etc/rc.d/rc${level}.d");

    my $rc_stop_dir = "";
    $rc_stop_dir = "/etc/rc0.d" if (-d "/etc/rc0.d");
    $rc_stop_dir = "/etc/rc.d/rc0.d" if (-d "/etc/rc.d/rc0.d");

    my $initd_dir = "";
    $initd_dir = "/etc/init.d" if ( -d "/etc/init.d" );
    $initd_dir = "/etc/rc.d/init.d" if ( -d "/etc/rc.d/init.d" );
  
    # return if no directory could be found
    if( $rc_start_dir eq "" || $initd_dir eq "" || $rc_stop_dir eq "" ) {
        # let the user know they will have to copy the script themselves.
        my $message = $no_rc_dir;
        $message =~ s/SCRIPTNAME/S96$scriptname/g;
        $message =~ s/LYR_DIR/$lyr_dir/g;
        ReturnToContinue($message);
        return;
    }

    # modify the query, to list the actual startup dir
    my $newquery = $query;
    $newquery =~ s/INITD_DIR/$initd_dir/g;
    $newquery =~ s/RC_START_DIR/$rc_start_dir/g;
    $newquery =~ s/RC_STOP_DIR/$rc_stop_dir/g;
    $newquery =~ s/SCRIPTNAME/$scriptname/g;

    # copy the startup script to the appropriate directory
    if(&YesOrNo($newquery) eq 'y') {

        my $run_this = $cp_program{$platform}.
            " $lyr_dir/bin/S96$scriptname $initd_dir/$scriptname";
        &Execute($run_this, 1);
        &UnInstaller("# Remove ListManager startup script");
        &UnInstaller("rm -f $initd_dir/$scriptname");

        $run_this="rm -f $rc_start_dir/S96$scriptname";
        &Execute($run_this, 1);
        $run_this="ln -fs $initd_dir/$scriptname $rc_start_dir/S96$scriptname";
        &Execute($run_this, 1);
        &UnInstaller("rm -f $rc_start_dir/S96$scriptname");

        $run_this="rm -f $rc_stop_dir/K96$scriptname";
        &Execute($run_this, 1);
        $run_this="ln -fs $initd_dir/$scriptname $rc_stop_dir/K96$scriptname";
        &Execute($run_this, 1);
        &UnInstaller("rm -f $rc_stop_dir/K96$scriptname");
        
        &UnInstaller("");
    }
}

sub CheckRunningAsRoot {
    return if($> == 0);

    &ReturnToContinue($must_install_as_root);
    &ExitInstallation;
}

sub CheckFileDescriptors {
    my $should_exit = $_[0];

    print "\nChecking file descriptors....\n";

    $descriptor_count = &shellout("ulimit -n","unable to determine number of file descriptors",0);
    
    print "Note: your maximum descriptor count is currently set to: $descriptor_count\n";
    if (($descriptor_count > 149) || ($descriptor_count =~ /unlimited/)){
        print "check succeeded\n\n";
        return;
    }

    if ($unix_platform =~ /SOLARIS/) {

        # if solaris, append two lines to /etc/system to increase maximum file descriptors
        &PageBreak;
        if (&YesOrNo($descriptor_increase1.$descriptor_increase1b) eq 'y') {
            $command = $echo_program{"unix"}." 'set rlim_fd_max=0x900' >>/etc/system";
            &Execute($command, 1);
            $command = $echo_program{"unix"}." 'set rlim_fd_cur=0x300' >>/etc/system";
            &Execute($command, 1);
            &PageBreak;
            &ReturnToContinue($descriptor_increase2);
        }
        else {
            if ($should_exit eq 'exit-if-not-enough-descriptors') {
                &ExitInstallation;
            }
            elsif (&YesOrNo($descriptor_ask_exit) eq 'y') {
                    &ExitInstallation;
            }
        }
    }
    elsif ($unix_platform =~ /LINUX/) {
        # if other, simply give helpful information
        &PageBreak;
        &ReturnToContinue($descriptor_increase1.$descriptor_increase1d);
        if ($should_exit eq 'exit-if-not-enough-descriptors') {
            &ExitInstallation;
        }
        elsif (&YesOrNo($descriptor_ask_exit) eq 'y') {
            &ExitInstallation;
        }
    }
    else {
        # if other, simply give helpful information
        &PageBreak;
        &ReturnToContinue($descriptor_increase1.$descriptor_increase1c);
        if ($should_exit eq 'exit-if-not-enough-descriptors') {
            &ExitInstallation;
        }
        elsif (&YesOrNo($descriptor_ask_exit) eq 'y') {
            &ExitInstallation;
        }
    }
    
}

# install Web server CGI programs for Lyris interface
sub InstallCGI {

    my $cgidefault = "";

    &PageBreak;
    my $ynresp = &YesOrNoOrHelp($shell_cgi1, $lweb_help1);

    if ($ynresp eq 'n') {
        &PageBreak;
        &ReturnToContinue($lweb_cgi_help);
        return;
    }

    my $cgimaybe = "";
    $cgimaybe = '/home/httpd/cgi-bin' if ($unix_platform =~ /LINUX/);

    # try to guess default cgi-bin
    if (-d $cgimaybe) {
	$cgidefault = $cgimaybe;
    }
    # ask for the CGI directory
    print "\nPlease enter your Web server's CGI-BIN directory.\n";
    my $CGI = &PickDir($cgidefault,$no_cgidir_create);


    $run_this = $cp_program{$platform}." apache/cgi-bin/lyris.pl $CGI/.";
    &Execute($run_this, 1);
    $run_this = "chmod 755 $CGI/lyris.pl";
    &Execute($run_this, 1);

    $run_this = $cp_program{$platform}." apache/cgi-bin/lyrispath.pm $CGI/.";
    &Execute($run_this, 1);

    # modify lyrispath.pm to have correct info
    if (!$test) {
	my $lyrpath = "$CGI/lyrispath.pm"; 
	open(LYRPATH,$lyrpath) || die("can't open file $lyrpath for reading");
	my @lyrpathdata = <LYRPATH>;
	close(LYRPATH);
	open(LYRPATH,">$lyrpath") || die("can't open file $lyrpath for writing");
	my $lyrline;
	my $lyrlibdir = "$lyr_dir/apache/lib";
	foreach $lyrline (@lyrpathdata) {
	    $lyrline =~ s/#unshift\(\@INC,\"Insert Lyris Lib directory here/unshift\(\@INC,\"$lyrlibdir/;
	    print LYRPATH $lyrline;
	}
	close(LYRPATH);
    }
    
    # modify lyris.pl slightly -> "use lyrispath" -> "use lyrispath; use lib "$lyr_dir/apache/lib"
    if (!$test) {
	my $lyrpath = "$CGI/lyris.pl"; 
	open(LYRPATH,$lyrpath) || die("can't open file $lyrpath for reading");
	my @lyrpathdata = <LYRPATH>;
	close(LYRPATH);
	open(LYRPATH,">$lyrpath") || die("can't open file $lyrpath for writing");
	my $lyrline;
	my $lyrlibdir = "$lyr_dir/apache/lib";
	foreach $lyrline (@lyrpathdata) {
	    $lyrline =~ s/use lyrispath;/use lyrispath;\n    use lib \"$lyrlibdir\";/;
	    print LYRPATH $lyrline;
	}
	close(LYRPATH);
    }

    
    &UnInstaller("# Remove ListManager CGI scripts");
    &UnInstaller("rm -f $CGI/lyris.pl");
    &UnInstaller("rm -f $CGI/lyrispath.pm");

    &UnInstaller("");

    return;
}

# copy the GIFs that the Lyris web interface uses
sub InstallLyrisGraphics {

    #  wrong answer, we do need to copy stuff
    #	
    # no need to copy graphics if this is an upgrade
    #if ($is_upgrade != 0 ) {
    #    return;
    #}

    &PageBreak;

    if (&YesOrNoOrHelp($web_gif1, $lweb_help1) eq 'n') {
        &PageBreak;
        &ReturnToContinue($web_gif2);
        return;
    }

    # try to find default doc root
    my $docrootdef = "";

    if ($unix_platform !~ /SOLARIS/) {
	if ( -f "/home/httpd/htdocs/index.html") {
	    $docrootdef = "/home/httpd/htdocs";
	} elsif ( -f "/home/httpd/html/index.html") {
	    $docrootdef = "/home/httpd/html";
	}
    }

    # ask for web server document root directory
    print "\nPlease enter your Web server's document root directory.\n";
    my $DocRoot = &PickDir($docrootdef,$no_htmldir_create);

    $command = $mkdir_program{$platform}." ".$DocRoot."/lyris";
    $retval = &Execute($command);

    $run_this = $cp_program{$platform}." apache/docs/lyris/*gif $DocRoot/lyris/.";
    &Execute($run_this, 1);
    &UnInstaller("# Remove ListManager graphics from your web server");
    &UnInstaller("rm -rf $DocRoot/lyris/");
    &UnInstaller("");

    return;
}


# perform sanity checks on the state of their system.
sub SystemChecks {
    print $starting_system_checks;
    &ReturnToContinue; 

    # check if the umask is correct
    my $umask = umask();
    if ($umask != 022) {
       my $question = sprintf "Warning: your umask is %#o, you should probably reset it to 022. \n(type \"umask 022\" on the command line).\n\nDo you wish to continue anyway?\n\n", $umask;
       if( &YesOrNo($question) eq 'n' ) {
          exit(0);
       }
    }
   

    &LyrisRunning;

    # check DNS settings
    &PageBreak;
    &get_dns_server();
    &CheckDns();

    &PageBreak;
    &CheckFileDescriptors('ask-user-about-exit');

    print $finished_system_checks;
}

# determine if Lyris is already running, and shut down
sub LyrisRunning {
    my $pidfile = "";
    if ($unix_platform =~ /SOLARIS/) {
	$pidfile = "/etc/lyris.pid";
    } elsif ($unix_platform =~ /LINUX/) {
	$pidfile = "/var/run/lyris.pid";
    }
    if ( -f $pidfile) { # lyris is probably running
	my $lyrpid = `cat $pidfile`;
	chomp($lyrpid);

	    my @procs = `ps -p $lyrpid`;
	    if ((join('',@procs)) =~ /lyris/i) {
		print "Please shut down Lyris ListManager before running this script\n";
		&ExitInstallation;
	    }
	
    }
}

# print the initial welcome message
sub DoTheyWantToStart {
    if (&YesOrNoOrHelp("LYRIS LISTMANAGER INSTALLATION PROGRAM FOR ".$unix_platform.$start_q1, $start_help) eq 'n') { &ExitInstallation; }
    &PageBreak;
}

# ask if the person wants to run this installation program
sub CheckUpgrade {

    $answer = &YesOrNo($is_upgrade_text, $up_help);

    $is_upgrade = 0;
    if ($answer eq 'n') {
        &PageBreak;
        $is_upgrade = 1;
        &PageBreak;
        &ReturnToContinue($upgrade_hint1);
        return;
    }
    else {
        if (&YesOrNoOrHelp($start_q2, $start_help2) eq 'n') { &ExitInstallation; }
        &PageBreak;
        &display_sendmail_coexistence_help;
        return;
    }
}

sub display_sendmail_coexistence_help {
    if (&YesOrNo($lyris_continue_help1) ne 'y') { &ExitInstallation("");; }
}

# exits the installation program
sub ExitInstallation {
    print $exit_now;
    exit;
}


# install the web interface
sub InstallWebInterface {
    &PageBreak;
    
    # Install the included web server
    &InstallWebServer;

    # should we use an existing web server in addition to the included one?
    my $installedCGI = 0;
    if(&YesOrNo($use_cgi) eq 'n') {
        &InstallCGI;
        &InstallLyrisGraphics;
        $installedCGI = 1;
    }

    if($installedCGI || &YesOrNo($use_perl_utils) eq 'y') {
        &InstallPerl;
    }

}


# make modifications to mod_perl files for this platform
sub InstallWebServer {
    &PageBreak;

    # make sure the local conf file exists, and set the HTTP port
    if( $is_upgrade == 0 || ! -e "$lyr_dir/apache/conf/local.conf" ) {
        my $def_port = "80";

        # Get the port number for the Lyris web server
        select STDOUT;
        $| = 1;
        print $web_server_intro;
        
        my $port = "";
        while ($port !~ /^\d+$/) {
            print "    Invalid port." if($port ne "");
            print "    server port [$def_port]  ";
            $port = <STDIN>;
            chomp($port);
            $port="80" if($port eq "");
        }
        
        # Write out httpd.conf 
        open(TEMPLATE,"<$lyr_dir/apache/conf/local.conf.tmpl");
        open(CONF,">$lyr_dir/apache/conf/local.conf");
        
        my $temp_rs = $/;
        undef $/;  $file = <TEMPLATE>;  $/ = $temp_rs;
        $file =~ s/HTTPD_PORT/$port/g;
        print CONF $file;
        close TEMPLATE;
        close CONF;
    } 

    # set up the startup script

    if ($unix_platform =~ /FREEBSD/) {
       &AddFreeBSDStartupScript($web_auto_start_freebsd, 
                      $web_freebsd_start_stop_script, 
                      "apache-lyris");
    } else {
    &AddStartupScript($web_auto_start, 
                      $web_start_stop_script, 
                      "apache-lyris");
    }
}

# install and configure perl
sub InstallPerl {
    &PageBreak;

    $perl_installed = 0;
    $PerlPath = "/usr/bin";
    if (-f "/usr/bin/perl") {
        my $perl_ver = `/usr/bin/perl -v`;
        my ($maj_num) = ($perl_ver =~ /version ([\d]*)\./);
        my ($min_num) = ($perl_ver =~ /version \d*\.(\d*)/);
        $maj_num = 0 + $maj_num;
        $min_num = 0 + $min_num;

        # actually look for 5.004 or newer
        if ($maj_num > 5 || ($maj_num == 5 && $min_num >= 4)) {
            $perl_installed = 1;
            print $perl_is_installed1;
        }
        else {
            if (&YesOrNo($no_perl2) eq 'n') {
                &PageBreak;
                &ReturnToContinue($perl_self_install);
                return;
            }
        }
    }
    else {
        if (&YesOrNo($no_perl1) eq 'n') {
            &PageBreak;
            &ReturnToContinue($perl_self_install);
            return;
        }
    }


    if ($perl_installed == 0) {
        # ask for the perl directory
        print "\nEnter the directory where you would like Perl installed.\n";
        $PerlPath = &PickDir($perl_dir{$platform});
    
        # make a link to the lyris perl executables
        $run_this = "ln -s $lyr_dir/bin/perl $PerlPath/perl";
        &Execute($run_this, 1);

        # make link between /usr/bin/perl and real location of perl, if didn't install to /usr/bin
        if ($PerlPath ne "/usr/bin") {
            $run_this = "ln -s $PerlPath/perl /usr/bin/perl";
            &Execute($run_this, 1);
        }
    }

    # ask for the perl library directory
    $PerlLibPath = $perl_lib_dir{$platform};
    $lib_path = `/usr/bin/perl -e 'print join(\"\\n\",\@INC)' `;
    @lib_paths = split(/\n/, $lib_path);

    my @tmppath = ($lib_path =~ m!(/.*site_perl.*)\b!g);
    $PerlLibPath = $tmppath[0];
    foreach $p (@tmppath) {
        $PerlLibPath = $p  if length($p) < length($PerlLibPath);
    }

    if ($PerlLibPath eq "" && length($lib_paths[0]) > 0) {
        $PerlLibPath = $lib_paths[0];
        print "\nPossible Perl library directories are:\n$lib_path\n";
    }

    print "\nPlease enter the path to your Perl library directory\n";
    my $PerlLibPath = &PickDir($PerlLibPath);

    # copy perl libraries
    $run_this = "ln -s $lyr_dir/apache/lib/Exporter.pm $PerlLibPath/.";
    &Execute($run_this, 1);
    $run_this = "ln -fs $lyr_dir/apache/lib/SelfLoaderMP.pm $PerlLibPath/.";
    &Execute($run_this, 1);
    $run_this = "ln -s $lyr_dir/apache/lib/Carp.pm $PerlLibPath/.";
    &Execute($run_this, 1);

    $run_this = "ln -fs $lyr_dir/apache/lib/lyrislib.pl $PerlLibPath/.";
    &Execute($run_this, 1);

    my $oldpath = "/usr/local/lib/perl5";
    if ( -l  "$oldpath/lyrislib.pl") { # link to legacy version of lyrislib
	$run_this = "rm -f $oldpath/lyrislib.pl";
	&Execute($run_this, 1);
	$run_this = "ln -fs $lyr_dir/apache/lib/lyrislib.pl $oldpath/.";
	&Execute($run_this, 1);
    } elsif ( -f  "$oldpath/lyrislib.pl") { # legacy version of lyrislib
	my $count = 0;
	my $done = 0;

	while (!$done) {
	    my $filename = sprintf "lyrislib%.03d",$count;
	    if ( ! -f "$oldpath/$filename") {
		print "Notice: moving old lyrislib.pl to $filename in directory $oldpath\n";
		$run_this = "mv -f  $oldpath/lyrislib.pl $oldpath/$filename";
		&Execute($run_this, 1);
    		last;
	    }
	    $count ++;
	}
	$run_this = "ln -fs $lyr_dir/apache/lib/lyrislib.pl $oldpath/.";
	&Execute($run_this, 1);
    }


    $run_this = "ln -fs $lyr_dir/apache/lib/lyrlang* $PerlLibPath/.";
    &Execute($run_this, 1);
    $run_this = "ln -fs $lyr_dir/apache/lib/lyrispg.pl $PerlLibPath/.";
    &Execute($run_this, 1);

    if ( -l  "$oldpath/lyrispg.pl") { # link to legacy version of lyrispg
	$run_this = "rm -f $oldpath/lyrispg.pl";
	&Execute($run_this, 1);
	$run_this = "ln -fs $lyr_dir/apache/lib/lyrispg.pl $oldpath/.";
	&Execute($run_this, 1);
    } elsif ( -f  "$oldpath/lyrispg.pl") { # legacy version of lyrispg
	my $count = 0;
	my $done = 0;

	while (!$done) {
	    my $filename = sprintf "lyrispg%.03d",$count;
	    if ( ! -f "$oldpath/$filename") {
		print "Notice: moving old lyrispg.pl to $filename in directory $oldpath\n";
		$run_this = "mv -f  $oldpath/lyrispg.pl $oldpath/$filename";
		&Execute($run_this, 1);
    		last;
	    }
	    $count ++;
	}
	$run_this = "ln -fs $lyr_dir/apache/lib/lyrispg.pl $oldpath/.";
	&Execute($run_this, 1);
    }



    $run_this = "ln -fs $lyr_dir/apache/lib/lyris.plc $PerlLibPath/";
    &Execute($run_this, 1);

    $run_this = "ln -fs $lyr_dir/apache/cgi-bin/lyrispath.pm $PerlLibPath/";
    &Execute($run_this, 1);

    # only add uninstalls for the non-standard pieces
    &UnInstaller("rm -f $PerlLibPath/lyrispg.pl");
    &UnInstaller("rm -f $PerlLibPath/lyrislib.pl");
    &UnInstaller("rm -f $PerlLibPath/lyrlang*");
    &UnInstaller("rm -f $PerlLibPath/SelfLoaderMP.pm");
    &UnInstaller("rm -f $PerlLibPath/lyris.plc");
    &UnInstaller("rm -f $PerlLibPath/lyrispath.pm");

    return;
}





#############################################################
# Do sanity checks on the DNS settings, return the number of tests
# that fail.
#############################################################
sub CheckDns {


    # Ask the user if they want to allow the DNS checks
    if( &YesOrNo($allow_dns_check) eq 'n' ) {
        return;
    }

    #
    # get easy info about the machine
    #
    my @ips = get_ips();
    my $hostname = get_hostname(); 

    select STDOUT;
    $|=1;

    #
    # Sanity check the hostname 
    # 

    # hostname has corresponds to an IP address
    print "\n";
    print "CHECKING HOSTNAME\n";

    print "    checking address of $hostname....";
    my $addr = lookup($hostname);
    print $addr, "\n";

    # check for errors
    if($addr eq "failure") {
        $failures++;
	$addr = "";
        print "\n  **ERROR: The IP address for $hostname could not be found.\n";
        print "    You must correct this for Lyris ListManager to function properly\n\n";
    }
    
    # continue checking
    else {
        # hostname has valid reverse mapping

        print "    checking reverse mapping of $addr....";
        my $name = lookup($addr,$hostname);
        print $name, "\n";

        # check for errors
        if($name eq "failure") {
	    $warnings++;
#            $failures++;
#            print "\n  **ERROR: could not do a reverse lookup on $addr.\n";
            print "\n  **WARNING: could not do a reverse lookup on $addr.\n";
            print "    Some mail servers with anti-spam filters may not accept mail \n";
            print "    from Lyris ListManager unless you correct this.  You can try to use\n";
            print "    \"lyris helotext $hostname\" on the command line to get\n";
	    print "    around this problem.\n\n";
        }

        else {
            # verify that the hostname and the reverse lookup match
            if( lc($hostname) ne lc($name) ) {
                print "\n  **WARNING: your hostname ($hostname) does not match \n";
                print "    the name entry for your IP address ($name)\n\n";
#                $failures++;
                $warnings++;
            }

            # as long as we found SOME ip addresses through ifconfig, check
            # that the hostname mapped to one of them.
            if(@ips > 0 ) {
                my $found = 0;
                foreach (@ips) {
                    $found = 1 if($addr eq $_);
                }
                
                if(!$found) {
                    print "\n  **WARNING: the IP address for $hostname does not match\n";
                    print "    any of this machine's addresses (as shown by " .
                        "'ifconfig -a')\n\n";
                    $failures++;
                }
            }
        }
    }

    #
    # check the machine's IP addresses
    #
    print "\n";
    print "CHECKING IP ADDRESSES\n";
    my $ip;
    foreach $ip (@ips) {
        # skip the hostname, since we already checked that one
        if($ip eq $addr) {
            print "    address $ip already checked\n";
            next;
        }

        # address has valid reverse mapping

        print "    checking reverse mapping of $ip....";
        my $name = lookup($ip);
        print $name, "\n";

        # check for errors
        if($name eq "failure") {
            $warnings++;
	    $name = "";
            print "\n  **WARNING: could not find a valid hostname for $ip.\n";
            print "    Some mail servers with anti-spam filters may not accept mail \n";
            print "    from Lyris unless you correct this.  You can try to use\n";
            print "    \"lyris helotext $hostname\" on the command line to get\n";
	    print "    around this problem.\n\n";
        }

        else {
            # check the forward lookup for the ip address

            print "    checking address of $name....";
            my $newip = lookup($name,$ip);
            print $newip, "\n";

            # check for errors
            if($newip eq "failure") {
                $warnings++;
               print "\n  **ERROR: Your DNS failed to find an address for $name.\n";
               print "    Some mail servers with anti-spam filters may not accept mail \n";
               print "    from ListManager unless you correct this.  You can try to use\n";
               print "    \"lyris helotext $hostname\" on the command line to get\n";
	       print "    around this problem.\n\n";
            }

            else {
                # verify that the hostname and the reverse lookup match
                if($ip ne $newip ) {
                    print "\n  **WARNING: your hostname ($hostname) does not match \n";
                    print "    the name entry for your IP address ($name)\n\n";
                    $failures++;
                }
            }
        }
    }

    print "\n";


    # check to see if Lyris notices the problem, and force an exit if it does.
    $descriptor_count = `PATH=/sbin:/bin:/usr/bin bin/lyris maxfiles 2>&1`;
    if( $descriptor_count =~ /hostname.*invalid/i ) {
        if($failures == 0) {
            print $descriptor_count;
        }
        print $dns_failed;
        &ExitInstallation;
    }

    # If there were failures, ask the user if they want to proceed
    if( $failures > 0 ) {
        my $text = $dns_failure_warning;
        $text =~ s/NN/$failures/;
        print $text;

        if(&YesOrNo("Exit and fix the errors now? (recommended)\n\n") eq 'y'){
            &ExitInstallation;
        }
    }

    # print a happy message - everything was successful
    else {
        print "ALL TESTS SUCCESSFUL\n";
	if ($warnings == 1) {
	    print "There was 1 warning.\n";
	} elsif ($warnings > 1) {
	    print "There were $warnings warnings.\n";
	}
    }

}


#############################################################
# Get the IP addresses on which this machine is listening
#############################################################
sub get_ips {

    # run ifconfig
    my $prog = find_prog("ifconfig");
    my $ifconfig = `$prog -a`;
    return () if($? != 0);

    # parse the output of ifconfig
    my @tmp = ();
    @tmp = ($ifconfig =~ /inet.*?(\d+\.\d+\.\d+\.\d+)/g );

    # strip out localhost
    my @addrs = ();
    foreach (@tmp) {
        push(@addrs, $_) unless /127.0.0.1/;
    }

    return @addrs;
}

#############################################################
# Figure out the host name
#############################################################
sub get_hostname () { 
    my $domainname;

    my $hostname = &shellout("hostname","cannot determine the host name","failure");
    chomp($hostname);
    $failures++ if ($hostname eq "failure");

    if ($unix_platform =~ /LINUX/) {
	$domainname = &shellout("dnsdomainname","cannot determine the DNS domain name","failure");
    } else {
	$domainname = &shellout("domainname","cannot determine the DNS domain name","failure");
    }

    chomp($domainname);


    if ($domainname eq "failure") {
	$failures++;
	$domainname = "";
    }

    # combine the host and domain into a coherent whole, if domainname
    # is defined, and not a subset of hostname.
    $hostname = "$hostname.$domainname" 
        if ($domainname ne ""  &&  index($hostname,$domainname) == -1);
    return $hostname;
}

#############################################################
# Use a safe shelling out to prevent segmentation faults
#############################################################
sub shellout {
    my ($command,$errmsg,$errret) = @_;
    my $file = "/tmp/Xlyris_tmpX";
    my $rc = system("$command > $file 2>&1");
    my @value;

    $rc >>= 8;
    if ($rc != 0) {
#	print "\n  **ERROR: $errmsg\n";
	print "\n  **WARNING: $errmsg\n";
	unlink($file);
#	$failures++;
	$warnings++;
	return $errret;
    }

    open(LYRTMP,$file) || return;
    @value = <LYRTMP>;
    close (LYRTMP);
    unlink($file);

    return join ("",@value);
#    return $value;
}

#############################################################
# use resolver library calls to look up a hostname 
#############################################################
sub liblookup ($) {
    my $target = shift;
    my ($name,$aliases,$addrtype,$length,@addr);

    
    # do an initial name-based lookup
    ($name,$aliases,$addrtype,$length,@addr) = gethostbyname($target);
    return "failure" if($name eq "");

    # if we were looking up by hostname, just return the address
    return join('.', unpack('C4',$addr[0]))
        if($target !~ /^\d+\.\d+\.\d+\.\d+$/);

    # otherwise, we were looking up based on address.  In this case,
    # the "name" field is useless (it's just the address in text
    # format), so we now do an address-based lookup.
    define_socket_vars();
    ($name,$aliases,$addrtype,$length,@addr) = 
        gethostbyaddr($addr[0], $lyris::AF_INET);
    return "failure" if($name eq "");
    return lc($name);
}




#############################################################
# Define variables used by socket routines
#############################################################
sub define_socket_vars { 
    # get the operating system
    my $os = $^O;

    # set variables, based on the OS
    if ($os =~ /linux/i || $os =~ /win/i || $os =~ /aix/i) {
        $lyris::AF_INET = 2;
        $lyris::SOCK_STREAM = 1;
    } 
    elsif ($os =~ /solaris/i) {
        $lyris::AF_INET = 2;
        $lyris::SOCK_STREAM = 2;
    } 
    else {
        #warn "Unrecognized OS - guessing at AF_INET and SOCK_STREAM values\n";
        $lyris::AF_INET = 2;
        $lyris::SOCK_STREAM = 1;
    }
}

#############################################################
# Find the specified program by searching first in the path and then
# checking other standard places..
#############################################################
sub find_prog {
    my $prog = shift;
    
    # Add dirs from the PATH
    my @dirs = split(/:/, $ENV{'PATH'});

    # add some standard directories (NOTE: these are at the end)
    push @dirs, '/bin', '/usr/bin', '/usr/local/bin', '/sbin', '/usr/sbin', '/usr/local/sbin', '/etc', '/usr/ucb';

    # Search the directories
    my $d;
    foreach $d (@dirs) {
        return "$d/$prog" if( -x "$d/$prog" );
    }

    # return undef if the program was not found
    return undef;
}



#############################################################
# Do a DNS lookup, and return the name & address
#############################################################
sub lookup ($$) {
    my $target = shift;
    my $expected = shift;

    # try nslookup first
    my @ret = nslookup($target);

    # if that fails, try using the resolver library via perl
    @ret = liblookup($target) if($ret[0] eq "failure");

    # If we received a list of responses, try to return the expected one.
    foreach (@ret) {
        return $expected if($_ eq $expected);
    }

    # check that the response looks like a full host name or IP address
    return "failure" if($ret[0] !~ /\..*\./);

    # otherwise, just return the first response
    return $ret[0];
}


#############################################################
# use nslookup to look up a host name
#############################################################
sub nslookup ($) {
    my $target = shift;

    # return failure if we previously had trouble finding nslookup
    return "failure" if($nslookup_prog eq "failure");

    $nslookup_prog = find_prog("nslookup");

    return "failure" if( !defined $nslookup_prog );

    my $server = get_dns_server();

    # run nslookup to get info.


#    my $out = &shellout("$nslookup_prog $target $dns_servers 2>/dev/null","nslookup failure","");
    my $out = &shellout("$nslookup_prog $target $dns_servers","nslookup failure","");

    my @lines = split(/\n/, $out);
    shift @lines; shift @lines;  # ditch the server name and address
    shift @lines; 

    my (@addr,@name);
    foreach (@lines) {
        if( /^addr/i ) {
            $_ =~ s/^addr.*?\s+//i;             # strip of header info
            push @addr, split(/[\s,]+/, $_);    # split the address list
            next;
        }
        
        if( /^name/i ) {
            $_ =~ s/^name.*?\s+//i;             # strip of header info
            push @name, split(/[\s,]+/, $_);    # split the address list
            next;
        }

        if( /^alias/i ) {
            $_ =~ s/^alias.*?\s+//i;            # strip of header info
            push @name, split(/[\s,]+/, $_);    # split the address list
            next;
        }
    }
    
    # return if nslookup doesn't work
    if(@addr == 0 || @name == 0) {
	print "\n    ran command \"$nslookup_prog $target $dns_servers\"\n\n";
	return "failure";
    }

    # figure out whether to return the name or the address
    return @name if($target =~ /^\d+\.\d+\.\d+\.\d+$/);
    return @addr;
}


#############################################################
# figure out DNS server
#############################################################
sub get_dns_server () {

    if($dns_servers eq "") {
        $dns_servers = "127.0.0.1";
        $resolv_conf = `cat /etc/resolv.conf`;
        $resolv_conf =~ /^\s*nameserver (.*)#?.*/;
        if ($1 > 0) {
            $dns_servers = $1;
        }
        print $lyr_dns1;
        $dns_servers = &DefaultEntry($dns_servers);
    }

    if($primary_dns eq "") {
        ($primary_dns) = ($dns_servers =~ /^\s*(\S+)/);
    }

    return $primary_dns;
}





sub RunLyrisCommand {
    $lyris_executable = "$lyr_dir".$dir_sep{$platform}."bin".$dir_sep{$platform}."lyris";

    if ($test != 1) {
        if (!(-x $lyris_executable)) {
            &PageBreak;
            print "AN ERROR HAS OCCURRED\n\nThe Lyris ListManager executable could not be found at\n$lyris_executable\n\nListManager Setup is unable to continue.\n";
            &ExitInstallation;
        }
    }
    $command = "PATH=/sbin:/bin:/usr/bin $lyris_executable ".$_[0];

    return &Execute($command);
}
# ask the user to press return to continue
sub ReturnToContinue {
    if (length($_[0]) > 0) {
        print $_[0];
    };

    print "Press return to continue.";
    <STDIN>;
    print "-------------------------\n";
    return;
}

# run a given command
sub Execute {
    my $command = $_[0];
    my $quiet = $_[1];
    if ($test != 1) {
        #if ($quiet != 1) {
        #    $command .= " >NUL";
        #}

        print "Running command: \"$command\"\n";
        $installation_log .= "# Running command: \"$command\"\n";
        $installation_log .= "$command\n";
        my $retval = system($command);
        if ($retval != 0) {
            print "Tried to run: $command\n";
            print "Command failed (return value: $retval)\n\n";
            $installation_log .= "# Above command failed (return value: $retval)\n";
            return $retval;
        }
        $installation_log .= "\n";
        return 0;
    }
    else {
        print "Test mode, would have run: $command\n";
        return 0;
    }
}

# ask yes or no
sub YesOrNo {
    my $mess = $_[0];

    my $in = "";
    while (($in ne 'n') && ($in ne 'y')) {
        &PageBreak;
        print $mess;
        print "Please answer (Y)es or (N)o\n-->";
        $in = <STDIN>;
        $in = lc(substr($in,0,1));
    }
    return $in;
}

# ask yes or no or give help
sub YesOrNoOrHelp {
    my $mess = $_[0];
    my $help = $_[1];

    my $in = "";
    while (($in ne 'n') && ($in ne 'y')) {
        &PageBreak;
        print $mess;
        print "Please answer (Y)es, (N)o or (H)elp\n-->";
        $in = <STDIN>;
        $in = lc(substr($in,0,1));
        if ($in eq 'h') {
            &PageBreak;
            print $help."\n";
            &ReturnToContinue("");
        }
    }
    return $in;
}

# get some entry text, or return for a default value
sub PickDir {

    my $default = shift;
    my $create_directory_message = shift;

    # If no create_directory_message was passed in, use a default
    $create_directory_message = $no_dir_create 
        if($create_directory_message eq "");

    my $DirPicked;
    my $exit_now = 0;
    while ($exit_now == 0) {
        print "enter a directory name";
        if (length($default) > 0) {
            print ", or press return for \"".$default."\"";
        }
        print "\n-->";
        $in = <STDIN>;
        $in = &TrimDir($in);
        if (length($in) > 0) {
            $DirPicked = $in;
        }
        else {
            $DirPicked = $default;
        }

        if (length($DirPicked) > 0) {
            # check if directory exists, if it doesn't, ask to create it
            if (-d $DirPicked) {
                return $DirPicked;
            }
            else {
                if (&YesOrNo($create_directory_message) eq 'n') {
                    print "\n";
                }
                else {
                    $command = $mkdir_program{$platform}." ".$DirPicked;
                    $retval = &Execute($command);
                    if ($retval == 0) {
                        return $DirPicked;
                    }
                    else {
                        print "\nUnable to create directory\n";
                    }
                }
            }
        }
    }
}

# get some entry text, or return for a default value
sub DefaultEntry {

    my $default = $_[0];
    if (length($default) > 0) {
        print "or press return for \"".$default."\"\n";
    }
    print "-->";
    $in = <STDIN>;
    $in = &Trim($in);
    if (length($in) > 0) {
        return $in;
    }
    else {
        return $default;
    }
}

# print a pagebreak
sub PageBreak {
    print "\n-------------------------------------------------------------------------------\n\n";
}

# removes whitespace at the beginning and end of a string
sub Trim {
    local $tmp = $_[0];
    $tmp =~ s/[\s]+$//g;
    $tmp =~ s/^[\s]+//g;
    return($tmp);
}

# change the current directory to the initial directory of this script
sub CdHome {
    chdir($current_dir);
    &Execute($command);
}

# trim whitespace and a pending slash if the user entered one.  Used when asking for directories.
sub TrimDir {
    local $tmp = $_[0];
    $tmp = &Trim($tmp);
    $tmp =~ s/\/$//;
    $tmp =~ s/\\$//;
    return $tmp;
}

sub DefinePlatformSpecifics {
    # directory separator character
    $dir_sep{"windows"} = "\\";
    $dir_sep{"unix"} = '/';

    # copy program
    $cp_program{"windows"} = 'copy';
    $cp_program{"unix"} = 'cp -rf';

    # make directory program
    $mkdir_program{"windows"} = 'mkdir';
    $mkdir_program{"unix"} = 'mkdir -p';

    # change directory program
    $chdir_dir{"windows"} = 'cd';
    $chdir_dir{"unix"} = 'cd';

    # get current directory program
    $cur_dir_program{"windows"} = 'cd';
    $cur_dir_program{"unix"} = 'pwd';

    # perl install program
    $perl_install_program{"windows"} = 'perlinst.bat';
    $perl_install_program{"unix"} = '';

    # default perl directory
    $perl_dir{"windows"} = 'c:\perl5';
    $perl_dir{"unix"} = '/usr/bin';

    # default perl library directory
    $perl_lib_dir{"windows"} = 'c:\perl5\lib';
    $perl_lib_dir{"unix"} = '/opt/lib/perl5';

    $echo_program{"windows"} = 'echo';
    $echo_program{"unix"} = 'echo';

    # determine the current directory
    $command = $cur_dir_program{$platform};
    $current_dir = `$command`;
    $current_dir = &TrimDir($current_dir);

}

sub SetLanguage {
    my @languages = qw(english deutsch francais italiano dansk magyar espanol portugues);

    my $default_language = $languages[0];
    my $lang_choices = join " ",@languages;

    my $lang_loop_done = 0;
    my $LangPicked = $default_language;

    while(! $lang_loop_done) {

	print "\nPlease enter your language of choice for the web interface\n";
	print "Choices are one of \"$lang_choices\"\n";
    
	print "or press return for $default_language\n";
	
	print "\n-->";
        $in = <STDIN>;
        $in = &Trim($in);
        if (length($in) > 0) {
            $LangPicked = $in;
        }

	# verify language was one of those chosen
	my $lang;
	foreach $lang(@languages) {
	    if ($LangPicked eq $lang) {
		$lang_loop_done = 1;
		last;
	    }
	}
	if (!$lang_loop_done) {
	    print "\nError, language entered not one of the available choices\n";
	}
    }    
    
    # now set up the lyris.plc file

    return if ($LangPicked eq $default_language); # default language already enabled
    
    open(PLC,"$lyr_dir/apache/lib/lyris.plc") or warn "can't open $lyr_dir/apache/lib/lyris.plc";

    my @plc = <PLC>;
    
    close PLC;
    open(PLC,">$lyr_dir/apache/lib/lyris.plc") or warn "can't open $lyr_dir/apache/lib/lyris.plc";

    foreach (@plc) {
	s/language=english/language=$LangPicked/;
	print PLC $_;
    }
    close PLC;
    
    return;
}


sub DefineHelp {

#############################################################################

$start_help = <<'END';
Lyris ListManager consists of three components:
  1) the ListManager server
  2) the ListManager web interface
  3) perl utility scripts for interacting with ListManager


1) The ListManager Server will be copied to a directory you will specify and
will run from that directory.


2) The ListManager web interface can be installed in one of two modes:

  a) The ListManager installation includes a web server that has been
     optimized speed up access to the ListManager web interface.  Installing
     the included web server will require no changes to your system
     configuration files.  This is the recommended installation
     method.

  b) You can also optionally install the Lyris web interface for use
     with an existing web server in addition to the included web
     server.  This requires making the following changes to your
     existing web server:
       * Various graphics files (GIFs) will be copied to a directory
         named ~/lyris off of your HTML document root directory.
       * Several Perl source files will be copied to your Web Server's
         CGI-BIN directory.
       * If you do not have Perl 5.004 or newer installed on your system,
         it will be installed for you.
       * Several Perl library files will be copied to your Perl library
         directory. 

3) The lyris/scripts directory contains several perl scripts that can be
useful for interacting with Lyris.  These expect to find perl
installed as /usr/bin/perl.  If perl is installed elsewhere, you can
run the scripts by typing "perl SCRIPTNAME".  If perl is not currently
installed, the installation script can install it for you.

END

#############################################################################

$start_help2 = <<'END';
In order for Lyris ListManager to be able to send and receive mail
at all times, we recommend that ListManager run on a computer
with a full time TCP/IP connection. If you do not have a full time
TCP/IP connection, you can still use ListManager, but outsiders will likely
be frustrated by the delay in getting answers back from ListManager, and 
by the fact that your ListManager Web Interface is not dependably accessible.

ListManager uses a web interface for all administration.  The ListManager web 
interface can optionally be located on a different computer than ListManager. 
Thus, it is not a requirement that a web server be running on the same
computer as ListManager. However, this does complicate the installation
process, and so we recommend that first-time users have a web server on their
ListManager server machine to simplify initial setup.  Refer to the ListManager
manual for information about locating the ListManager web interface on a separate 
computer. You will need to make a single line change in the "lyris.plc" 
configuration file.

The ListManager installation program may need root access in order to copy 
certain files.  No root actions will be made without your express permission, 
and all actions can be easily undone.

END

#############################################################################

$start_q1 = <<'END';


  This program can install Lyris ListManager on your machine, or upgrade an 
  existing ListManager installation.

  By proceeding with this installation program, you certify that you agree to
  the terms of the software license agreement contained in the LICENSE file.

  A script named "uninstall_lyris_now" will be created during the installation
  process, which will cleanly remove ListManager from your system, if you later
  decide that you want to remove ListManager from this computer.

Do you want to continue?
END

#############################################################################

$start_q2 = <<'END';
  In order for Lyris ListManager to be installed on this machine, you must:

    * Be currently logged into an account with root access.
    * Have a web browser available to you (not necessarily on this machine)
    * Have a full time TCP/IP connection.
    * Have at least 10 megs of free hard disk space.

Do you want to continue?
END

#############################################################################

$exit_now = <<'END';

  Exiting the Lyris ListManager installation program.

END

#############################################################################

$dir_lyris_new = <<'END';
  Please choose the directory where you would like Lyris ListManager installed.  
  ListManager should be installed in its own subdirectory.

END

#############################################################################

$dir_lyris_up = <<'END';
  Please specify directory where Lyris ListManager is currently installed.

END

#############################################################################

$is_upgrade_text = <<'END';
  Is this a new installation of Lyris ListManager?

  --> YES: A new installation of ListManager
  --> NO: you are upgrading an existing installation of ListManager.

END

#############################################################################

$up_help = <<'END';
If this is your first time installing Lyris ListManager, or you are unsure, 
choose YES.

If you already have ListManager installed and want to upgrade it to this 
version, choose NO.  All your existing data will be preserved, and a backup 
will be made.  You should make sure that ListManager is not currently running.  
You can do this by running the command "telnet localhost 2020" and making 
sure the connection is refused.
END

#############################################################################

$no_dir_create = <<'END';
  That directory does not exist.  Do you want to create it?

END

#############################################################################

$no_cgidir_create = <<'END';
  The CGI-BIN directory you specified does not exist.  In most cases,
  this directory should already be there, so it is likely that you
  mis-typed the directory name.

  Do you want to install the Lyris ListManager web interface files in this
  directory?  (it will be created for you if you answer "Yes")

END

#############################################################################

$no_htmldir_create = <<'END';
  The document root directory you specified does not exist.  In most
  cases, this directory should already be there, so it is likely that
  you mis-typed the directory name.

  Do you want to install the Lyris ListManager web interface graphics in 
  this directory?  (it will be created for you if you answer "Yes")

END

#############################################################################

$shell_cgi1 = <<'END';
Lyris Setup needs to copy several files into your Web Server's CGI-BIN
directory in order to install the Lyris ListManager Web Interface.

Would you like to do this now?

END

#############################################################################

$shell_cgi2 = <<'END';
Please specify the location of your web server's CGI-BIN directory:
END

#############################################################################

$lweb_help1 = <<'END';
The Lyris ListManager Web Interface is a Perl script which runs on a web server.  
It uses TCP/IP to communicate with the ListManager server.  The ListManager Web 
Interface does not have to be located on the same machine as ListManager. 
However, you must install the ListManager Web Interface *somewhere* so that you 
can create and administer your mailing lists.  For most sites, it is easiest to 
have a web server on the same machine as the ListManager server.

NOTE: the "server_ip_address=" line in the file "lyris.plc" tells the Web
Interface where the ListManager server is located. This file is located with the
other CGI-BIN files.

To install the ListManager Web Interface, ListManager setup will copy several 
Perl (.pl) files to your web server's CGI-BIN directory.  If Perl 5.004 or newer 
does not exist on your machine, it will be installed.  Several Perl library files
(.pm) will be copied to your Perl library directory. Finally, several graphic
files (.gif) will be copied to a ~/lyris directory in your HTML document
root directory.

END

#############################################################################

$lweb_cgi_help = <<'END'; 
You will need to manually copy files from the ~/lyris/apache/cgi-bin
directory to your web server's CGI-BIN directory.

END

#############################################################################

$web_gif1 = <<'END';
The ListManager web interface requires that several GIF graphics files be copied
to your Web Server's HTML document directory.

Would you like to install these files now?

END

#############################################################################

$web_gif2 = <<'END';
You will need to manually copy the GIF files from the ~/apache/docs/lyris
directory into a /lyris directory off of your Web server document root
directory.

END

#############################################################################

$lyr_db1 = <<'END';
Lyris ListManager uses a number of database files for storing information. 
These files are necessary in order for Lyris to function and contain all the 
information ListManager uses (mailing lists, messages, members, etc).

Would you like to create these now?

END

#############################################################################

$lyr_db2 = <<'END';
You will need to run "lyris dbcreate" to create these databases by hand,
in order for Lyris ListManager to work correctly.

END

#############################################################################

$lyr_dns1 = <<'END';
Lyris ListManager needs the TCP/IP address of at least one DNS server in 
order to be able to delivery email.

ListManager also has the ability to automatically load balance between any 
number of DNS servers.  So, if you have several DNS servers available to 
you, please specify them here, separating each TCP/IP address with a space.
This will also buy you additional reliability, in case one of your DNS
servers fails.

Please enter the TCP/IP address of your DNS server(s).
END

#############################################################################

$lyr_ip_only1 = <<'END';
By default, ListManager will listen to all the TCP/IP addresses assigned to the
hostname of this machine.

If you want to restrict ListManager to only listen to specific TCP/IP addresses,
please list them here, with each TCP/IP address separated by a space.

Please enter the tcp/ip addresses to listen to,
END

#############################################################################

$lyr_admin_email1 = <<'END';
Please enter your email address, or the email address of the ListManager
administrator.

END

#############################################################################

$smtp_port_setting2 = <<'END';
By default, ListManager listens for incoming email on port 25.  If you would 
like to change this, enter a new port number now.
END
#############################################################################

$lyris_auto_startup_freebsd = <<'END'; 
Would you like Lyris ListManager to automatically start when your
system boots?

If you answer "Yes", a script named 'SCRIPTNAME' will be placed in your
RC_START_DIR directory.

NOTE: If you answer "No", you will need to start ListManager by 
hand each time your system reboots.

END

#############################################################################

$lyris_auto_startup1 = <<'END'; 
Would you like Lyris ListManager to automatically start when your
system boots?

If you answer "Yes", a script named 'SCRIPTNAME' will be placed in your
INITD_DIR directory, with symbolic links to start and stop the 
the server in RC_START_DIR and RC_STOP_DIR respectively.

NOTE: If you answer "No", you will need to start ListManager by 
hand each time your system reboots.

END

#############################################################################

$lyr_db_help = <<'END';
Lyris ListManager stores all its information in database files stored in a 
~/db directory under the mail ~/lyris directory.  The files use the standard 
FoxPro file format, and can be read by any program which can read and write 
FoxPro files.

The first time you install ListManager, you need to create the blank database 
files which will hold its information. Once these files have been created, 
ListManager will use them to store all its information.  If you want to backup, 
restore, or move ListManager to another machine, you need only worry about the 
files in the ~/lyris/db directory -- that's where all the ListManager information 
about your site is stored.

END

#############################################################################

$upgrade_help1 = <<'END';
In order to use your existing Lyris ListManager data, your ListManager databases 
need to be upgraded to the latest format.  The command "lyris dbupgrade" does 
this.

It is important that the ListManager Server *not* be current running in the
background during this operation.  If it is running, please stop it.

All your existing information will be kept -- nothing will lost.

A backup of your data will be saved to the ~/lyris/bin/dbup directory.

Do you want to upgrade your databases now?
END
#############################################################################

$upgrade_40 = <<'END';
If you are upgrading from Lyris ListManager v3.0 or v3.5 to v4.0, would you 
like to type in your new activation code now?

END

#############################################################################

$upgrade_hint1 = <<'END';
We recommend that you have made a complete backup of your ~/lyris directory
before upgrading.

While it is very unlikely that there will be a problem in upgrading your
ListManager installation, your data is important, and it makes good sense to 
make a backup before any major change.

END

#############################################################################

$admin_pass1 = <<'END';
Please choose an administrator password.

You will need to use this password when using the ListManager Web Interface
to access the Server Administrator menus.

Please choose a password
END

#############################################################################

$no_perl1 = <<'END';
You do not appear to have Perl 5.004 or newer installed on your system.

ListManager Setup looked for the file /usr/bin/perl and did not find it.

The ListManager Web Interface requires Perl 5.004 or newer.

You can either choose to install Perl yourself or have ListManager Setup 
install it for you. The Perl software is available at www.perl.com.

Would you like to have ListManager Setup install Perl for you?
END

#############################################################################

$perl_is_installed1 = <<'END';
Good, you already have Perl 5.004 or newer installed on your system.

ListManager Setup will need to copy some Perl library files in order for the
ListManager Web Interface to work.
END

#############################################################################

$no_perl2 = <<'END';
The version of Perl on your system, at /usr/bin/perl, does not appear to be
Perl version 5.004 or newer.  It appears that you have an old version of
Perl.

The ListManager Web Interface requires Perl 5.004 or newer.

You can either choose to install Perl yourself or have Lyris Setup install
it for you. The Perl software is available at www.perl.com.

Would you like to have ListManager Setup install Perl for you?
END

#############################################################################

$perl_self_install = <<'END';
You will need to manually install Perl on your system, and then copy the
files from the /lyris/perl/lib directory into your Perl library in
order for the ListManager Web Interface to work.

END

#############################################################################

$descriptor_increase1 = <<'END';
You currently have fewer than 150 file descriptors allocated on your system.

ListManager requires at least 150 descriptors in order to work reliably while
sending email.  File descriptors are used during TCP/IP sessions, and
thus a low number of file descriptors limits concurrent mail send sessions.

END

#############################################################################

$descriptor_increase1b = <<'END';
To increase the number of maximum file descriptors on a Solaris system, 
the following two lines must be added to /etc/system and your system must 
be rebooted.  This change will not adversely affect any other programs on
your system.

Append to the /etc/system

  set rlim_fd_max=0x900
  set rlim_fd_cur=0x300

Would you like to have ListManager Setup add these lines to /etc/system for 
you?
END

#############################################################################

$descriptor_increase1c = <<'END';
Many versions of Unix, such as IRIX and HPUX allow you to change the 
descriptor limit on the fly, by using the "ulimit -n ###" command, like so:

  ulimit -n 300

In such a situation, simply make sure that you run this ulimit command
before your start Lyris ListManager.

If you type the command right now: 

  ulimit -n 300; ulimit -n
  
you should receive the number '300'.  After doing this, you can proceed
with the ListManager installation.

END

#############################################################################

$descriptor_increase1d = <<'END';
Linux allows you to change the file descriptor limit on the fly, by using 
the "ulimit -n ###" command, like so (if you are using the bash shell):

  ulimit -n 300

In such a situation, simply make sure that you run this ulimit command
before your start Lyris ListManager.

If you type the command right now: 

  ulimit -n 300; ulimit -n
  
you should receive the number '300'.  After doing this, you can proceed
with the ListManager installation.

Also, if you have a license for a high performance version of ListManager, 
you should consider increasing the maximum number of system file descriptors
by modifying /proc/sys/fs/file-max.

END

#############################################################################

$descriptor_increase2 = <<'END';
The lines for increasing the file descriptor limit have been appended
to the file named "/etc/system"

You will need to reboot your machine in order for this change to take
effect.  Until you reboot, ListManager will not be able to run.

END

#############################################################################

$descriptor_ask_exit = <<'END';

Your current file descriptor settings are too low.  We recommend that
you exit the installation program, and fix this problem.  

Would you like to exit now?

--> YES, exit now, to increase the number of descriptors 
--> NO, continue with the installation (you will need to increase the
    number of file descriptors later)

END

#############################################################################

$lyris_setup_done1 = <<'END';
Your Lyris ListManager system has now been upgraded!

END

#############################################################################

$lyris_setup_done2 = <<'END';
Lyris ListManager has now been installed on your system!

END

#############################################################################

$lyris_setup_done3 = <<'END';

Lyris ListManager setup has completed.

Please press return to exit.

END

#############################################################################

$lyris_continue_help1 = <<'END';
Before you install Lyris ListManager, it is important that you have decided 
what method you want to use to have ListManager coexist with your existing
mail server.

Please read the file "readme.html" for information about this.


Do you want to install ListManager now?

--> YES, proceed with installation program
--> NO, I want to exit, and read the instructions

END

#############################################################################

$lyris_default_start_stop_script = <<'END';
#!/bin/sh

# Lyris ListManager startup script,
# Place this in /etc/rc2.d (or elsewhere, depending on Unix platform)

state=$1

case $state in

'start')                
        # Set high file descriptors, and then start ListManager in the background

        if [ -f /etc/lyris.pid ] ; then
            echo "/etc/lyris.pid already exists: ListManager may already be running"
        else 
          if [ -f /var/run/lyris.pid ] ; then
            echo "/var/run/lyris.pid already exists: ListManager may already be running"
          else 
            ulimit -n 5000 
            PATH=/sbin:/bin:/usr/bin LYR_DIR/bin/lyris -bd 
          fi
        fi

        ;;
'stop')
        if [ -f /etc/lyris.pid ] ; then
            kill -15 `cat /etc/lyris.pid`
        else if [ -f /var/run/lyris.pid ] ; then
            kill -15 `cat /var/run/lyris.pid`
            fi
        fi
        ;;
*)
        echo "Usage: $0 { start | stop }"
        ;;
esac

END


#############################################################################

$lyris_redhat_start_stop_script = <<'END';
#!/bin/sh
#
# ListManager   This shell script takes care of starting and stopping
#               ListManager.
#
# chkconfig: 35 96 96
# description: Lyris ListManager is a mailing list manager package.  For \
#              documentation and other information, visit www.lyris.com.
# processname: lyris
# pidfile: /var/run/lyris.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -x LYR_DIR/bin/lyris ] || exit 0

# See how we were called.
case "$1" in
  start)
    if [ -f /var/run/lyris.pid ] ; then
        echo "/var/run/lyris.pid already exists: Lyris ListManager may already be running"
    else 
        echo -n "Starting Lyris ListManager: "
        ulimit -n 3072  # more file descriptors
        PATH=/sbin:/bin:/usr/bin daemon LYR_DIR/bin/lyris -bd
        echo
        touch /var/lock/subsys/lyris
    fi
    ;;
  stop)
    echo -n "Stopping ListManager: "
    killproc lyris -TERM
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/lyris
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  status)
    status lyris
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac
exit 0

END


#Choose which start/stop script to use
if( -f "/etc/redhat-release" ) {
    $lyris_start_stop_script = $lyris_redhat_start_stop_script;
} else {
    $lyris_start_stop_script = $lyris_default_start_stop_script;
}

#############################################################################

$lyris_freebsd_start_stop_script = <<'END';
#!/bin/sh
#
# ListManager   This shell script takes care of starting and stopping
#               Lyris ListManager.
#
# description: ListManager is a mailing list manager package.  For \
#              documentation and other information, visit www.lyris.com.
# processname: lyris
# pidfile: /var/run/lyris.pid

#! /bin/sh 
# ListManager startup script for FreeBSD
#

pidfile=/var/run/lyris.pid

case "$1" in
start)
		 if [ -f $pidfile ] && kill -0 `cat $pidfile` 2> /dev/null ; then
		 		 echo "$pidfile already exists: Lyris ListManager may already be running"
		 else 
		 		 echo "Starting Lyris ListManager"
		 		 ulimit -n 3072  # more file descriptors
		 		 PATH=/sbin:/bin:/usr/bin LYR_DIR/bin/lyris -bd
		 fi
		 ;;
stop)
		 if [ -f $pidfile ] ; then
		 		 kill -TERM `cat $pidfile`
		 		 echo "Stopping Lyris ListManager"
		 else 
		 		 echo "$pidfile does not exist: Lyris ListManager is probably not running"
		 fi
                  ;;
*)
        echo "Usage: `basename $0` {start|stop}" >&2
        exit 1
        ;;
esac

exit 0
END

#############################################################################

$empty_string = <<'END';
END

#############################################################################

$web_server_intro = <<'END';

This version of Lyris ListManager includes its own web server (based on
Apache/mod_perl technology), specially tuned to greatly speed up the
ListManager web interface.  This web server can either replace any
existing web servers on your system, or run in addition to them.

We recommend that you run the ListManager web server as the main web server
on the machine, listening on port 80.  If you have an existing web
server that you want to remain on port 80, we recommend having the
ListManager web server use port 81.  NOTE: no two web servers can share a
port number, so you must make sure that you don't have a conflict.

Please select a port for the ListManager web server:

END

#############################################################################

$use_cgi = <<'END';

In addition to the included web server, you may optionally install the
ListManager web interface files with an existing web server.  The included
web server has been configured to improve the performance of the ListManager
web interface, so in most cases it will be faster than an existing
server.

The ListManager web server has already been installed, and you have not
currently installed support for an existing web server that may be
installed on your machine.  Is this OK?

--> YES, use only the included ListManager web server (recommended).
--> NO, add support for my existing web server as well.

END

#############################################################################
$web_freebsd_start_stop_script = <<'END';    
#! /bin/sh 
# Lyris ListManager Apache startup script for FreeBSD
#

# set some useful variables
pidfile="LYR_DIR/apache/logs/httpd.pid"

dir="LYR_DIR/apache"
cmd="$dir/bin/apache-lyris"

# exit if the command doesn't exist
[ -x "$cmd" ] || exit 0 

# set PERL5LIB
PERL5LIB="$dir/lib"; export PERL5LIB
   
#choose a config file  
conf="$dir/conf/httpd.conf.unix"
if [ -f "$dir/conf/httpd.conf.noperl" ]; then
    conf="$dir/conf/httpd.conf.noperl"
fi

# See how we were called
case "$1" in
start)
		 if [ -f $pidfile ] && kill -0 `cat $pidfile` 2> /dev/null ; then
		 		 echo "$pidfile already exists: apache-lyris may already be running"
		 else 
		 		 echo "Starting apache-lyris"
		 		 $cmd -d $dir -f $conf
		 fi
		 ;;
stop)
		 if [ -f $pidfile ] ; then
		 		 kill -TERM `cat $pidfile`
		 		 echo "Stopping apache-lyris"
		 else 
		 		 echo "$pidfile does not exist: apache-lyris is probably not running"
		 fi
                  ;;
restart)
                  $0 stop
                  sleep 4
                  $0 start
                  ;;

*)
        echo "Usage: `basename $0` {start|stop|restart}" >&2
        exit 1
        ;;
esac

exit 0
END

#############################################################################

$web_default_start_stop_script = <<'END';
#!/bin/sh

# Web server startup script for Lyris ListManager web interface
# Place this in /etc/rc2.d (or elsewhere, depending on Unix platform)

state=$1

case $state in

'start')
        LYR_DIR/apache/bin/start           # start the web server
        ;;
'stop')
        LYR_DIR/apache/bin/stop            # stop the web server
        ;;
*)
        echo "Usage: $0 { start | stop }"
        ;;
esac

END

#############################################################################

$web_redhat_start_stop_script = <<'END';
#!/bin/sh
#
# ListManager   This shell script takes care of starting and stopping
#               Lyris ListManager.
#
# chkconfig: 35 96 96
# description: A pre-built apache/mod_perl installation for use with \
#              the Lyris ListManager web interface.  In most cases, this should be \
#              run as long as the "lyris" service is running. \
# processname: lyris pidfile: /var/run/lyris.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# set some useful variables
dir="LYR_DIR/apache"
cmd="$dir/bin/apache-lyris"

# exit if the command doesn't exist
[ -x "$cmd" ] || exit 0 

# set PERL5LIB
PERL5LIB="$dir/lib"; export PERL5LIB

# choose a config file
conf="$dir/conf/httpd.conf.unix"
if [ -f "$dir/conf/httpd.conf.noperl" ]; then
    conf="$dir/conf/httpd.conf.noperl"
fi
    

# See how we were called.
case "$1" in
  start)
    echo -n "Starting apache-lyris: "
    ulimit -n 3072  # more file descriptors
    daemon $cmd -d $dir -f $conf
    echo
    touch /var/lock/subsys/apache-lyris
    ;;
  stop)
    echo -n "Stopping apache-lyris: "
    killproc apache-lyris -TERM
    echo
    rm -f /var/lock/subsys/apache-lyris
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  status)
    status apache-lyris
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac
exit 0

END


#Choose which start/stop script to use
if( -f "/etc/redhat-release" ) {
    $web_start_stop_script = $web_redhat_start_stop_script;
} else {
    $web_start_stop_script = $web_default_start_stop_script;
}


#############################################################################

$web_auto_start = <<'END';
Would you like the Lyris ListManager web server to automatically start when your
system boots?

If you answer "Yes", a script named 'SCRIPTNAME' will be placed in your
INITD_DIR directory, with symbolic links to start and stop the 
server in RC_START_DIR and RC_STOP_DIR respectively.

NOTE: If you answer "No", you will need to start the ListManager web
server by hand in order to have access to the ListManager web interface.

END
#############################################################################

$web_auto_start_freebsd = <<'END';
Would you like the ListManager web server to automatically start when your
system boots?

If you answer "Yes", a script named 'SCRIPTNAME' will be placed in your
RC_START_DIR directory.

NOTE: If you answer "No", you will need to start the ListManager web
server by hand in order to have access to the ListManager web interface.

END
#############################################################################

$no_rc_dir = <<'END';

The directory for the startup script could not be found.  The installation
script has created a sample startup script for you in 
LYR_DIR/bin/SCRIPTNAME.

To start ListManager automatically when the system boots, you will need to copy 
this script into the appropriate startup directory.

END

#############################################################################

$use_perl_utils = <<'END';

The ListManager installation includes a number of utility scripts in the
'scripts' directory.  Would you like to check your configuration, to
make sure perl is installed, and that the ListManager perl libraries are
available to it?

--> Yes, check my perl installation
--> No, I don't want to use the extra utilities

END

#'############################################################################

$reminder_readme1 = <<'END';

You can now run ListManager in the foreground with the command:
    LYR_DIR/bin/lyris start

or run ListManager in the background (daemon) with the command:
    LYR_DIR/bin/lyris -bd

We recommend that you first run ListManager in the foreground, so that you
see any warning messages.  If you encounter a conflict on port 25 with
Sendmail, please see the README file.

You can start the ListManager daemon by running:
    LYR_DIR/bin/S96lyris start
and stop it by running:
    LYR_DIR/bin/S96lyris stop

You can start the Apache daemon by running:
    LYR_DIR/bin/S96apache-lyris start
and stop it by running:
    LYR_DIR/bin/S96apache-lyris stop

END

#############################################################################

$reminder_readme2 = <<'END';

To create your first mailing list, you can point your web browser at:

    http://HOSTNAME/cgi-bin/lyris.pl

When prompted for a user name and password, you should give the name
"admin" and the password "ADMIN_PASS"

A log of the actions taken by this installation program has been saved
to LYR_DIR/install.log

A ListManager uninstall script has been created for you at:
   LYR_DIR/uninstall_lyris_now

If you would like to review this information at a later time, you can
find it in LYR_DIR/README.

END

#############################################################################

$allow_dns_check = <<'END';

In order for ListManager to function properly, your DNS configuration
must be correct.  I can check this for you now.  (This may take
a few minutes if things are not configured correctly.) 

Proceed with the DNS check? (recommended)

END

#############################################################################

$dns_failure_warning = <<'END';

The installation script detected NN error(s).  It is highly
recommended that you quit, and restart the installation after these
errors have been fixed.

END

#############################################################################

$dns_failed = <<'END';

The installation script detected fatal errors in your DNS configuration.  
ListManager will not run properly until these problems are corrected.  
Please correct these problems, and run the installation again.

END

#############################################################################

$must_install_as_root = <<'END';

You do not appear to be running with root privileges.  ListManager requires
root privileges to function properly.  Please log in as root, and try
the installation again.

END

#############################################################################

$start_lyris_now = <<'END';

Would you like the setup program to start ListManager for you now?

END

#############################################################################

$starting_system_checks = <<'END';

To ensure that ListManager will operate properly, I will now check your system's
configuration.

END

#############################################################################

$finished_system_checks = <<'END';

The system checks are complete.  I will now continue with the main 
installation.

END

#############################################################################
$needs_upgrade_code = <<'END';

**********************************************************
 THIS INSTALLATION REQUIRES AN 
  UPGRADE CODE
**********************************************************

Please note that Lyris List Server version 3.0 requires upgrading to
be compatible with Lyris ListManager version 4.0. There is no charge
for this upgrade, provided it is done within 60 days of release and 
you wish to stay at the same license level.  Please contact our Sales
Department at (800) 768-2929 or (510) 549-4350, or send an email
to sales@lyris.com to obtain a valid upgrade code.  

This code will have the form: UP4-XXXXX-YYY.

END
#############################################################################

$needs_activation_code = <<'END';

**********************************************************
 THIS INSTALLATION REQUIRES A LICENSE
**********************************************************

  Your Lyris ListManager installation does not have 
  a valid Activation Code.

  Please note that the free 3.0 version may not be 
  upgraded to 4.0 free of charge. If you wish to 
  continue to use ListManager free version, you will 
  need to start with a new download of 4.0 or contact 
  our Sales Department at sales@lyris.com to purchase 
  a license and obtain a valid upgrade code.

  This upgrade requires that you purchase a license and 
  have a valid upgrade code. 

  If you have already purchased a license, but have not 
  yet registered, please visit the registration page at 
  http://www.lyris.com/register.html. You can get your 
  upgrade code at the same time you get your activation
  code.

  If you have not purchased a license, please call our 
  sales department at (800) 768-2929, or send an email 
  to sales@lyris.com.

  Once you have entered your activation code into your 
  Lyris ListManager server, please try the installation 
  program again.

END

#############################################################################

$invalid_upgrade_code = <<'END';

  The Upgrade Code you have entered is not valid. It 
  might be a typo, in which case you may want to 
  re-enter it. If you believe your upgrade code is 
  valid and continue to experience difficulty, please 
  call our sales department at (800) 768-2929, or send 
  an email to sales@lyris.com.

END

}




