#!/usr/bin/perl
##########################################################
## Castle Links						##
## Created: 06/17/1999					##
##########################################################
# By: Castellum.net					##
# WebSite: www.castellum.net				##
##########################################################
##########################################################
# (C)Copyright 1999-2000 Castellum.net, All rights reserved	
##########################################################
# DISCLAIMER:						##
# THIS PROGRAM IS PROVIDED WITHOUT WARRANTIES OF ANY    ##
# KIND, WHETHER EXPRESSED OR IMPLIED.   THIS PROGRAM IS ##
# PROVIDED WIThOUT WARRANTIES AS TO PERFORMANCE, OR  	##
# MERCHANTABILITY OF THIS PROGRAM.			##
# TERMS OF USE:						##
# THIS SCRIPT MAY BE MODIFIED, BUT NOT REDISTRIBUTED IN	##
# ANY WAY, SHAPE, OR FORM.  IN ANY CASE, COPYRIGHT AND  ##
# SCRIPT INFORMATION MUST BE KEPT IN PLACE		##
##########################################################
use CGI::Carp qw(fatalsToBrowser);
if (-e "install.lock"){&IError("Install lock will not allow you to overwrite files.  Delete the lock and try again");}

require "global.pl";
&InitGlobalVariables;

if ($ENV{'QUERY_STRING'} eq ""){
&PrintMain;
}elsif($ENV{'QUERY_STRING'} eq "install"){
&Install;
}


sub Install {
my %input = &GetFormInput;

&LoadSetup;
$setup{'referers'} = $input{'domain'};
$setup{'admin_email'} = $input{'email'};
$setup{'sendmail'} = $input{'sendmail'};
&WriteSetup;
$perl = $input{'perl'};

require "admin_setup.pl";
$new_username = 'admin';
$new_password = 'password';

my $password = crypt($new_password, substr($new_username,0,2));

open(ADMIN, ">admin_setup.pl")||&Error("Couldn't open admin_setup.pl: $!");
if ($setup{'flock'} == 1){flock(ADMIN,2);}
print ADMIN "\$admin_username = '$new_username';\n";
print ADMIN "\$admin_password = '$password';\n";
print ADMIN "1;\n";
if ($setup{'flock'} == 1){flock(ADMIN,8);}
close(ADMIN);

$mode = (stat('install.cgi'))[2];                
$chmod = sprintf "%04o", $mode & 07777;

@cgi_files = ("main.cgi","add.cgi","search.cgi","category.cgi","admin.cgi");

foreach $file (@cgi_files){
if ($input{'777'} == 1){
chmod(oct('0777'),"$file");
}
	open(FILE, "$file")||&IError("$file: $!");
	@lines = <FILE>;
	close(FILE);
	open(FILE, ">$file")||&IError("$file: $!");
	print FILE "$perl\n";
	print FILE @lines;
	close(FILE);

if ($input{'777'} == 1){
chmod(oct($chmod),"$file");
}
}

if ($input{'777'} == 1){
@other_files = ("admin_setup.pl","clinks_setup.pl","data/categories.dat","data/links.dat");
foreach $file (@other_files){
chmod(oct("777"),"$file");
}
}

&PrintStandardHeader;

print qq~
<html>
<head>
<title>Castle Links Readme</title>
</head>
<body bgcolor="#E9E9E9" link="#800000">
<h3 align="center">Castle Links Installation</h3>
<hr>
<p align="left">Congratulations, installation is complete.&nbsp; A lock file has
been created to keep anyone else from running this script again, but it is
recommended that you delete the install.cgi file.</p>
<p align="left">The main script and where you want to point any visitors is the
&quot;<a href="main.cgi" target="_blank">main.cgi</a>&quot; file, it is what
displays the main categories to visitors.&nbsp; The admin script can be accessed
by going to the &quot;<a href="admin.cgi" target="_blank">admin.cgi</a>&quot;
file and entering a username and password.&nbsp; The default username and
password combination is &quot;admin&quot; and &quot;password.</p>
<p align="left">Please <a href="http://www.cgi-resources.com/rate?03857">Rate
this Script \@ The CGI Resource Index</a>.&nbsp; By using this script and rating
it you are helping ensure that future versions are released and that the script
is continually revised.</p>
<p align="left">There are a number of html files in the &quot;templates&quot;
directory.&nbsp; You will want to edit them to give the script the look and feel
of your website.&nbsp; More help on that can be found at our web site: <a href="http://castlecgi.castellum.net/scripts/CastleLinks/help.shtml">http://castlecgi.castellum.net/scripts/CastleLinks/help.shtml</a></p>
<p align="left">We hope you find this script useful and easy to use.&nbsp; Any
suggestions, comments, or questions can be emailed to us at: <a href="mailto:castlecgi\@castellum.net">castlecgi\@castellum.net</a></p>
<p align="left">Thank you,</p>
<p align="left">Castle CGI<br>
<a href="http://castlecgi.castellum.net">http://www.castle-cgi.com</a></p>
<hr>
<p align="center">©1999-2000 <a href="http://www.castellum.net">Castellum.net</a>
&amp; <a href="http://www.castle-cgi.com">Castle-CGI.com</a>, All Rights
Reserved</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</BODY>
</HTML>
~;
open(LOCK, ">install.lock")||&IError("Couldn't create install lock");
close(LOCK);
}




sub PrintMain {
if ($ENV{'HTTP_REFERER'} =~ /^http:\/\/(www\.){0,1}([\w\-]+\.[\w]{2,3})/i){
$domain = $2;
}else{$domain = "";}
$email = "";

open(INS, "install.cgi");
$perl = <INS>;
close(INS);

$results = `whereis sendmail`;
@spots = split(/ /,$results);
$sendmail = $spots[1];

&PrintStandardHeader;
print qq~

<html>
<head>
<title>Castle Links Readme</title>
</head>
<body bgcolor="#E9E9E9" link="#800000">
<h3 align="center">Castle Links Installation</h3>
<hr>
<p align="left">You have now completed the first step of the installation
process.&nbsp; If you are reading this, that means that you successfully
uploaded this file a got it working.&nbsp; Below is a short form.&nbsp; The
script needs to know some things before it can run without messing up, none of
them are too technical, but ask your host if you don't understand what it is
asking for.&nbsp; The script has gone ahead and put some of the variables that
it found automatically in the boxes for you.</p>
<form method="POST" action="install.cgi?install">
  <div align="left">
    <table border="0" width="792">
      <tr>
        <td width="120" bgcolor="#008080">Domain Name:</td>
        <td width="658" bgcolor="#008080"><input type="text" name="domain" size="43" value="$domain"></td>
      </tr>
      <tr>
        <td width="784" colspan="2" bgcolor="#C0C0C0">This is the domain that
          you will be running the script from.&nbsp; So, if you are running it
          on <a href="http://www.thesite.whatever.com">http://www.thesite.whatever.com</a>
          you would enter thesite.whatever.com.&nbsp; The script needs this to
          enact some Spam fighting measures to keep people from spamming your
          links database..</td>
      </tr>
      <tr>
        <td width="120"><br>
        </td>
        <td width="658"></td>
      </tr>
      <tr>
        <td width="120" bgcolor="#008080">Email Address:</td>
        <td width="658" bgcolor="#008080"><input type="text" name="email" size="43" value="$email"></td>
      </tr>
      <tr>
        <td width="778" colspan="2" bgcolor="#C0C0C0">This is the email address
          of the Links Administrator.&nbsp; It is used for any correspondence
          that the script might send out.</td>
      </tr>
      <tr>
        <td width="120"><br>
        </td>
        <td width="658"></td>
      </tr>
      <tr>
        <td width="120" bgcolor="#008080">Perl Location</td>
        <td width="658" bgcolor="#008080"><input type="text" name="perl" size="43" value="$perl"></td>
      </tr>
      <tr>
        <td width="778" colspan="2" bgcolor="#C0C0C0">This is the location of
          your perl interpretor.&nbsp; If the script was able to retreive it
          automatically, you probably don't want to change it.&nbsp; But, if the
          box is blank, put the correct location in there.&nbsp; This would be
          the same line that you put on the first line of the install.cgi file.</td>
      </tr>
      <tr>
        <td width="120"></td>
        <td width="658"><br>
        </td>
      </tr>
      <tr>
        <td width="120" bgcolor="#008080">Sendmail Location:</td>
        <td width="658" bgcolor="#008080"><input type="text" name="sendmail" size="44" value="$sendmail"></td>
      </tr>
      <tr>
        <td width="778" colspan="2" bgcolor="#C0C0C0">This is the location of
          sendmail.&nbsp; It is used for sending emails out.&nbsp; The script
          may have been able to identify it's location automatically, but if
          not, enter it.</td>
      </tr>
      <tr>
        <td width="120"></td>
        <td width="658"></td>
      </tr>
    </table>
  </div>
  <p align="left">You are now ready to install the script.&nbsp; Once you press
  the button please don't stop it.&nbsp; The script has some work to do and it
  may take a minute or two to complete the process.&nbsp; <b>Note:</b> If you
  receive a &quot;permission denied&quot; error, try setting the permissions on
  the .cgi files and any other file that it has trouble opening to 777.&nbsp; That should allow the install script to write to
  it.&nbsp; Once the install is completed, you may want to change them back to
  755 or whatever you had them at previously.&nbsp; Also, if you receive a
  install lock error, FTP in and delete the &quot;install.lock&quot; file.</p>
  <p align="left">If you get a permission denied error, or know that your server
  requires files to be 777 in order to write to them, check this box: <input type="checkbox" name="777" value="1"></p>
  <p align="left"><input type="submit" value="Install the Script" name="B1"></p>
</form>
<hr>
<p align="center">©1999-2000 <a href="http://www.castellum.net">Castellum.net</a>
&amp; <a href="http://www.castle-cgi.com">Castle-CGI.com</a>, All Rights
Reserved</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</BODY>
</HTML>

~;

}


sub IError {
	my ($error_string) = @_;
	print "Content-type: text/html\n\n";
	print "ERROR: $error_string";
	exit;
}



1;