#!/usr/bin/perl -w
use strict;
use Data::Dumper;

my $file = join '', <DATA>;
die "No data in file." unless $file;
$file =~ m/begin raw_codes(.*)end raw_codes/sm;
die "Unable to find raw_codes block in file." unless $1; 
my $raw_codes = $1;

my %keys = ($raw_codes =~ /name\s+([^\n]+)((?:\s+\d+)+)/gsm);

my $smallest;
for my $key (sort keys %keys) {
    my $ptext = $keys{$key};
    $ptext =~ s/^\s+//;
    $ptext =~ s/\s+$//;
    $ptext =~ s/\s+/ /g;

    my @periods = split /\s+/, $ptext;
    $keys{$key} = \@periods;

    # Scale to some sort of sane size:
    my $total = 0;
    for my $time (@periods) {
	if (!defined $smallest or $time < $smallest) {
	    $smallest = $time;
	}
    }
}

my $factor = 2/$smallest;
print "        __   __\n";
print "Scale: (  |_|  is a $smallest us pulse)\n";
    
for my $key (sort {$keys{$a}[0] <=> $keys{$b}[0]} keys %keys) {
    my $state = 1;
    
    my $l1 = ' 'x20;
    my $l2 = $key;
    while (length($l2) < 6) {
	$l2 .= ' ';
    }
    my $period = int(($keys{$key}[0]+$keys{$key}[1]) / 200)/10;
    $l2 .= "$period ms";
    while (length($l2) < 20) {
	$l2 .= ' ';
    }

    $l2 .= '    ';
    $l1 .= '____';
    
    for my $time (@{$keys{$key}}) {
	$state = $state ? 0 : 1;
	my $l = ($time * $factor) -1;
	my $u = '_'x$l;
	my $s = ' 'x$l;
	
	if ($state) {
	    $l1 .= " $u";
	    $l2 .= "|$s";
	    
	} else {
	    $l1 .= " $s";
	    $l2 .= "|$u";
	}
    }

    $l2 .= '|';
    $l1 .= ' ';
    while(length($l1) < 100) {
	$l1 .= '_';
    }
    print "$l1\n$l2\n";
}


__DATA__
#
# this config file was automatically generated
# using lirc-0.6.3 on Thu Mar 29 17:41:59 2001
#
# contributed by Christoph Bartelmus
#
# brand:             BLAUPUNKT
# model:             RC10
# supported devices: car remote control
#

begin remote

  name   BLAUPUNKT
  flags RAW_CODES
  eps             0
  aeps          200

  ptrail          0
  repeat     0     0
  gap    109400

      begin raw_codes

          name SRC
             6875    6850     660

          name SRC+
             8050    8050     660

          name UP
             3850    3850     660

          name OK
             7500    7400     660

          name DOWN
             4450    4400     660

          name RIGHT
             5660    5630     660

          name LEFT
             6280    6200     660

          name V+
             2650    2580     660

          name V-
             3250    3200     660

          name MUTE
             5050    5000     660

          name .
             2050    2000     660

      end raw_codes

end remote
