#!/usr/bin/perl

unless($ARGV[0]) {
	print "usage: $0 package-file-name\n";
	exit 1;
}

my $tmpdir="/tmp/pkg-$$";
my $pkgfile=$ARGV[0];

# Get the package name by running pkginfo

my $pkgname = `/usr/bin/pkginfo -d $pkgfile`;
die "Can't parse name" unless $pkgname =~ /^[^\s]+\s([^\s]+)\s+/;
$pkgname = $1;

# Extract the package
mkdir($tmpdir);
system("/usr/bin/pkgtrans -i $pkgfile $tmpdir all > /dev/null 2>&1");

# Read the base dir from the pkginfo file
my $basedir = "/usr/local";
open(my $ih, "$tmpdir/$pkgname/pkginfo")
	or die "Can't read $tmpdir/$pkgname/pkginfo: $!";
while(my $line = <$ih>) {
	chomp $line;
	if($line =~ /^BASEDIR=(.*)/) {
		$basedir = $1;
	}
}
close ($ih);

# Read the file names from the package map file
open(my $fh, "$tmpdir/$pkgname/pkgmap")
	or die "Can't read $tmpdir/$pkgname/pkgmap: $!";

while(my $line = <$fh>) {
	chomp $line;

	my @parts = split /\s+/, $line;
	shift @parts;
	my $type = shift @parts;

	# Show files
	if($type eq 'f') {
		shift @parts;
		pop @parts;
		pop @parts;
		pop @parts;
		pop @parts;
		pop @parts;
		pop @parts;
		my $fn = join(' ', @parts);
		print "$basedir/$fn\n";
	}

	# Show symlinks
	if($type eq 's') {
		shift @parts;
		my $ln = join(' ', @parts);
		if($ln =~ /(.*)=(.*)/) {
			print "$basedir/$1 -> $2\n";
		}
	}
}
close($fh);

# Clean up.
system("/usr/bin/rm", "-rf", "/tmp/pkg-$$");

