#!/usr/bin/perl

# CGI/Perl script to fork a bkgo-server, called by CGI-URL:
#
#    start_server.pl?port=30000

# Set this variable to your installation directory for JDK or JRE:

$javahome = "/usr/local/jdk";

# Set this variable to the *absolute* filesystem location of all the
# bkgo *.class files:

$bkgo_code = "/home/httpd/html/bkgo/classes";

#---------------------------------------------------

# Fetch the URL argument to this script, i.e. "start_server.pl?port=30000"
$query_string = $ENV{'QUERY_STRING'};
($fieldname, $port) = split(/=/, $query_string);

# Construct the command to launch a GoServer
$launchcommand = $javahome."/bin/java "."-classpath ".$javahome."/lib/classes.zip:".$bkgo_code." GoServer ".$port;


 
# Swedish Chef:  La, da-da-da dee-dee-dee da, fork, fork, fork!

if (my $pid = fork) {
    # process successfully forked:
    print "Content-type: text/html", "\n\n";
    print "<h2>Success</h2>\n";
    print "Launched a GoServer on port <b>".$port."</b>, PID ".$pid.".<p>\n\n";
    print "<p>";
    print "Click <a href=\"list_servers.pl\">here</a> to return to the
           list of running GoServers.";

    exit(0);
}
elsif (defined $pid) { # i.e. == 0
    close(STDOUT);
    `$launchcommand`;
    exit 0;
}
else {
    print "<b>Error</b>: Could not launch a GoServer. <br>\n";
    print "<i>Unable to fork a process.</i>\n";
    print "<p>Click <a href=\"list_servers.pl\">here</a> to return 
                    to the list of running GoServers.";
}

exit(0);
