#!/usr/bin/perl

$frag = "/www/cgi-bin/formmail.txt";

$domain = $ENV{'SERVER_NAME'};
$root_dir = $ENV{'SERVER_NAME'};
$root_dir =~ s/\.com|\.net|\.org//i;
$root_dir =~ s/www.//i;
$docroot = $root_dir;
$root_dir = "/www/htdocs/".$root_dir;

$uid = (stat($root_dir))[4];
$owner = (getpwuid($uid))[0];

$header = "<HTML><HEAD><TITLE>Planetmind Gallery Maker Script</TITLE>
</HEAD>
<BODY background=http://planetmind.net/images/hunabku_bkg3.gif>
<IMG SRC=http://planetmind.net/images/planetwomanlogo.gif WIDTH=212 HEIGHT=54><p>";

# Parse Form Contents

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);

   # Un-Webify plus signs and %-encoding
   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ s/<!--(.|\n)*-->//g;

   $FORM{$name} = $value;
}

# branch...

if (!$FORM{'submit'}) {

   &return_form;
   exit;

}

$install_dir = $FORM{'location'};


$ex = mkdir("$root_dir/$install_dir",0775);

if (!$ex) {
 
  $result =  "<font size=+1 color=red>Error: $!. Make sure your folder name is valid.</font><P>\n";
  &return_form;
  exit;

}
  
$ex = `mkdir -m777 $root_dir/$install_dir/thumb`;
$ex = `cp /www/tools/gallery/index.php $root_dir/$install_dir`;
$ex = `cp -R /www/tools/gallery/buttons/ $root_dir/$install_dir`;
$ex = `cp /www/tools/gallery/slideshow.php /www/tools/gallery/open.php /www/tools/gallery/clicker.php $root_dir/$install_dir`;
$ex = `cp /www/tools/gallery/upload.cgi $root_dir/cgi-bin`;
$ex = `chmod 755 $root_dir/cgi-bin/upload.cgi`;
$ex = `cp /www/tools/gallery/gallery_manager.cgi $root_dir/cgi-bin`;
$ex = `chmod 755 $root_dir/cgi-bin/gallery_manager.cgi`;
$ex = `cp -R /www/tools/gallery/buttons $root_dir/$install_dir`;


open(FILE, ">$root_dir/$install_dir/header.inc") || print "no header\n";
print FILE $FORM{'header'};
close(FILE);

open(FILE, ">$root_dir/$install_dir/footer.inc") || print "no footer\n";
print FILE $FORM{'footer'};
close(FILE);


# return response

print "Content-type: text/html\n\n";
print $header;
print "<font size=+1>You new gallery has been installed. The URL of your gallery is:<P>\n";

print "<A HREF=\"http://".$ENV{'SERVER_NAME'}."/$install_dir/\">http://".$ENV{'SERVER_NAME'}."/".$docroot."/$install_dir/</A></font><P>\n";
print qq!
Any JPEG (.jpg) image placed in that directory will be indexed and thumbnailed automatically.<P>
A few rules:<BR>
<UL>
<LI>All images must be JPEG format, and have a .jpg (or .JPG) extension\!
<LI>Do not use spaces, commas,single quotes, or other characters in filenames\!
</ul>
To add captions, upload and delete images, use the <a href="http://$ENV{'SERVER_NAME'}/cgi-bin/cgiwrap/$owner/gallery_manager.cgi">Gallery Manager</a> tool. 
(It's also available from your <a href="http://$ENV{'SERVER_NAME'}/admin">Admin Page.)<P>
!;

print "</body></html>";

exit;


sub return_form {
print "Content-type: text/html\n\n";
print qq!
<HTML><HEAD><TITLE>Planetmind Form Processing Script</TITLE>
</HEAD>
<BODY background=http://planetmind.net/images/hunabku_bkg3.gif>
<IMG SRC=http://planetmind.net/images/planetwomanlogo.gif WIDTH=212 HEIGHT=54><p>
$result

<H1>Planetmind Gallery Maker Script Installer</H1>
Use this form to create your own image gallery. <P>

<HR><P>
<FORM ACTION="gallery_maker.cgi" METHOD="POST">
Install Directory: <I>http://www.$domain/</I><INPUT NAME="location" TYPE="text" SIZE=32,1 value="gallery"><P>
Paste in your site header (optional - you can use what's there):<BR> <TEXTAREA NAME="header" ROWS=6 COLS=60 WRAP=soft>
<HTML><HEAD><TITLE>$ENV{'SERVER_NAME'} Image Gallery</TITLE></HEAD>
<BODY BGCOLOR="Black" TEXT="White" LINK="Yellow" VLINK="Silver" ALINK="Black">
<H2 align=center>$ENV{'SERVER_NAME'} Image Gallery</H2>
</TEXTAREA><p>
Paste in your site footer (optional):<BR> <TEXTAREA NAME="footer" ROWS=6 COLS=60 WRAP=soft>
<P><HR><P>
[<a href="http://$ENV{'SERVER_NAME'}">Back to the Site</a>]
</body></html>
</TEXTAREA><p>
<INPUT NAME="submit" TYPE="submit" VALUE="Create Gallery"> 

</FORM>

<BR><BR><BR><BR><HR><P>

</body></html>
!;


}
