#!/usr/bin/perl

# (C) Till Kamppeter - Code under GPL (www.gnu.org).

# Easy starting of xojpanel with auto-detection of available devices.

# Read the HPOJ config file and search for configured devices. Check
# whether they have a display and start "xojpanel" for the devices
# with display.

my $hpoj_config = "/etc/ptal-start.conf";
if (open HPOJCONFIG, ("< $hpoj_config")) {
    while (my $line = <HPOJCONFIG>) {
	chomp $line;
	# Comment or blank line
	next if (($line =~ /^\s*\#/) || ($line =~ /^\s*$/));
	# Only lines beginning with "ptal-mlcd" are interesting.
	next if ($line !~ /^\s*ptal-mlcd\s+(\S+)\s+/);
	my $ptaldevice = "mlc:$1";
	# Has the device a display? Check it by trying to read it with
	# the command line tool
	open LCD, ("ptal-hp $ptaldevice display 2>&1 |") or next;
	my $lcdcontent = join('', <LCD>);
	close LCD;
	next if ($lcdcontent =~ /^\s*\(unavailable\)\s*$/m);
	# Start xojpanel for this device
	system("xojpanel $ptaldevice >/dev/null 2>&1 &");
    }
    close HPOJCONFIG;
}
