Git init
[pkgs/e/elektra.git] / scripts / convert-hwconfKudzu
1 #!/usr/bin/perl
2
3 ########################################################################
4 ##
5 ## This script will create a registry tree based on the detected HW
6 ## located in /etc/sysconfig/hwconf. That file is parsed and
7 ## re-generated at every boot by kudzu.
8 ##
9 ## This script must calculate a unique ID for each device. It can't
10 ## be sequential because sequence may change. It can't be only the
11 ## description of device, because you may have 2 identical devices.
12 ##
13 ## It will generate 2 trees for faster searches:
14 ##    system/hw/byClass    and
15 ##    system/hw/byBus
16 ## The entries in the second tree are links to the first.
17 ##
18 ## The correct way to run it is:
19 ##
20 ## bash# ./hwconfKudzu-convert | sh -e
21 ##
22 ##
23 ## To make tests you can do:
24 ##
25 ## bash$ ROOT=user/test ./hwconfKudzu-convert | sh -e
26 ##
27 ## Avi Alkalay <avi@unix.sh>
28 ## Apr 2004
29 ##
30 ## $Id$
31 ##
32 ########################################################################
33
34
35 $HWFILE="/etc/sysconfig/hwconf";
36 $ROOT="system/hw";
37 $RG="kdb";
38
39
40
41 sub printEntry {
42         if (%entry) {
43                 $entry{desc}=~s/^\"(.*)\"$/$1/; # remove "
44                 
45                 $uniqDevID=$entry{desc};
46                 $uniqDevID=~s/[\W|\s]*//g;
47                 if (defined($entry{pcifn})) {
48                         $uniqDevID.=$entry{pcifn};
49                 } elsif (defined($entry{usbbus})) {
50                         $uniqDevID.=$entry{usbbus};
51                 } elsif (defined($entry{device})) {
52                         $uniqDevID.=$entry{device};
53                 } else {
54                         printf STDERR "Can't calculate unique identifier for \"$entry{desc}\"\n";
55                         return;
56                 }
57                 
58                 # Some escaping and treatments
59                 $entry{desc}=~s/\'/\\'/g;       # escape '
60                 $entry{desc}=~s/\"/\\"/g;       # escape "
61                 $entry{desc}=~s/\(/\\\(/g;      # escape (
62                 $entry{desc}=~s/\)/\\\)/g;      # escape )
63                 $entry{desc}=~s/\|/\\\|/g;      # escape |
64                 $entry{desc}=~s/\#/\\\#/g;      # escape #
65                 
66                 foreach $key (keys(%entry)) {
67                         print("$RG set $ROOT/byClass/$entry{class}/$uniqDevID/$key " );
68                         print("-- $entry{$key}\n");
69                 }
70
71                 # Make link for the 'byBus' tree
72                 # print("$RG set -t dir \"$ROOT/byBus/$entry{bus}\"\n");
73                 print("$RG ln \"$ROOT/byClass/$entry{class}/$uniqDevID\" \"$ROOT/byBus/$entry{bus}/$uniqDevID\"\n\n");
74         }
75 }
76
77
78
79 open(HWFILE);
80
81 while(<HWFILE>) {
82         SWITCH: {
83                 if (/^-$/) {
84                         printEntry(%entry);
85                         undef(%entry);
86                         last SWITCH;
87                 }
88 #               if (/^class\:\s*(.*)/) {
89 #                       $entry{class}=$1;
90 #                       last SWITCH;
91 #               }
92 #               if (/^bus\:\s*(.*)/) {
93 #                       $entry{bus}=$1;
94 #                       last SWITCH;
95 #               }
96                 /(.*)\:\s+(.*)/;
97                 $entry{$1}=$2;
98         }
99 }
100
101 close(HWFILE);
102
103 # Handle last entry
104 printEntry(%entry);