: # *-*-perl-*-*
	eval 'exec perl -S $0 "$@"'
	if $running_under_some_shell;  
#
# buildmirror.pl
#
$version = "2.03";
# 
# Last update March 16th 2004
#
# Part of the Fachwelt-Physik- & SINN-project
#
# Copyright by Thomas Severiens, 
# Institute for Science Networking Oldenburg GmbH
#
#
# buildmirror.pl -h    prints helptext
# To setup a Fachwelt-Physik-Mirror just download this file
# and let it run. Please do not forget to send
# an email to
# severiens@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:
    buildmirror.pl [-h|-help|-?|-v]
    
buildmirror will setup a local mirror of the Fachwelt-Physik
http://www.fachwelt-physik.de

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

  -v          will display the version number
  
buildmirror 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 Fachwelt-Physik
before running the script a second time.

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

buildmirror is part of the Fachwelt-Physik mirror and may be used
for free as long as you mirror the whole package of
Fachwelt-Physik as it is! All parts are protected by the German
Urheberrecht. Last update of this script: March 16th 2004 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/\/buildmirror.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 Fachwelt-Physik-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 \"fachwelt\"
# localdir = /www/fachwelt
localdir = /www/fachwelt

# The Server, which the data are taken from
# sourceserver = www.fachwelt-physik.de/mirror
sourceserver = www.fachwelt-physik.de/mirror

# For  internal communication within the Fachwelt-Physik 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/\/buildmirror.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;}
    }
  }
  close (CONF);
  if ($localdir =~ /^\s*$/) {print "Missing data in mirror.conf file: \nlocaldir (sould be set as \"\/www\/fachwelt\")\n\n"; die;}
  if ($sourceserver =~ /^\s*$/) {print "Missing data in mirror.conf file: \nsourceserver (sould be set as \"www.fachwelt-physik.de\/mirror\")\n\n"; die;}
  else {$sourceserver =~ s/http\:\/\///ig;}
}


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);
    }
  }
}

