Init basesystem source codes.
[staging/basesystem.git] / systemservice / config / library / system_manager_config / order / launch_order_xml2cfg.pl
1 #!/usr/bin/perl -w 
2 use strict;
3
4 use XML::XPath;
5 use File::Copy;
6
7 my $main_file="launch_order_main.xml";
8 my $body_file="launch_order_body.xml";
9
10
11 sub usage(){
12   print STDERR "USAGE:$0 *.order *.cfg\n"
13 }
14     
15
16
17
18 #=== MAIN ===
19 my $ret=system("which xmllint > /dev/null");
20 if($ret != 0){
21     print STDERR  "It need xmllint. Please install it.\n";
22     exit 1;
23 }
24
25 if(2 != @ARGV){
26     usage();
27     exit 1;
28 }
29
30 my $infile=$ARGV[0];
31 my $launchfile=$ARGV[1];
32
33
34 #ARG CHECK
35 if($infile !~ /.+\.order$/){
36   print STDERR "$infile is not *.order\n";
37   exit 1;
38 }elsif(! -e $infile){
39   print "$infile not found\n";
40   usage();
41   exit 1;
42 }
43
44 if($launchfile !~ /.+\.cfg$/){
45   print STDERR "$launchfile is not *.cfg\n";
46   exit 1;
47 }elsif(! -e $launchfile){
48   print "$launchfile not found\n";
49   usage();
50   exit 1;
51 }
52
53
54 system("cp -f $infile $body_file");
55
56 #check xml vaild 
57 $ret=system("xmllint --noout --valid $main_file");
58 if($ret != 0){
59   print STDERR "XML is Invalid. \n";
60   exit 1;
61 }
62
63 my $date=`LANG=en date`;
64 print "#  This file is created from $infile and $launchfile\n";
65 print "#  created date : $date\n";
66
67 my %gnamemap;
68 my %gidmap;
69
70 open(FIN,"<$launchfile")  or die("error :$!");
71 while (my $line = <FIN>){
72   if($line =~ /^Launch/){
73     $line =~ s/^[^|]+=//g;
74     my @args =  split(/\|/,$line);
75     $gnamemap{$args[0]}=$args[1];
76     $gidmap{$args[1]}=$args[0];
77   }
78 }
79 close(FIN);
80
81 foreach my $key (sort{$a <=> $b} keys(%gidmap)){
82    print "#$key:$gidmap{$key}\n";
83 }
84
85
86 my $xml = XML::XPath->new(filename=>$main_file);
87
88 my $orders = $xml->find('/launch_order/order');
89 foreach my $order ($orders->get_nodelist) {
90   my $groups = $order->find('./group');
91   
92   my $oname=$order->findvalue('@oname');
93   my $fixed_group=$order->findvalue('@fixed_group');
94
95   print "[$oname]\n";
96   print "order=";
97   
98   my %l_gidmap = %gidmap;
99   my $sep="";
100   #fix groups
101   if($fixed_group ne "NULL"){
102     if(! exists $gnamemap{$fixed_group}){
103       die("$fixed_group not found");
104     }
105     foreach my $gid (sort{$a <=> $b} keys(%l_gidmap)){
106       print "$sep$gid";
107       $sep="|";
108       my $str=$l_gidmap{$gid};
109       delete $l_gidmap{$gid};
110       if($str eq $fixed_group){
111         last;
112       }
113     }
114   }
115
116   # re-order group via *.order
117   my %waitow;
118   foreach my $group ($groups->get_nodelist) {
119     my $gname=$group->findvalue('@gname');
120     my $waittime=XML::XPath::Number::value($group->findvalue('@waittime'));
121     
122     my $gid=$gnamemap{$gname};
123     
124     if(! exists $gnamemap{$gname}){
125       die("$oname:$gname is not defined");
126     }
127
128     if(! exists $l_gidmap{$gid}){
129       die("$oname:$gname is multiple defined");
130     }
131
132     print "$sep$gid";
133     $sep="|";
134     delete $l_gidmap{$gid};
135     
136     if($waittime > 0){
137       $waitow{$gname} = $waittime;
138     }
139   }
140   
141 #  Output waittime configuration
142
143   #out rest groups
144   foreach my $gid (sort{$a <=> $b} keys(%l_gidmap)){
145     print "$sep$gid";
146     $sep="|";
147   }
148   
149   print "\n";
150   if(keys(%waitow) > 0 ){
151     print "owlist=";
152     $sep="";
153     foreach my $owgroup ( keys(%waitow)) {
154         print "${sep}oww_${owgroup}";
155         $sep="|";
156     }
157     print "\n";
158     
159     foreach my $owgroup ( keys(%waitow)) {
160         print "oww_${owgroup}=$waitow{$owgroup}\n";
161     }
162
163     
164   }
165   print "\n";
166 }
167
168
169
170
171