#!/usr/bin/perl

$irvmajor = 0;
$irvminor = 1;
$irvrelease = 11;

$irm = "irmanager";
$tmp = "/tmp/signalconf-".$ENV{USER}.".tmp";

$version = `$irm -V 2>&1`;
if ($?) {
	$irm = "./irmanager";
	$version = `$irm -V 2>&1`;
	if ($?) {
		die("IRManager not found in path or current directory");
	}
}

if ($version=~/version ([0-9]*)\.([0-9]*)\.([0-9]*)[^0-9]/) {
	$v = $1*65536+$2*256+$3;
	if ($v < $irvmajor*65536+$irvminor*256+$irvrelease) {
		die("This script requires IRManager $irvmajor.$irvminor.$irvrelease or higher ($1.$2.$3 won't cut it)");
	}
}
else {
	die("Invalid version string from IRManager");
}

$dia = "dialog";
$diadef = "--title \"IRManager Signal Capture\"";
system("$dia 2> /dev/null");
if ($?==32512) {
	die("This script requires 'dialog' to be in the system path");
}

if (scalar(@ARGV)>1) {
	die "Too many parameters";
}

if (scalar(@ARGV)) {
	($sigfile) = @ARGV;
}
else {
	$sigdir = $ENV{'HOME'}."/.irmanager";
	if (! -d $sigdir) {
		mkdir($sigdir,oct("770")) or die "Unable to create directory \'$sigdir\' ($!)";
	}
	$sigfile = $sigdir."/signals";
}

if (! -e $sigfile) {
	system("$dia $diadef --yesno \"Signal file \'$sigfile\' does not exist. Create it?\" 8 40");
	if ($?) {
		die("Script aborted");
	}
	open SF, ">$sigfile" or die "Unable to create file \'$sigfile\'";
	close SF;
}
else {
	die("\'$sigfile\' is not a regular file") if (! -f $sigfile);
	die("File \'$sigfile\' is not writable") if (! -w $sigfile);
	open SF, $sigfile;
	while (<SF>) {
		chomp;
		if ($_=~/^(.*?)\/\//) {
			$_ = $1;
		}
		if ($_=~/^[ \t]*signal[ \t]+([a-z0-9\-]+)[ \t]*=[ \t]*0x([0-9a-f]+)[ \t]*;[ \t]*$/i) {
			$codetable{$2} = $1;
		}
		else {
			if ($_=~/^[ \t]*serial[ \t]*=[ \t]*\"([^\"]*)\"[ \t]*;[ \t]*$/) {
				$serial = $1;
			}
			else {
				die("Parse error \'$_\' in old signal file") if (!($_=~/^[ \t]*$/));
			}
		}
	}
	close SF;
}

$serial = "/dev/ttyS0" if (!$serial);

system "$dia $diadef --inputbox \"What is the tty (or pty) for the IRMan device?\" 9 40 $serial 2> $tmp";
die "Canceled" if ($?);
$serial = `cat $tmp`;

open IR, "$irm -ndlt - \'$serial\' |" or die "Unable to start IRManager";

$SIG{INT} = $SIG{QUIT} = exitscript;

while (true) {
	system "clear";
	printf "Now capturing signals from your remote. Press Ctrl-C to finish.\n";
	$line = <IR>;
	print "$line\n";
	if (!$line) {
		&exitscript;
	}
	if ($line=~/0x([0-9a-f]+)/) {
		$code = $1;
		if (not exists $codetable{$code}) {
			system "$dia $diadef --menu \'Unknown signal 0x$code".
			" received. What shall I do with it?\' 10 40 2 a Add i".
			" Ignore 2> $tmp";
			if (`cat $tmp` eq "a") {
				system "$dia $diadef --inputbox \'Please type".
				" a name for signal 0x$code:\' 9 40 2> $tmp";
				if (`cat $tmp`) {
					$codetable{$code} = `cat $tmp`;
				}
			}
		}
		else {
			system "$dia $diadef --menu \'Already known signal:".
			" $codetable{$code} ($code) was received.".
			" What shall I do with it?\' 11 40 3 i Ignore".
			" r Rename d Delete 2> $tmp";
			if (`cat $tmp` eq "r") {
				system "$dia $diadef --inputbox \'Please type".
				" a new name for signal: $codetable{$code}".
				" (0x$code)\' 9 40 2> $tmp";
				if (`cat $tmp`) {
					$codetable{$code} = `cat $tmp`;
				}
			}
			else {
				if (`cat $tmp` eq "d") {
					delete $codetable{$code};
				}
			}
		}
	}
}


sub exitscript {
	system "clear";
	system "$dia $diadef --yesno \'Save before exiting?\' 7 40";
	if (!$?) {
		open SF, ">$sigfile" or die "Unable to create file \'$sigfile\'.";
		print SF "serial = \"$serial\";\n";
		foreach (keys %codetable) {
			print SF "signal $codetable{$_} = 0x$_;\n";
		}
		close SF;
		printf "Signals saved. Remember you need to #include the signalfile you have just created in your main configuration file!\n";
	}
	unlink $tmp;
	exit 0;
};

