
#
# Set background color
#

$ENV{BACKGROUND}="BGCOLOR=\"$ENV{SERVER_WWW_BACKGROUND}\"";

require "flush.pl";

$SERVER_HOME     = $ENV{SERVER_HOME};
$CGI_DIR         = $ENV{SERVER_CGI_HOME};

$TOKEN = "\U$ARGV[0]";
unless ($TOKEN) {
  print "usage: make-web <software token>\n";
  exit(-1);
}
unless (-d "$ENV{SERVER_SOLVERS}/$TOKEN") {
  print "make-web: cannot find $ENV{SERVER_SOLVERS}/$TOKEN\n";
  exit(-1);
}

#
# Create WEB directory for HTML
#

$HDIR = "$ENV{SERVER_WWW_URL}/solvers/$TOKEN";
$DIR  = "$ENV{SERVER_WWW_HOME}/solvers/$TOKEN";
unless (-d "$DIR") {
  print "creating $DIR\n";
  system("mkdir $DIR") && die "$0: can't create $DIR";
}

#
# Create HTML forms 
#

print "creating $HDIR/index.html\n";
&clobber("$DIR/index.html") && 
  system("$^X make-solver-homepage.pl $TOKEN > $DIR/index.html");

print "creating $HDIR/solver-www.html\n";
&clobber("$DIR/solver-www.html") && 
  system("$^X make-solver-www.pl $TOKEN > $DIR/solver-www.html");

print "creating $HDIR/solver-comment.html\n";
&clobber("$DIR/solver-comment.html") && 
  system("$^X make-solver-comment.pl $TOKEN > $DIR/solver-comment.html");

print "creating $HDIR/solver-email.html\n";
&clobber("$DIR/solver-email.html") && 
  system("$^X make-solver-email.pl $TOKEN > $DIR/solver-email.html");

print "creating $HDIR/solver-template.txt\n";
&clobber("$DIR/solver-template.txt") && 
  system("$^X $ENV{SERVER_BIN}/make-solver-template.pl $TOKEN > $DIR/solver-template.txt");

unless ($TOKEN =~ /^ADMIN:.*$/) {
  print "creating $HDIR/solver-sample.html\n";
  &clobber("$DIR/solver-sample.html") && 
    system("$^X make-solver-sample.pl $TOKEN > $DIR/solver-sample.html");
}

#
# Create CGI directory
#

$HDIR = "$ENV{SERVER_CGI_URL}/solvers/$TOKEN";
$DIR  = "$CGI_DIR/$TOKEN";
unless (-d "$DIR") {
  system("mkdir $DIR") && die "$0: can't create $DIR";
} 

#
# Create CGI forms
#

print "creating $HDIR/nph-solver-www.cgi\n";
&clobber("$DIR/nph-solver-www.cgi") && 
  system("$^X make-solver-www-cgi.pl     $TOKEN > $DIR/nph-solver-www.cgi")     && die "$0: can't create $DIR/nph-solver-www.cgi";
print "creating $HDIR/nph-solver-sample.cgi\n";
&clobber("$DIR/nph-solver-sample.cgi") && 
  system("$^X make-solver-sample-cgi.pl $TOKEN > $DIR/nph-solver-sample.cgi") && die "$0: can't create $DIR/nph-solver-sample.cgi";
print "creating $HDIR/solver-comment.cgi\n";
&clobber("$DIR/solver-comment.cgi") &&
  system("$^X make-solver-comment-cgi.pl $TOKEN > $DIR/solver-comment.cgi") && die "$0: can't create $DIR/solver-comment.cgi";

system("chmod a+x $DIR/solver-comment.cgi") && die;
system("chmod a+x $DIR/nph-solver-www.cgi")     && die;
system("chmod a+x $DIR/nph-solver-sample.cgi") && die;

sub clobber {
  if ($ENV{CLOBBER} == 1) {
    return 1;
  }
  if ($ENV{NOCLOBBER} == 1) {
    print "not changing $_[0]\n";  
    return 0;
  }
  if (-f "$_[0]") {
    while(1) {
      print "$_[0] exists, overwrite? [y/n or a/0 (all/none)] --> ";
      $_ = <STDIN>;
      return 1 if (/(y)/);
      return 0 if (/(n)/);
      if (/a/) {
        $ENV{CLOBBER} = 1;
        return 1;
      }
      if(/0/) {
	  $ENV{NOCLOBBER} = 1;
	  return 0;
      }
    }
  } else {
    return 1;
  }
}



