#!/usr/bin/perl
#
# Script to move author spaces from
# /home/author
# to
# /home/httpd/html/priv/domain/author
#
# Should not be run automatically, but manually as part of the installation
#
# Since this is going to be run during installation, we cannot rely on lonc/lond/lonnet,
# and need to collect information the pedestrian way
#
use strict;
use File::Copy;

print "\nScript to move author spaces\n";
print "-----------------------------\n\n";
print "If run without parameters, the script will just tell you what it *would* do\n";
print "and give you warnings regarding possible problems.\n\n";

my $parameter=shift;

my ($login,$pass,$uid,$gid) = getpwnam('www');

my $commit=($parameter=~/commit/);

if ($commit) {
   print "\n *** Really running this\n";
} else {
   print "\nJust running in exploratory mode.\n";
   print "Run with parameter 'commit' to actually move the author spaces, e.g.\n";
   print "cpfiles.pl commit\n\n";
}

if ($commit) {
   print "\nMaking /home/httpd/html/priv\n";
   mkdir('/home/httpd/html/priv');
   chown($uid,$gid,'/home/httpd/html/priv');
}

# Authors hosted on this server
my %domauthors=();

foreach my $domain_directory (</home/httpd/lonUsers/*>) {
   my ($domain) = ($domain_directory=~/\/([^\/]+)$/);
   print "Found domain: $domain\n";
   my $dom_target="/home/httpd/html/priv/$domain";
   if ($commit) {
      print "Making $dom_target\n";
      mkdir($dom_target);
      chown($uid,$gid,$dom_target);
   } else {
      print "Would make $dom_target\n";
   }
   my @domauth=();
   foreach my $domain_author (</home/httpd/html/res/$domain/*>) {
      my ($author)=($domain_author=~/\/([^\/]+)$/);
      push(@domauth,$author);
   }
   $domauthors{$domain}=join(',',@domauth);
   print "Authors in $domain: $domauthors{$domain}\n";
}

# Go over all directories in the /home-directory
foreach my $home_directory (</home/*>) {
# Extract the author name
   my ($author) = ($home_directory=~/\/([^\/]+)$/); 
# Does this have a public_html-directory?
   unless (-e "/home/$author/public_html") { next; }
   print "Found author: $author\n";
   my $domain='';
   foreach my $trydom (keys(%domauthors)) {
      foreach my $domauth (split(/\,/,$domauthors{$trydom})) {
         if ($author eq $domauth) {
            print "$author found in $domauth\n";
            if ($domain) {
               print "*** ERROR: $author found in $domain earlier\n";
               print "*** This could be a serious problem\n";
               print "Enter 1: use $domain\n";
               print "Enter 2: use $trydom\n";
               print "Enter 3: stop\n";
               print "Your input: ";
               my $choice=<STDIN>;
               if ($choice==3) { print "Stopped.\n"; exit; }
               if ($choice==2) { $domain=$trydom; }
            } else {
               $domain=$trydom;
            }
            print "Will use $domain for $author\n";
         }   
      }  
   }
   if ($domain) {
      my $source_path="/home/$author/public_html";
      my $target_path="/home/httpd/html/priv/$domain/$author";
      if ($commit) {
         print "Moving $source_path to $target_path\n";
         move($source_path,$target_path);
         chown($uid,$gid,$target_path);
      } else {
         print "Would move $source_path to $target_path\n";
      }
   } else {
      print "*** WARNING: $author has no domain. The author may not have published.\n";
      print "Enter 1: do nothing, continue\n";
      print "Enter 2: stop\n";
      print "or enter domain for user to be placed into\n";
      print "Your input: ";
      my $choice=<STDIN>;
      if ($choice==2) { print "Stopped.\n"; exit; }
      if ($choice!=1) {
         chomp($choice);
         if ($choice) {
            my $dompath="/home/httpd/html/priv/$choice";
            my $newpath="/home/httpd/html/priv/$choice/$author";
            unless (-e $dompath) {
               print "*** WARNING: $dompath does not yet exist.\n";
            }
            if ($commit) {
               print "Making author $author in domain $choice\n";
               unless (-e $dompath) {
                  print "Making $dompath\n";
                  mkdir($dompath);
                  chown($uid,$gid,$dompath);
               }
               print "Making $newpath\n";
               mkdir($newpath);
               chown($uid,$gid,$newpath);
            } else {
               print "Would make author $author in domain $choice\n";
               unless (-e $dompath) {
                  print "Would make $dompath\n";
               }
               print "Would make $newpath\n";
            }
         }
      }
   }
}
print "\nDone.\n";
