summaryrefslogtreecommitdiffstats
path: root/convert/merge_MonitorsDB.pl
blob: 7a24cfe39958c63859352b91494e626eb7de9339 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/perl

# ./merge_MonitorsDB.pl ../../monitor-edid/test/new_MonitorsDB ../lst/MonitorsDB > MonitorsDB.new
# diff -u ../lst/MonitorsDB MonitorsDB.new

@ARGV == 2 or die "usage: $0 [--verif | <entries to add>] <mdk MonitorsDB>\n";

my ($to_add, $MonitorsDB) = @ARGV;
my $verif = $to_add eq '--verif';


use MDK::Common;

my $monitors_db = readMonitorsDB($MonitorsDB);

if ($verif) {
    verif($monitors_db);
} else {
    my $to_add = readMonitorsDB($to_add);

    rationalize_EISA_ID($_) foreach @$to_add, @$monitors_db;

    my %EISA_ID_to_VendorName = EISA_ID_to_VendorName($monitors_db);
    foreach (@$to_add) {
	my $VendorName = $_->{EISA_ID} =~ /(...)/ && $EISA_ID_to_VendorName{$1} or next;

	$_->{ModelName} =~ s/^(\Q$_->{VendorName}\E|\Q$VendorName\E)\s//i;
	$_->{VendorName} = $VendorName;
	$_->{ModelName} = "$VendorName $_->{ModelName}";
	$_->{text} = join_line($_);
    }

    #- first see if the EISA_ID is already there
    my %to_add = map { $_->{EISA_ID} => $_ } @$to_add;
    foreach (@$monitors_db) {
	$to_add{$_->{EISA_ID}} or next;

	$_->{text} =~ s!.*\n$!$to_add{$_->{EISA_ID}}{text}!;
	delete $to_add{$_->{EISA_ID}};
    }

    my @to_add = sort { lc($a->{text}) cmp lc($b->{text}) } values %to_add;
    foreach (@$monitors_db) {
	while (@to_add && lc("$to_add[0]{VendorName}; $to_add[0]{ModelName}") lt lc("$_->{VendorName}; $_->{ModelName}")) {
	    $_->{text} = (shift @to_add)->{text} . $_->{text};
	}
    }

    print $_->{text} foreach (@$monitors_db, @to_add);
}

sub verif {
    my ($monitors_db) = @_;
    
    my %l; 
    foreach my $e (@$monitors_db) {	
	rationalize_EISA_ID($e, sub {
	    my ($previous) = @{$l{$e->{EISA_ID}} || []} or return;
	    my @vs = map { 
		$e->{$_} eq $previous->{$_} ? () : "$e->{$_} vs $previous->{$_}";
	    } 'ModelName', 'HorizSync', 'VertRefresh';
	    warn "$e->{lineno}: duplicate $e->{EISA_ID}: " . (@vs ? join(", ", @vs) : $e->{ModelName}) . "\n";
	});

	push @{$l{$e->{EISA_ID}}}, $e;
    }
}

sub rationalize_EISA_ID {
    my ($e, $o_verif) = @_;

    if ($e->{EISA_ID} =~ /^([A-Z]{3})([0-9a-f]{4})$/i) {
	# perfect!
	$e->{EISA_ID} = uc($1) . lc($2);
	$o_verif and $o_verif->();
    } elsif ($e->{EISA_ID} eq '0') {
	# ok
    } elsif ($e->{EISA_ID} =~ /^([a-z]{3})([0-9a-f]{0,3})$/i) {
	# we can correct this (?)
	warn sprintf("$e->{lineno}: $e->{EISA_ID} should be %s%04x\n", $1, hex($2)) if $verif;
    } else {
	warn "$e->{lineno}: bad EISA_ID $e->{EISA_ID}\n" if $o_verif;
    }
}

sub EISA_ID_to_VendorName {
    my ($monitors_db) = @_;

    my %l;
    foreach (@$monitors_db) {
	$_->{EISA_ID} =~ /(...)/ and $l{$1}{$_->{VendorName}}++;
    }
    map {
	my $v = $l{$_};
	my @l = sort { $v->{$b} <=> $v->{$a} } keys %$v;
	$_ => $l[0];
    } keys %l;
}

BEGIN {
    our @fields = qw(VendorName ModelName EISA_ID HorizSync VertRefresh dpms);
}

sub join_line {
    my ($e) = @_;
    join('; ', @$e{@fields}) . "\n";
}
sub split_line {
    my ($s) = @_;
    my %l; @l{@fields} = split(/\s*;\s*/, $s);
    \%l;
}

sub readMonitorsDB {
    my ($f) = @_;
    my $s = '';
    my @monitors_db;
    my $lineno = 0;
    foreach (cat_($f)) {
	$lineno++;
	$s .= $_;
	s/\s+$//;
	/^#/ and next;
	/^$/ and next;

	my $e = split_line($_);
	($e->{text}, $s) = ($s, '');
	$e->{lineno} = $lineno;
	push @monitors_db, $e;
    }
    \@monitors_db;
}