#!/usr/bin/perl
##
##  mirror-summary -- Create A Mirror Summary List
##  Copyright (c) 1998-2001 Ralf S. Engelschall <rse@engelschall.com>
##
##  Usage: mirror-summary 
##

## 
##  parse command line request
##

%R = {};
foreach $arg (@ARGV) {
    if ($arg =~ m|^(\S+)=(.+)$|) {
        $R->{$1} = $2;
    }
}

##
##  read mirror package configuration
##

#   read all package config files
@F = glob($R->{'pkgdir'}."/*");
$config = '';
foreach $f (sort(@F)) {
    open(FP, "<$f") || die;
    $config .= $_ while (<FP>);
    close(FP);
}
$config .= "\n\n";
$config =~ s|^#.+?||mg;

#   convert package config into internal structure
$P = {};
$config =~ s|package=(\S+).*?\n(.+?\n)\n|&do_package($1, $2), ''|sge;
sub do_package {
    my ($name, $config) = @_;
    $P->{$name} = {};
    $config =~ s|^\s*([a-zA-Z0-9_-]+)([+=].*?)$|$P->{$name}->{$1} = $2, ''|mge;
}

#   expand the key+value notation
foreach $e (keys(%{$P->{'defaults'}})) {
    $P->{'defaults'}->{$e} =~ s|^=||;
}
foreach $p (keys(%{$P})) {
    next if ($p eq 'defaults');
    foreach $e (keys(%{$P->{$p}})) {
        ($op, $val) = ($P->{$p}->{$e} =~ m|^([+=])(.*)$|);
        if ($op eq '+') {
            $P->{$p}->{$e} = $P->{'defaults'}->{$e}.$val;
        }
        else {
            $P->{$p}->{$e} = $val;
        }
    }
}

##
##  generate summary
##

#   place a string into a specific number of columns   
sub cutfill {
    my ($str, $col) = @_;
    if (length($str) > $col) {
        $str = substr($str, 0, $col-1)."+";
    }
    elsif (length($str) < $col) {
        $str = substr($str." "."."x$col, 0, $col);
    }
    return $str;
}

if ($R->{'excerpt'} ne '') {
    %O = ();
    foreach $p (sort(keys(%{$P}))) {
        next if ($p eq 'defaults');
        next if ($P->{$p}->{'skip'} eq 'yes');
        $what = $P->{$p}->{'comment'};
        if ($what =~ s|\s*\[\*\]||) {
            $from = "ftp://".$P->{$p}->{'site'}.$P->{$p}->{'remote_dir'};
            $to   = $P->{$p}->{'local_dir'};
            $to   =~ s|^$R->{'rootdir'}||;
            $O{$to} = sprintf("%-30s %-35s\n", &cutfill($to, 30), $what);
        }
    }
    $T = '';
    foreach $o (sort(keys(%O))) {
        $T .= $O{$o};
    }

    ($user, $group, $files, $begin, $prefix, $end) = split(':', $R->{'excerpt'});
    if ($files =~ m|.+\+.+|) {
        @F = split(/\+/, $files);
    }
    else {
        @F = ($files);
    }
    $T =~ s|^|$prefix|mg;
    foreach $file (@F) {
        open(FP, "<$file");
        $text = '';
        $text .= $_ while (<FP>);
        close(FP);
        $text =~ s|(.*$begin.*?\n).*\n(.*?$end.*)|$1$T$2|s;
        open(FP, ">$file.new");
        print FP $text;
        close(FP);
        system("cp $file.new $file && rm -f $file.new");
        system("chown $user:$group $file");
    }
}

if ($R->{'full'} ne '') {
    %O = ();
    foreach $p (sort(keys(%{$P}))) {
        next if ($p eq 'defaults');
        next if ($P->{$p}->{'skip'} eq 'yes');
        $what = $P->{$p}->{'comment'};
        $what =~ s|\s*\[\*\]||;
	$from = "ftp://".$P->{$p}->{'site'}.$P->{$p}->{'remote_dir'}."/";
	$from =~ s|/+$|/|;
	$to   = $P->{$p}->{'local_dir'}."/";
	$to   =~ s|^$R->{'rootdir'}|$R->{'rooturl'}|;
	$to   =~ s|/+$|/|;
	$O{$what}  = "$what\n";
	$O{$what} .= "    Origin: $from\n";
	$O{$what} .= "    Mirror: $to\n";
	$O{$what} .= "\n";
    }
    $T = "\n";
    foreach $o (sort(keys(%O))) {
        $T .= $O{$o};
    }

    ($user, $group, $file, $begin, $prefix, $end) = split(':', $R->{'full'});
    $T =~ s|^|$prefix|mg;
    open(FP, "<$file");
    $text = '';
    $text .= $_ while (<FP>);
    close(FP);
    $text =~ s|(.*$begin.*?\n).*\n(.*?$end.*)|$1$T$2|s;
    open(FP, ">$file.new");
    print FP $text;
    close(FP);
    system("cp $file.new $file && rm -f $file.new");
    system("chown $user:$group $file");
}

