#!/usr/bin/perl
#
#	extract_f3pod - extract Fury3 POD files
#	written by Jan Engelhardt, 2004-2007
#
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the WTF Public License version 2 or
#	(at your option) any later version.
#

use Getopt::Long;
use strict;
do (($0 =~ m{^(.*)/})[0] || ".")."/shared.pm" ||
	die "Could not load shared.pm: $!\n";

my $ex_out_dir      = ".";
my $ex_archive_file = "-";
my $ex_auto_lower   = 0;
my $ex_verbose      = 0;

Getopt::Long::Configure(qw(bundling));
GetOptions(
	"C=s" => \$ex_out_dir,
	"L"   => \$ex_auto_lower,
	"f=s" => \$ex_archive_file,
	"v"   => \$ex_verbose,
);

&main();

sub main ()
{
	local(*IN, *OUT);
	my($buf, $dir, $num_files);

	open(IN, "< $ex_archive_file") ||
		die "Could not open $ex_archive_file: $!\n";
	binmode IN;

	$num_files = unpack("V", &getcf(\*IN, 4));
	read(IN, $buf, 5 * 16);
	read(IN, $dir, $num_files * 40);
	if ($ex_verbose) {
		print "$num_files files in archive\n";
	}

	for (my $i = 0; $i < $num_files; ++$i) {
		my $dentry = substr($dir, 40 * $i, 40);
		my($nam, $len, $ofs) = unpack("Z32VV", $dentry);

		$nam =~ s{\\}{/}giso;
		if ($ex_auto_lower && $nam !~ /[a-z]/) {
			$nam =~ tr/A-Z/a-z/;
		}
		if ($ex_verbose) {
			print "\@$ofs: $nam ($len bytes)\n";
		}

		my $abs_file = "$ex_out_dir/$nam";
		&mkdir_p_stripbase($abs_file);
		open(OUT, "> $abs_file") ||
			warn "Could not write to $abs_file: $!\n";
		binmode OUT;
		seek(IN, $ofs, 0);
		&transfer(\*OUT, \*IN, $len);
		close OUT;
	}
}
