/install.pl
#!/usr/bin/perl

use strict;
use warnings;

use Config;
use Sys::Hostname;
use File::Copy;
use File::Path;

my $etc = '/etc';
my $bin = '/usr/bin';
my $sbin = '/usr/sbin';

# This should work better.
my $perllib = $Config{sitelib};

my $hostname = hostname();
if (($hostname =~ /^([a-z0-9][a-z0-9-]*)(\.|$)/) && $1 !~ /-$/) {
	$hostname = $1;
} else {
	print <<EOD;

Your hostname ($hostname) is weird, and is probably
not a valid internet hostname.  A valid hostname uses only alphanumeric
characters and hyphens.  The hostname cannot begin or end with a hyphen.

EOD
	exit 1;
}

if ($> != 0) {
	print <<EOD

You are not running with root privs. This could make installation problematic 
if you're using the default paths.
EOD
}

print <<EOD;

This script will install the AwesomeGrid perl libraries, utilities, and
configuration. A short summary is printed below. If this doesn't look
right to you, please edit this script.

Configuration will be installed to:
	$etc/awesomegrid

User binaries will be installed to:
	$bin

Superuser binaries will be installed to:
	$sbin

Perl libraries will be installed to:
	$perllib

EOD

# Make sure that $perllib is actually in the @INC path, and warn if not.
unless (grep $perllib, @INC) {
  print <<EOD;
Caution!  $perllib not found in your \@INC path.
AwesomeGrid may not run correctly during and/or after installation!

EOD
}

print "Continue? [Y/n] ";
my $ans = <STDIN>;
unless ($ans =~ /^([Yy]|)$/) {
	print "Aborting.\n";
	exit 0;
}

print "Installing user binaries...\n";
mkpath($bin) unless -d $bin;
for my $file (qw/ag-passwd/) {
	print "$file => $bin/$file\n";
	copy($file, "$bin/");
	chmod 0755, "$bin/$file";
}

print "Installing superuser binaries...\n";
mkpath($sbin) unless -d $sbin;
for my $file (qw/ag-export ag-flush ag-import ag-keyring ag-listusers ag-update-shadow ag-update-openbsd ag-useradd ag-userdel/) {
	print "$file => $sbin/$file\n";
	copy($file, "$sbin/");
	chmod 0700, "$sbin/$file";
}

print "Installing perl libraries...\n";
mkpath("$perllib/AwesomeGrid") unless -d "$perllib/AwesomeGrid";
print "lib/AwesomeGrid.pm => $perllib/AwesomeGrid.pm\n";
copy("lib/AwesomeGrid.pm", "$perllib/");
for my $file (qw/Config Keyring User/) {
	print "lib/AwesomeGrid/$file.pm => $perllib/AwesomeGrid/$file.pm\n";
	copy "lib/AwesomeGrid/$file.pm", "$perllib/AwesomeGrid/";
}

# Be extra careful for configuration bits
umask 0077;

print "Installing configuration...\n";
mkpath("$etc/awesomegrid") unless -d "$etc/awesomegrid";
unless (-f "$etc/awesomegrid/admins") {
	open ADMINS, ">$etc/awesomegrid/admins";
	print ADMINS <<EOD;
# hostname	admin GPG id
$hostname	foo\@bar.com
EOD
	close ADMINS;
}
unless (-f "$etc/awesomegrid/exports") {
	open EXPORTS, ">$etc/awesomegrid/exports";
	print EXPORTS <<EOD;
# List here, one per line, users to export to other trusted systems (as
# defined in the 'trusted' file)
EOD
	close EXPORTS;
}
unless (-f "$etc/awesomegrid/trusted") {
	open TRUSTED, ">$etc/awesomegrid/trusted";
	print TRUSTED <<EOD;
# List here, one per line, trusted systems (short hostname)
EOD
	close TRUSTED;
}
unless (-f "$etc/awesomegrid/awesomegrid.conf") {
	open CONF, ">$etc/awesomegrid/awesomegrid.conf";
	print CONF <<EOD;
# The GPG id of the admin of this machine
gpg-id: foo\@bar.com
# The place to install new home directories for imported users
homedir: /home
# The shell to give imported users if they don't specify one
default-shell: /bin/bash
EOD
	my $gpg = `which gpg`;
	if ($? >> 8 != 0) {
		print <<EOD;
I couldn't find gpg on your system. You'll have to edit
$etc/awesomegrid/awesomegrid.conf and put in the
location of gpg yourself.
EOD
	} else {
		print CONF "gpg: $gpg\n";
	}
	close CONF;
}
mkdir "$etc/awesomegrid/users" unless -d "$etc/awesomegrid/users";
unless (-d "$etc/awesomegrid/keyring") {
	print <<EOD;
It appears that you don't have an AwesomeGrid keyring. You will need one
to export users to other systems (and have other admins import from
you).

EOD
	print "Shall I run 'ag-keyring initialize' to generate a new keyring? [Y/n] ";
	my $ans = <STDIN>;
	if ($ans =~ /^([Yy]|)$/) {
		$ENV{AWESOMEGRID_CONFDIR} = "$etc/awesomegrid";
		system("$sbin/ag-keyring initialize");
	}
}

print <<EOD;

Installation finished. Please look over the configuration in
$etc/awesomegrid and customize it to suit your system.
EOD
__END__

=head1 NAME

install.pl - Install AwesomeGrid

=head1 SYNOPSIS

install.pl

=head1 DESCRIPTION

Installs AwesomeGrid on the current system, with some basic sanity checking.
Currently, it is pretty simple, and verified only on Slackware, Debian (and
variants), and OpenSolaris.

=head1 OPTIONS

None.

=head1 BUGS

Too many.  Far, far too many.

=head1 AUTHOR

Chip Black (bytex64@bytex64.net), Matt Erickson (peawee@peawee.net)

=cut