/lib/AwesomeGrid/Config.pm
package AwesomeGrid::Config;
use strict;

use AwesomeGrid;

use Sys::Hostname;

# Return a list of trusted machines
sub trusted {
	open PEERS, "$AwesomeGrid::confdir/trusted";
	my @peers;
	while (<PEERS>) {
		chomp;
		s/#.*$//;
		next if /^$/;
		push @peers, $_;
	}
	close PEERS;
	return @peers;
}

# Return a list of exported users
sub exports {
	open EXPORTS, "$AwesomeGrid::confdir/exports";
	my @exports;
	while (<EXPORTS>) {
		chomp;
		s/#.*$//;
		next if /^$/;
		push @exports, $_;
	}
	close EXPORTS;
	return @exports;
}

# Return a hashref of admin IDs for each machine.
sub admins {
	my %admins;
	open ADMINS, "$AwesomeGrid::confdir/admins";
	while (<ADMINS>) {
		chomp;
		my ($machine, $admin) = split(/\s+/);
		$admins{$machine} = $admin;
	}
	close ADMINS;
	return \%admins;
}

# Return a hashref of values from awesomegrid.conf
sub config {
	my %config;
	open CONFIG, "$AwesomeGrid::confdir/awesomegrid.conf";
	while (<CONFIG>) {
		s/#.*$//;
		next if /^\s+$/;
		if (/^([A-Za-z0-9_-]+):\s+(.*)$/) {
			$config{$1} = $2;
		} else {
			print "Config error: line $.\n";
		}
	}
	close CONFIG;
	return \%config;
}

# Return the short hostname for this host, or undef if the hostname is
# weird
sub hostname {
	my $hostname = Sys::Hostname::hostname();
	if (($hostname =~ /^([a-z0-9][a-z0-9-]*)(\.|$)/) && $1 !~ /-$/) {
		return $1;
	} else {
		return undef;
	}
}

1;