#!/usr/bin/perl



system("gunzip -f *gz mini/*gz");
foreach my $top (qw(. mini)) {
    foreach my $file (glob("$top/*zip")) {
	   system("yes A | unzip -q $file && rm -f $file");
    }
    foreach my $file (glob("$top/*tar")) {
	   my $dir = $file;
	   $dir =~ s/.tar$//;
	   $dir =~ s/(-html|.html|-HOWTO)*//g;
	   mkdir $dir;
	   # handle twice compressed files such as Astronomy-HOWTO-html.tar
	   system("file $file | fgrep -q gzip && (mv " . $file . "{,.gz}; gunzip $file.gz)");
	   system("( tar xf $file -C $dir && rm -f $file ) || echo \"$file is broken\"");
#	   my @list = glob("$dir/*");
#	   system("echo $dir; mv @list $dir/..; rmdir $dir") if $#list eq 0;
	   my @list = glob("$dir/*/*");
	   system("echo $dir; mv @list $dir/; rmdir $dir/*/") if @list;
    }
    foreach my $file (glob("$top/*.html_single")) {
	   my $new_file = $file;
	   $new_file =~ s/_single$//g;
	   rename($file, $new_file);
    }
}

# remove broken links mirrored from FTP:
unlink($_) foreach glob("Linux-html-HOWTOs.*");

#system("tar cfj ~/rpm/SOURCES/howto-html.tar.bz2 .");
fork_("tar cfj $ENV{HOME}/rpm/SOURCES/howto-html.tar.bz2 .");

sub fork_ {
    my ($prog) = @_;
    my $pid;
    unless ($pid = fork()) {
        print STDERR "cannot fork: %s", "$!" unless defined $pid;
        exec($prog);
    }
}
