#! /usr/bin/perl -w
use strict;

# Split the HOWTO tarballs into free and non-free components, based on the
# files in debian/copyrights.

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

if (@ARGV < 2) {
    print STDERR "Usage: $0 <type> <tar.bz2>\n";
    exit 1;
}

rmtree 'free', 0, 1;
mkdir 'free';
rmtree 'non-free', 0, 1;
mkdir 'non-free';

my $type = shift;

my (%free, %nonfree);

for my $status (qw(free non-free)) {
    opendir COPYRIGHTS, "debian/copyrights/$status"
	or die "Can't open debian/copyrights/$status directory: $!";
    for my $copy (readdir COPYRIGHTS) {
	next unless -f "debian/copyrights/$status/$copy";
	open COPYRIGHT, "< debian/copyrights/$status/$copy"
	    or die "Can't open debian/copyrights/$status/$copy: $!";
	while (<COPYRIGHT>) {
	    chomp;
	    last if /^$/;
	    next unless s/^\Q$type\E: //o;
	    if ($status eq 'free') {
		$free{$_} = 1;
	    } else {
		$nonfree{$_} = 1;
	    }
	}
	close COPYRIGHT;
    }
    closedir COPYRIGHTS;
}

mkdir 'tmp';

my $tar = shift;
system "tar -C tmp -xjf \Q$tar\E";
my $dir = 'tmp';
$dir = 'tmp/HOWTO' if -d 'tmp/HOWTO';
$dir = 'tmp/HOWTO/mini' if -d 'tmp/HOWTO/mini';

opendir FILES, $dir or die "Can't open $dir directory: $!";
my @files = grep !/^\.\.?$/, readdir FILES;
closedir FILES;

for my $file (@files) {
    (my $canon = $file) =~ s/(?:-\d+)?\.html$//;
    $canon =~ s/-\Q$type\E//;

    # These always go in the free package.
    if ($file =~ /^(?:COPYRIGHT|INDEX|README|images|index\.html)$/
	    or $file =~ /\.gif$/) {
	move "$dir/$file", "free/$file";
	next;
    }

    # Special cases (sigh).
    if ($canon =~ /^(?:C\+\+Programming|PostgreSQL)$/) {
	move "$dir/$file", "free/$file";
	next;
    } elsif ($canon eq 'HOWTO-INDEX') {
	# goes in both directories for the benefit of debian/html2docs
	system 'cp', '-a', "$dir/$file", "free/$file";
	move "$dir/$file", "non-free/$file";
	next;
    } elsif ($type eq 'HOWTO' and $canon eq 'Boot+Root+Raid+LILO') {
	# placeholder for a free mini-HOWTO
	move "$dir/$file", "free/$file";
	next;
    } elsif ($canon eq 'Access') {
	$canon = 'Accessibility';
    } elsif ($canon eq 'Distributions') {
	$canon = 'CD-Distributions-EN';
    } elsif ($canon eq 'GTEK-BBS') {
	$canon = 'GTEK-BBS-550';
    } elsif ($canon eq 'IR') {
	$canon = 'Infrared';
    } elsif ($canon eq 'Loadlin+Win95') {
	$canon = 'Loadlin+Win95-98-ME';
    } elsif ($canon eq 'NET3-4') {
	$canon = 'Net';
    } elsif ($canon eq 'call-back') {
	$canon = 'Call-back';
    }

    if ($free{$canon}) {
	move "$dir/$file", "free/$file";
	if ($nonfree{$canon}) {
	    warn "Duplicate copyright information for $file ($canon)!\n";
	}
    } elsif ($nonfree{$canon}) {
	move "$dir/$file", "non-free/$file";
    } else {
	warn "No copyright information for $file ($canon)!\n";
    }
}

for my $status (qw(free non-free)) {
    (my $tarout = $tar) =~ s{/([^/]*)$}{/$status-$1};
    $tarout = "../$tarout" unless $tarout =~ m[^/];
    system "cd \Q$status\E && tar -cjf \Q$tarout\E *";
}

rmtree 'free', 0, 1;
rmtree 'non-free', 0, 1;
rmtree 'tmp', 0, 1;
