package server;

use warnings;
use strict;


our $ip      = '172.16.10.1';
our $netmask = '255.255.255.0';
our $bcast   = '172.16.10.255';

sub setup_from_dev {
	my $dev = shift || return;
	my $ifconfig = `/sbin/ifconfig $dev`;
	( $ip, $bcast, $netmask ) = ( $1, $2, $3 ) if $ifconfig =~ m{inet addr:(\S+)\s+Bcast:(\S+)\s+Mask:(\S+)}s;
	warn "DEV $dev $ip $bcast $netmask";
}

setup_from_dev( $ENV{DEV} || 'virtual' );

our ( $ip_from, $ip_to ) = ( 10, 100 );

our $domain_name = 'pxelator.lan';

our $base_dir = '/srv/pxelator';

our $conf = "$base_dir/conf/$ip";
mkdir $conf unless -e $conf;

use Module::Refresh qw//;
sub refresh {
	Module::Refresh->refresh;
	my $from = (caller(1))[3];
	$from =~ s{^(\w+)::.+$}{$1};
	my $eval = '$' . $from . '::debug = server::debug();';
	warn "refresh $eval\n";
	eval $eval;
	warn $@ if $@;
};

mkdir $_ foreach grep { ! -d $_ } map { "$conf/$_" } ( 'ip', 'mac' );

use File::Slurp;
sub shared {
	my ($name, $value) = @_;

	my $path ="$conf/$name";
	if ( defined $value ) {
		write_file $path, $value;
		warn "update $path = $value";
	} else {
		$value = read_file $path if -e $path;
	}
	return $value;
}

sub conf_default { shared($_[0]) || $_[1] }

sub debug { shared('debug', @_) || 0 }

warn "loaded";

1;
