#!/usr/bin/perl

$pkgdevice = $ARGV[0] || die "Unable to determine device ($!)\n";

$find = "/usr/bin/find";
$pkgproto = "/usr/bin/pkgproto";
$pkgmk = "/usr/bin/pkgmk";
$pkgtrans = "/usr/bin/pkgtrans";
$prototype = "prototype";
$pkginfo = "pkginfo";
$preinstall = "preinstall";
$egrep = "/usr/bin/grep -E";

# Sanity check

$pwd = `pwd`;
if ($pwd =~ '\/usr\/local') {
        $pwd = $`;
}            
die  "Wrong location, please cd to <PKGBASE>/usr/local/ and run again.\n"
        if ($pwd eq "");

open (PREPROTO,"$find . -print |$egrep -v \"^\.(/usr(/local)?|/opt)?\$\" | $pkgproto |") ||
	die "Unable to read prototype information ($!)\n";
open (PROTO,">$prototype") ||
	die "Unable to write file prototype ($!)\n";
print PROTO "i pkginfo=./$pkginfo\n";
print PROTO "i preinstall=./$preinstall\n";
while (<PREPROTO>) {
	# Read in the prototype information
	chomp;
	$thisline = $_;
	if ($thisline =~ " prototype "
	  or $thisline =~ " pkginfo "
	  or $thisline =~ " preinstall ") {
	    # Don't do anything as they aren't important
	} elsif ($thisline =~ "pst3") {
		# Needs to be installed SUID root
	  ($dir, $none, $file, $mode, $user, $group) = split / /,$thisline;
	  print PROTO "$dir $none $file 4755 root bin\n";
		
	} elsif ($thisline =~ "^[fd] ") {
	  # Change the ownership of files and directories
	  ($dir, $none, $file, $mode, $user, $group) = split / /,$thisline;
	  print PROTO "$dir $none $file $mode $user bin\n";
	} else {
	  # Symlinks and other stuff should be printed also
	  print PROTO "$thisline\n";
	}
}
close PROTO;
close PREPROTO;

# Now we can start building the package

$os = `uname -r`;
$os =~ '\.'; 
$os = "sol$'";
chomp $os;   

open (PKGINFO,"<$pkginfo") ||
	die "Unable to read package information ($!)\n";
while (<PKGINFO>) {
	# Read in the package information
	chomp;
	$thisline = $_;
	($var,$value) = split /=/,$thisline;
	if ("$var" eq "NAME" 
	  or "$var" eq "PKG"
	  or "$var" eq "VERSION"
	  or "$var" eq "ARCH") {
		$tmp = lc($var);
		$value =~ s/\"//g;
		if ("$tmp" eq "version"
		  and $value =~ ",REV") {
			($value,$var) = split /\,/,$value;
			$$tmp = $value;
		} else {
			$$tmp = $value;
		}
	}
}	
close PKGINFO;

$packagename = "$name-$version-$os-$arch-local";

print "Building package\n";
system ("$pkgmk -o -r `pwd` -d $pkgdevice");
system ("(cd $pkgdevice && $pkgtrans -s `pwd` ../$packagename $pkg)");
print "Done. ($packagename)\n";
