: # *-*-perl-*-*
	eval 'exec perl -S $0 "$@"'
	if $running_under_some_shell;  
#
# buildmirror.pl
#
$version = "2.03";
# 
# Last update May 13th 2002
#
# Part of the PhysNet- & SINN-project
#
# Copyright by Thomas Severiens, 
# Institute for Science Networking Oldenburg GmbH
#
#
# buildmirror.pl -h    prints helptext
# To setup a PhysNet-Mirror just download this file
# and let it run. Please do not forget to send
# an email to
# physnet@isn-oldenburg.de
# or severien@isn-oldenburg.de
# to inform us about the new mirror.
#
# Please include this file into your crontab 
# for frequently update !!
#
#
# What's new:
# 
# In 2.01:
# Checks, if $localdir is writable.
#
# In 2.02:
# Security-fix: Replaces 0777 with 0755 as rights for new
# directories.
# 
# In 2.03:
# Checks existing mirrorfiles.dat, if write protected.
# Sets rights of all files to 0755 explicitely.
# 
########################################################
$Usage =<<'End_of_Usage;';
Usage is:
    newbuild.pl [-h|-help|-?|-v]
    
newbuild will setup a local mirror of PhysNet
http://www.physics-network.org

  -h -help -? will display this help text.

  -v          will display the version number
  
newbuild needs the package LWP:simple which is part of
the most popular Perl installations. You can download
the module from www.CPAN.org
http://search.cpan.org/doc/GAAS/LWP5emu-0.01_01/lib/LWP/Simple.pm

On the first run of this script, a file mirror.conf is
created in the same directory this script runs in. 
The mirror.conf should be adapted to this mirror of PhysNet
before running the script a second time.

The latest version of newbuild will be downloaded and copied
automatically into the bin-directory of the mirror. This copy of
newbuild should be implemented into the crontab-file
to make it be executed frequently (e.g. once per week).
-----------------------------
newbuild replaces the former script buildmirror.pl

newbuild is part of the PhysNet mirror and may be used
for free as long as you mirror the whole package of
PhysNet as it is! All parts are protected by the German
Urheberrecht. Last update of this script: May 13th 2002 by 
Thomas Severiens severiens@isn-oldenburg.de
End_of_Usage;

# Requires File: mirror.conf in this Directory!!!

use LWP::Simple;

while (defined($ARGV[0])) 
{
    $opt = shift;

    if ($opt eq '-h' || $opt eq '-help' || $opt eq '-?')
    {
      print $Usage;
      exit;
    }
    elsif ($opt eq '-v')
    {
      print "Version $version\n";
      exit;
    }
}
&build_mirror_conf; # build configuration on first run of this script
&read_mirror_conf;  # read configuration file
if (!-x "$localdir") {mkdir("$localdir",0755) || die "Error: Couldn't create $localdir\n\n"; print "Created $localdir\n";} 
if (!-w "$localdir") {die "Error: Can't write files into $localdir\n\n";}
if (-e "$localdir/mirrorfiles.dat" && !-w "$localdir/mirrorfiles.dat")
  {die "Error: existing $localdir/mirrorfiles.dat is write protected\n\n";}
# create directory for the mirror, if not exists
$rc=getstore("http://$sourceserver/mirrorfiles.dat","$localdir/mirrorfiles.dat");
if (is_error($rc)) {die "Error: Couldn't download file mirrorfiles.dat from http://$sourceserver\n\n";}
#else {print "Downloaded file to $localdir/mirrorfiles.dat\n";}
# read list of files to be mirrored
&test_and_build_directories;
&build_htaccess;
&load_files;
&write_footer;


# SUBROUTINES
#
sub build_mirror_conf {
   $0 =~ s/\/newbuild.pl$//;
   if (!-e "$0/mirror.conf") {
     open (CONF,">$0/mirror.conf") || die "Error: Could not write into $0/mirror.conf\n";
     print CONF "# Directory, which the PhysNet-Mirror will be build
# This script has to have the rights, to write files into this place
# and build new subdirectories.
# Pathname should end with \"PhysNet\"
# localdir = /www/PhysNet
localdir = /www/PhysNet

# The Server, which the data are taken from
# sourceserver = www.physics-network.org/PhysNetMirror
sourceserver = www.physics-network.org/PhysNetMirror

# The URL the Institution, offering this mirror of PhysNet
# (will be linked in the footer of each page)
# localserver = www.isn-oldenburg.de
localserver = www.isn-oldenburg.de

# Name of the Institution offering this mirror of PhysNet
# (will be displayed in the footer of each page)
# localname = Institute for Science Networking, Oldenburg, Germany
localname = Institute for Science Networking, Oldenburg, Germany

# For communication internal of the PhysNet project only!
# admin-email = 
";
     close (CONF);
     print "Please edit the file $0/mirror.conf.\nRestart this script after editing $0/mirror.conf.\n\n";
     exit;
   }
}

sub read_mirror_conf {
  $0 =~ s/\/newbuild.pl$//;
  open (CONF,"<$0/mirror.conf") ||  die "Error: Couldn't open $0/mirror.conf\n";
  while (<CONF>) {
    if (/^#/) {next;}
    if (/=/) {
      @vorgabe = split (/=/);
      $vorgabe[0] =~ s/^\s*//;
      $vorgabe[0] =~ s/\s*$//;
      $vorgabe[1] =~ s/^\s*//;
      $vorgabe[1] =~ s/\s*$//;
      if ($vorgabe[0] =~ /localdir/i) {$localdir = $vorgabe[1]; next;}
      if ($vorgabe[0] =~ /sourceserver/i)   {$sourceserver = $vorgabe[1]; next;}
      if ($vorgabe[0] =~ /localserver/i)    {$localserver = $vorgabe[1]; next;}
      if ($vorgabe[0] =~ /localname/i)      {$localname = $vorgabe[1]; next;}
    }
  }
  close (CONF);
  if ($localdir =~ /^\s*$/) {print "Missing data in mirror.conf file: \nlocaldir (sould be set as \"\/www\/PhysNet\")\n\n"; die;}
  if ($sourceserver =~ /^\s*$/) {print "Missing data in mirror.conf file: \nsourceserver (sould be set as \"www.physics-network.org\/PhysNetMirror\")\n\n"; die;}
  else {$sourceserver =~ s/http\:\/\///ig;}
  if ($localserver =~ /^\s*$/) {print "Missing data in mirror.conf file: \nlocalserver (sould be set as \"www.myurl.xyz\/PhysNet\")\n\n"; die;}
  else {$localserver =~ s/http\:\/\///ig;}
  if ($localname =~ /^\s*$/) {print "Missing data in mirror.conf file: \nlocalname (a description of the mirror-server - name of the institution)\n\n"; die;}
}


sub test_and_build_directories {
  open(LIST,"<$localdir/mirrorfiles.dat") || die "Couldn't open $localdir/mirrorfiles.dat for reading.\n\n";
  while (<LIST>) {
    if (/^\/(.*)\/[^\/]+$/) {$dir=$1;
      if (!-x "$localdir/$dir") {mkdir("$localdir/$dir",0755) || die "Couldn'tcreate $localdir/$dir\n\n"; print "Created $localdir/$dir\n"}
    }
  }
  close(LIST);
}


sub build_htaccess {
  if (!-e "$localdir/.htaccess") {
    open (HTACCESS,">$localdir/.htaccess") || die "Error: Couldn't create file $localdir/.htaccess\n\n";
    print HTACCESS "Options +Includes +FollowSymLinks\nXbitHack full\n";
    close (HTACCESS);
    print "Created file $localdir/.htaccess\n";
  }
}


sub load_files {
  open(LIST,"<$localdir/mirrorfiles.dat") || die "Couldn't open $localdir/mirrorfiles.dat for reading.\n\n";
  while (<LIST>) {
    chomp;
    $rc=mirror("http://$sourceserver$_","$localdir$_");
    if (is_error($rc)) {print "Error: Couldn't download file $_ from http://$sourceserver\n";}
    chmod (0755,"$localdir$_");
#    else {print "Downloaded file to $localdir$_\n";}

  }
  close(LIST);
  
}


sub write_footer {
  $writefoot=0;
  if ($localserver ne "" && $localname ne "") {
    open (FOOT,"<$localdir/includes/footer2.html") || die "Error: Couldn't open $localdir/includes/footer2.html for reading\n\n";
    while (<FOOT>) {
      if (/PhysNet mirror via/) {$writefoot=1; last;}
    }
    close (FOOT);
    if (-x "$localdir/content/") {$writefoot=0;}
    if($writefoot==0) {
      if (-x "$localdir/content/") {
        open (FOOT,">$localdir/includes/footer2.html") || die "Error: Couldn't open $localdir/includes/footer2.html for writing\n\n";
      } else {
        open (FOOT,">>$localdir/includes/footer2.html") || die "Error: Couldn't open $localdir/includes/footer2.html for writing\n\n";
      }	
      print FOOT "<center>\n<font face=\"arial, helvetica, univers\" size=\"+1\" color=\"#666666\">\n<center>PhysNet mirror via <A href=\"http://$localserver\">$localname</A>\n</FONT>\n</center>\n";
      close (FOOT);
    }
  }
}
