#!/usr/bin/perl

#Copyright (C) 1999-2001 by  Sbastien Chaumat <schaumat@debian.org>
#                        and Loc Prylli <lprylli@lhpca.univ-lyon1.fr>

#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.

#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    A copy of the GNU General Public License is available as
#    `/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution
#    or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html.  You
#    can also obtain it by writing to the Free Software Foundation, Inc.,
#    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

use FileHandle;
use File::Path;
use File::Copy;

$spec = '/etc/rcd.spec';
$tmpsh = '/tmp/rcd.sh';

$START=0;
$STOP=1;

$dist = "/tmp/rcd-test/";
$dump = 0;

while (@ARGV) {
  $_ = shift @ARGV;
  if ($_ eq '--dump') {
    $dump = 1;
  } elsif ($_ eq '--destdir') {
    @ARGV > 0 || die "incomplete arg: --destdir\n";
    $dist = shift @ARGV;
  } elsif ($_ eq '--spec') {
    @ARGV > 0 || die "incomplete arg: --spec\n";
    $spec = shift @ARGV;
  } else {
    die "unknown option: $ARGV[0]\n";
  }
}

@typechar=("S","K");
%map=("S" => 1,
      "0" => 2,
      "6" => 3,
      "1" => 4,
      "2" => 5,
      "3" => 6,
      "4" => 7,
      "5" => 8);

if ($dump) {
  @dirs=`ls -d /etc/rc?.d`;
  foreach $d (@dirs) {
    chomp($d);
    print STDERR "$d\n";
    $d =~ m,/etc/rc(.)\.d, or die "dir:$d";
    my $level = $1;
    foreach $type (($START,$STOP)) {
      my $c = $typechar[$type];
      my @files = `ls $d/$c*`;
      foreach (@files) {
	chomp($_);
	m,^$d/$c(\d\d)([a-z-_.0-9]+)$, or die "bad entry:$_";
	my $prio = $1;
	$pack{$2}->[$type]->{$prio} ||= [];
	push @{$pack{$2}->[$type]->{$prio}},$level;
	if ($type == $START && (!$order{$2} or ($order{$2} lt "$map{$level}-$prio"))) {
	  $order{$2} = "$map{$level}-$prio-$2";
	}
#	my $levels = $pack{$2}->[$type]->{$prio};
#	print STDERR join(",",@$levels)."\n";
      }
    }
  }
  @keys = sort({ $order{$a} cmp $order{$b} } keys %pack);
  foreach $p (@keys) {
    print "$p ";
    foreach $type (($START,$STOP)) {
      foreach $prio (keys %{$pack{$p}->[$type]}) {
	print "$typechar[$type] $prio ";
	$levels = $pack{$p}->[$type]->{$prio};
	print join(",",@$levels)." ";
      }
    }
    print "\n";
  }
  exit 1;
}

my $f= new FileHandle "$spec" or die "$spec:$!";
my $out = new FileHandle ">$tmpsh" or die "$tmpsh:$!\n";

print $out "#!/bin/sh\n";

print $out "rm -rvf $dist/etc/rc?.d/*\n";

while (<$f>) {
  next if (/^\s*\#/);
  next if (/^\s+$/);
  @w = split(/\s+/,$_);
  $p = shift @w;
  while (@w >= 3) {
    my $type = shift @w;
    my $prio = shift @w;
    my $levels = shift @w;
    my @levels = split(",",$levels);
    foreach (@levels) {
      print $out "mkdir -p $dist/etc/rc$_.d\n";
      print $out "ln -s ../init.d/$p $dist/etc/rc$_.d/$type$prio$p\n";
    }
  }
}

$out->close;

$f->close;

system("sh $tmpsh");
