package screen;

use warnings;
use strict;
use autodie;

use File::Slurp;

use log;
use server;

our $debug = server::debug;

my $screenrc = '/tmp/screenrc';

sub screen_daemon {
	my $daemon = shift;
	my $run = "sudo perl -I$server::base_dir/lib/PXElator -I$server::base_dir/lib -M$daemon -e " . $daemon . "::start";
	warn "screen_daemon $daemon $run\n";
qq|

split

screen -t $daemon $run
logfile $log::dir/$daemon.log
log on

focus down
|}

write_file $screenrc, qq|

autodetach off
altscreen off
defscrollback 10000
startup_message off

sessionname PXElator

|
. screen_daemon('dhcpd')
. screen_daemon('tftpd')
. screen_daemon('dnsd')
#. screen_daemon('httpd')
. qq|remove|;
;

warn "screenrc $screenrc ", -s $screenrc if $debug;

sub start {

	my $screen="screen -R PXElator -c $screenrc";
	warn $screen;

	chdir $server::base_dir;

	if ( defined $ENV{DISPLAY} ) {
		exec "xterm -fg yellow -e $screen";
	} else {
		exec $screen;
	}

}

1;
