8 my $metainit_path = "./examples";
21 # Read in all meta-init scripts
22 my @metainit_files = <$metainit_path/*.metainit>;
26 for my $metainit_file (@metainit_files) {
27 my %data = Parse::parse($metainit_file);
28 $metainits{$data{Name}} = \%data
32 # Locate existing scripts
33 # We check for existing scripts only in /etc/rc2.d
36 for my $rc_file (<$etc/rc2.d/S??*>) {
37 my ($num, $name) = $rc_file =~ m!S(\d\d)([^/]+)!;
38 $startscripts{$name} = $num;
42 # Sort the scripts to existing and non-existing scripts
46 for my $metainit (keys %metainits) {
47 if (exists $startscripts{$metainit}) {
48 push @existing, $metainit;
54 # Check the dependencies for existing scripts
56 for my $existing (@existing) {
57 my @deps = @{$metainits{$existing}{"Required-Start"}};
59 if (exists $startscripts{$dep}) {
60 if ($startscripts{$dep} >= $startscripts{$existing}) {
61 warn "Late dependency $dep of $existing.\n"
63 } elsif (exists $facilities{$dep}) {
64 if ($facilities{$dep} >= $startscripts{$existing}) {
65 warn "Late facility $dep of $existing.\n"
67 } elsif (not exists $startscripts{$dep}){
71 warn "Unkown dependency $dep of $existing.\n";
85 for my $existing (@existing) {
86 my @deps = @{$metainits{$existing}{"Required-Start"}};
88 if (exists $startscripts{$dep}){
89 $upper{$dep} = min($startscripts{$existing}, $upper{$dep})
95 my @deps = @{$metainits{$new}{"Required-Start"}};
97 if (not exists $startscripts{$dep}){
98 $lower{$new} = $startscripts{$dep};
99 $lower{$new} = max($startscripts{$dep}, $lower{$new})
104 # Put a new scripts at appropriate location
105 my $current = pop @new;
108 if (($lower{$current}||0) > ($upper{$current}||99)) {
109 warn "Could not correctly fit in $current, please reorder manually.\n";
110 $startscripts{$current} = $lower{$current}||10 + 10;
111 push @processed, $current;
112 push @existing, $current;
113 } elsif (($lower{$current}||0 < 20) and not defined $upper{$current}) {
114 $startscripts{$current} = 20;
115 push @processed, $current;
116 push @existing, $current;
118 $startscripts{$current} = mid($lower{$current}, $upper{$current});
123 for my $done (@processed) {
124 printf "%s %02d\n", $done, $startscripts{$done}
129 return $_[1] if (defined $_[1] and $_[0] < $_[1]);
133 return $_[1] if (defined $_[1] and $_[0] > $_[1]);
138 return int(($a+$b)/2);