use strict;
use warnings;
+use List::Util qw(min max sum);
my $prefix = "";
$prefix = $ENV{METAINIT_PREFIX} if $ENV{METAINIT_PREFIX};
my @new_scripts = @ARGV;
my %facilities = (
- '$local_fs' => 0,
- '$network' => 0,
- '$named' => 15,
- '$portmap' => 18,
- '$remote_fs' => 21,
- '$syslog' => 10,
- '$time' => 0,
+ '$local_fs' => 0,
+ '$network' => 0,
+ '$named' => 15,
+ '$portmap' => 18,
+ '$remote_fs' => 21,
+ '$syslog' => 10,
+ '$time' => 0,
);
# Read in all meta-init scripts
my %metainits;
for my $metainit_file (@metainit_files) {
- my $data = MetaInit::Parse::parse($metainit_file);
- $metainits{$data->{Name}} = $data;
+ my $data = MetaInit::Parse::parse($metainit_file);
+ $metainits{$data->{Name}} = $data;
}
my %startscripts;
for my $rc_file (<$etc/rc2.d/S??*>) {
- my ($num, $name) = $rc_file =~ m!S(\d\d)([^/]+)!;
- $startscripts{$name} = $num;
+ my ($num, $name) = $rc_file =~ m!S(\d\d)([^/]+)!;
+ $startscripts{$name} = $num;
}
my @new;
for my $metainit (keys %metainits) {
- if (exists $startscripts{$metainit}) {
- push @existing, $metainit;
- } else {
- push @new, $metainit;
- }
+ if (exists $startscripts{$metainit}) {
+ push @existing, $metainit;
+ } else {
+ push @new, $metainit;
+ }
}
# Actually, we only want to treat the passed scripts as new
@new = @new_scripts;
-
+
# Check the dependencies for existing scripts
for my $existing (@existing) {
- my @deps = @{$metainits{$existing}{"Required-Start"}};
- for my $dep (@deps) {
- if (exists $startscripts{$dep}) {
- if ($startscripts{$dep} >= $startscripts{$existing}) {
- warn "Late dependency $dep of $existing.\n"
- }
- } elsif (exists $facilities{$dep}) {
- if ($facilities{$dep} >= $startscripts{$existing}) {
- warn "Late facility $dep of $existing.\n"
- }
- } elsif (not exists $startscripts{$dep}){
- # Nothing to do here
- } else {
-
- warn "Unkown dependency $dep of $existing.\n";
- }
- }
+ my @deps = @{$metainits{$existing}{"Required-Start"}};
+ for my $dep (@deps) {
+ if (exists $startscripts{$dep}) {
+ if ($startscripts{$dep} >= $startscripts{$existing}) {
+ warn "Late dependency $dep of $existing.\n"
+ }
+ } elsif (exists $facilities{$dep}) {
+ if ($facilities{$dep} >= $startscripts{$existing}) {
+ warn "Late facility $dep of $existing.\n"
+ }
+ } elsif (not exists $startscripts{$dep}){
+ # Nothing to do here
+ } else {
+
+ warn "Unkown dependency $dep of $existing.\n";
+ }
+ }
}
my @processed = ();
while (@new > 0) {
- # Check Bounds
-
- my %lower;
- my %upper;
-
- for my $existing (@existing) {
- my @deps = @{$metainits{$existing}{"Required-Start"}};
- for my $dep (@deps) {
- if (exists $startscripts{$dep}){
- $upper{$dep} = min($startscripts{$existing}, $upper{$dep})
- }
- }
- }
-
- for my $new (@new) {
- my @deps = @{$metainits{$new}{"Required-Start"}};
- for my $dep (@deps) {
- if (not exists $startscripts{$dep}){
- $lower{$new} = $startscripts{$dep};
- $lower{$new} = max($startscripts{$dep}, $lower{$new})
- }
- }
- }
-
- # Put a new scripts at appropriate location
- my $current = pop @new;
-
- # Standard case:
- if (($lower{$current}||0) > ($upper{$current}||99)) {
- warn "Could not correctly fit in $current, please reorder manually.\n";
- $startscripts{$current} = $lower{$current}||10 + 10;
- push @processed, $current;
- push @existing, $current;
- } elsif (($lower{$current}||0 < 20) and not defined $upper{$current}) {
- $startscripts{$current} = 20;
- push @processed, $current;
- push @existing, $current;
- } else {
- $startscripts{$current} = mid($lower{$current}, $upper{$current});
- }
+ # Check Bounds
+
+ my %lower;
+ my %upper;
+
+ for my $existing (@existing) {
+ my @deps = @{$metainits{$existing}{"Required-Start"}};
+ for my $dep (@deps) {
+ if (exists $startscripts{$dep}){
+ $upper{$dep} = min($startscripts{$existing}, $upper{$dep})
+ }
+ }
+ }
+
+ for my $new (@new) {
+ my @deps = @{$metainits{$new}{"Required-Start"}};
+ for my $dep (@deps) {
+ if (not exists $startscripts{$dep}){
+ $lower{$new} = $startscripts{$dep};
+ $lower{$new} = max($startscripts{$dep}, $lower{$new})
+ }
+ }
+ }
+
+ # Put a new scripts at appropriate location
+ my $current = pop @new;
+
+ # Standard case:
+ if (($lower{$current}||0) > ($upper{$current}||99)) {
+ warn "Could not correctly fit in $current, please reorder manually.\n";
+ $startscripts{$current} = $lower{$current}||10 + 10;
+ push @processed, $current;
+ push @existing, $current;
+ } elsif (($lower{$current}||0 < 20) and not defined $upper{$current}) {
+ $startscripts{$current} = 20;
+ push @processed, $current;
+ push @existing, $current;
+ } else {
+ $startscripts{$current} = mid($lower{$current}, $upper{$current});
+ }
}
for my $done (@processed) {
- printf "%s %02d\n", $done, $startscripts{$done}
+ printf "%s %02d\n", $done, $startscripts{$done}
}
-sub max {
- return $_[1] if (defined $_[1] and $_[0] < $_[1]);
- return $_[0]
-}
-sub min {
- return $_[1] if (defined $_[1] and $_[0] > $_[1]);
- return $_[0]
-}
sub mid {
- my ($a,$b) = @_;
- return int(($a+$b)/2);
+ return sum(@_) / scalar @_;
}
+
+# vim: sw=4:ts=4:expandtab
use MetaInit::Parse;
-$filename = shift || die "Filename expected!\n";
+$filename = shift || die "Useage: $0 <filename>\n";
my $output = shift;
# Parse the metainit in
%initparams = %{MetaInit::Parse::parse($filename)};
if ($output) {
- open(STDOUT,'>',$output) or die $!;
-}
-
-my $rl_start = "2 3 4 5";
-my $rl_stop = "0 1 6";
-
-if ($initparams{"No-Auto"}) {
- $rl_start = "";
- $rl_stop = "0 1 2 3 4 5 6";
+ open(STDOUT,'>',$output) or die "Can't open '$output': $!";
}
# Print the "dynamic" part of the initskript
#! /bin/sh
#
# This a generated file. DO NOT EDIT THIS FILE!
-# If you want to modify who this file works, please
+# If you want to modify how this file works, please
# modify $filename and re-run update-metainit.
#
# If you are sure that you want to modify this file,
my %quoted_initparams;
while (my ($k, $v) = each %initparams){
- $quoted_initparams{$k} = quotemeta ($v || '');
+ $quoted_initparams{$k} = quotemeta ($v || '');
}
print << "EOF"
EOF
if ($output) {
- close STDOUT;
- chmod 0755, $output or die $!;
-
+ close STDOUT;
+ chmod 0755, $output or die "Can't change permissions for '$output': $!";
}
+
+# vim: ts=4:sw=4:expandtab