#!/usr/bin/perl
require "wiz_resources";
require "wiz_creator";

nowdate();
$verify=param("verify");
$title="Site creator";
$title.=" (verified)" if ($verify ne "");
header($title,"","edit");

if ($user eq "") {
  headline("Error: Access denied","red","","You should not use this page unless you have logged into the system.  Please <a href=\"$default_cgi/login\">log in</a> and try again.");
} else {
  push @pages,"index";
  push @pages,"about_us";
  push @pages,"news";
  push @pages,"contact_us";
  
  $exists=0;
  
  for ($loop=0;$loop<@pages;$loop++) {
    if (-e "${homesite}$pages[$loop].html") {
      $EXISTS{$pages[$loop]}="exists";
      $exists++;
    }
  }
  
  if (($exists == 0) || (($exists>0) && ($verify eq "true"))){
    for ($loop=0;$loop<@pages;$loop++) {
      $err=0;
      open OUTPUT, ">${homesite}$pages[$loop].html" or $err=1;
      if ($err==1) {
        headline("Error: Unable to write temporary file \"$pages[$loop].html\"","red","","The file $pages[$loop].html could not be written to disk, which may affect the creation of the website.  Please contact <a href=\"mailto:$support_email\">$company support</a> if the site is not created properly.");
      } else {
        select OUTPUT;
        print "<html>\n<head>\n<title>Temporary file \"$pages[$loop].html\"</title>\n</head>\n<body bgcolor=\"#FFFFFF\" text=\"#000000\" link=\"#FF9900\" alink=\"#FFFF99\" vlink=\"#FFCC66\">\n";
        headline("Temporary page","","","This is a temporary file so that the web creation system will create the correct list of links.  If you can see this page something may have gone wrong with the site creation process, so please contact <a href=\"mailto:$support_email\">$company support</a> with any queries.");
        print "</body></html>";
        select STDOUT;
        close OUTPUT;
      }
    }
    
    set_change_date();
    
    for ($loop=0;$loop<@pages;$loop++) {
      nuke_paragraphs();
  
      if ($pages[$loop] eq "index") {
        push @para_title, "Welcome to our website!";
        $orgname=$user_shortorg;
        $orgname=$user_longorg if ($user_shortorg eq "");
        $orgname=~ s/^The //i;
        $paratext="Welcome to the $orgname website!";
        if ($user_description ne "") {
          $usrdes="$user_description.";
          unless ($usrdes =~ m~^we ~i) {
            $usrdes="We are ".lcfirst($usrdes);
            $usrdes=~ s~^We are maker ~We are makers ~;
            $usrdes=~ s~^We are create ~We create ~;
            $usrdes=~ s~^We are design ~We design ~;
          }
          $paratext.="  ".$usrdes;
        }
        push @para_text, $paratext;
        push @para_type, "";
        push @para_pic,  "";
        push @para_align,"";
        push @para_caption,"";
        push @para_morelink,"about_us.html";
      }
      if ($pages[$loop] eq "about_us") {
        push @para_title, "About $user_longorg";
        $usrdes="$user_longorg";
        $usrdes.=" - $user_description" if ($user_description ne "");
        $usrdes.=".  ";
        $usrdes.="Our interests include ".lcfirst($user_keywords)."." if ($user_keywords ne "");
        
        push @para_text, $usrdes;
        push @para_type, "";
        push @para_pic,  "";
        push @para_align,"";
        push @para_caption,"";
        push @para_morelink,"contact_us.html";
      }
      if ($pages[$loop] eq "news") {
        push @para_title, "Site created ($day $month $year)";
        push @para_text, "This website went online at $hour:$minute on $weekday, $day $month $year.";
        push @para_type, "";
        push @para_pic,  "";
        push @para_align,"";
        push @para_caption,"";
        push @para_morelink,"";
      }
      if ($pages[$loop] eq "contact_us") {
        push @para_title, "Our address";
        push @para_type, "address";
        push @para_pic,  "";
        push @para_align,"";
        push @para_caption,"";
        push @para_morelink,"";
        
        push @para_title, "Email form";
        push @para_type, "mailform";
        push @para_pic,  "";
        push @para_align,"";
        push @para_caption,"";
        push @para_morelink,"";
      }
      creator($pages[$loop]);
    }
    
    system "rm -rf ${homesite}gfx/";
    mkdir "${homesite}gfx", 0777;
    system "cp -r $templatedir/$colourscheme/gfx/* ${homesite}gfx/";
    system "chmod -R 777 ${homesite}gfx";
    
    print "You can now <a href=\"$baseurl\" target=\"_blank\">view your site</a>, <a href=\"$default_cgi/edit_page?page=index.html\">edit the index page</a> or <a href=\"$default_cgi/edit\">go to the main edit page</a>";
    
  } else {
  
    if ($exists>1) {
      headline("Safety check: pages already exist","","","The following pages already exist:");
    } else {
      headline("Safety check: page already exists","","","This page already exists:");
    }    
    while (($key, $val) = each %EXISTS) {
      $key=~s/_/ /;
      print "<br><font color=\"#FF0000\">*</font>&nbsp;<b>$key</b>\n";
    }
    print "<p>\n";    
    if ($exists>1) {print "These";} else {print "This";}    
    print" could simply be ";    
    if ($exists>1) {print "placeholders";} else {print "a placeholder";}
    
    print ", or you could be about to overwrite an existing site (possibly because there has been a problem with this system)\n<p>\n";
    print "If you are using this system for the first time, or your are sure that you are not damaging an existing site, you can override this safety check; otherwise please contact <a href=\"mailto:$support_email\">$company support</a> to check that everything is okay.";
    print "<p>\n<a href=\"$ENV{REQUEST_URI}?verify=true\"><font color=\"#ff0000\">Continue: override safety check</font></a> or <a href=\"$default_cgi/edit\"><font color=\"#00cc00\">Stop: go back to editing page</font></a>";
  }
}

footer();
