#!/usr/bin/perl -w
use strict;

use File::Copy;

# Set your destination directories here.  $module_dir should be in perl's
# @INC array and $util_dir should be in your shell's $PATH
my $module_dir = '/usr/local/lib/site_perl';
my $util_dir = '/usr/local/bin';

# Make sure we're being run from the correct directory
die "This installer must be run in the directory where LowCal was unpacked.\n"
  unless -e './LowCal.pm';

# Verify that directories exist
die "Fatal error: \$module_dir points to nonexistent path $module_dir\n"
  unless -d $module_dir;
die "Fatal error: \$util_dir points to nonexistent path $util_dir\n"
  unless -d $util_dir;

# Complain if directories aren't in appropriate paths
print "Warning:  \$module_dir points to $module_dir, which is not in \@INC\n"
  unless grep /^$module_dir\/?$/, @INC;
print "Warning:  \$util_dir points to $util_dir, which is not in \$PATH\n"
  unless $ENV{PATH} =~ /:$util_dir\/?:/;

# Create directory $module_dir/Apache/LowCal if it doesn't already exist
unless (-d "$module_dir/Apache/LowCal") {
  unless (-d "$module_dir/Apache") {
    die "No write permission for $module_dir\n" unless -w $module_dir;
    mkdir "$module_dir/Apache"
      || die "Failed to create $module_dir/Apache: $!\n";
  }

  die "No write permission for $module_dir/Apache\n"
    unless -w "$module_dir/Apache";
  mkdir "$module_dir/Apache/LowCal"
    || die "Failed to create $module_dir/Apache/LowCal: $!\n";
}

$module_dir .= "/Apache/LowCal";

# Copy all *.pm files to $module_dir/Apache/LowCal
foreach (<*.pm>) {
  print "Copying $_ to $module_dir\n";
  copy $_, $module_dir || die "Unable to copy files to $module_dir: $!\n";
}

# Copy all files from utils subdir to $util_dir
foreach (<utils/*>) {
  print "Copying $_ to $util_dir\n";
  copy $_, $util_dir || die "Unable to copy files to $util_dir: $!\n";
}

print "LowCal has been installed successfully.\n";
