#!/usr/bin/perl -w
#
# Automated processes to create SUN packages
# You can run this script after you did a 'make install' in the chrooted
# environment. Run it from the <whatever>/packagename-1.0/usr/local/ directory
#
# JA: 06-01-2000        Initial release
# JA: 25-01-2000        Beautified a little
# AH: 20-07-2000        Beautified a bit more


$bits = $ARGV[0];
$ver = $ARGV[1];
$name = $ARGV[2];
$pkg = "MHSL$name$bits";
print "\nStarting make_package (sun solaris)\nbits= $bits\nver= $ver\nname= $name, pkg=$pkg\n";

$find = "/usr/bin/find";
$pkgproto = "/usr/bin/pkgproto";
$pkgmk = "/usr/bin/pkgmk";
$pkgtrans = "/usr/bin/pkgtrans";
$temp = "/tmp/prototype$$";
$prototype = "prototype";
$pkginfo = "pkginfo";
$postinstall = "postinstall";
($gid ,$uid ,$userInfo ,$email ,$quota ,$group ,$passwd
,$category ,$userHome ,$vendor ,$loginShell ,$pstamp ,$basedir)=();

# Sanitycheck

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

system  ("$find . -print | $pkgproto > $temp");
open (PREPROTO,"<$temp") || 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 postinstall=./$postinstall\n";
while (<PREPROTO>) {
        # Read the prototype information from /tmp/prototype$$
        chomp;
        $thisline = $_;
        if ($thisline =~ " prototype "
         or $thisline =~ " pkginfo "
         or $thisline =~ " postinstall ") {
          # We don't need that line
        } elsif ($thisline =~ "^[fd] ") {
          # Change the ownership for files and directories
          ($dir, $none, $file, $mode, $user, $group) = split / /,$thisline;
          print PROTO "$dir $none $file $mode root bin\n";
        } else {
          # Symlinks and other stuff should be printed as well ofcourse
          print PROTO "$thisline\n";
        }
}
close PROTO;
close PREPROTO;

# Clean up
unlink $temp || warn "Unable to remove tempfile ($!)\n";

# Now we can start building the package
#
# First get some info

$default{"name"} = $name;
$default{"version"} = $ver;
$default{"pkg"} = "MHSL$name$bits";
$default{"arch"} = "sparc$bits";
$default{"category"} = "application";
$default{"vendor"} = "March Hare Software Ltd";
$default{"email"} = "tony-hoyle\@march-hare.com";
$login = getlogin();
($user, $passwd, $uid, $gid, $quota, $default{"pstamp"}, $userInfo,
 $userHome, $loginShell) = getpwnam ($login);
$default{"pstamp"} = "Jasper Aukes"
    	if ($default{"pstamp"} eq "");
$os = `uname -r`;
$os =~ '\.';
$os = "sol$'";
chomp $os;
$default{"basedir"} = "/";

# Check for correctness of guessed values by userinput

%questions = (
  pkg => "Please give the name for this package",
  name => "Now enter the real name for this package",
  arch => "What architecture did you build the package on?",
  version => "Enter the version number of the package",
  category => "What category does this package belong to?",
  vendor => "Who is the vendor of this package?",
  email => "Enter the email adress for contact",
  pstamp => "Enter your own name",
  basedir => "What is the basedir this package will install into?",
  packagename => "How should i call the packagefile?",
);

@vars = qw(pkg name arch version category vendor email pstamp basedir
    	   packagename);
foreach $varname (@vars) {
        $default{"$varname"} = "$name-$version-$os-sparc$bits"
	    	if ($varname eq "packagename");
        getvar($varname);
}
$classes = "none";

# Create the pkginfo file

print "\nNow creating $pkginfo file\n";
open (PKGINFO,">$pkginfo") || die "Unable to open $pkginfo for writing ($!)\n";
print PKGINFO "PKG=\"$pkg\"\n";
print PKGINFO "NAME=\"$name\"\n";
print PKGINFO "ARCH=\"$arch\"\n";
print PKGINFO "VERSION=\"$version\"\n";
print PKGINFO "CATEGORY=\"$category\"\n";
print PKGINFO "VENDOR=\"$vendor\"\n";
print PKGINFO "EMAIL=\"$email\"\n";
print PKGINFO "PSTAMP=\"$pstamp\"\n";
print PKGINFO "BASEDIR=\"$basedir\"\n";
print PKGINFO "CLASSES=\"$classes\"\n";
close PKGINFO;
print "Done.\n";

# Build and zip the package

print "Building package\n";
system ("$pkgmk -o -r `pwd`");
print "Translate package format\n";
print "   (cd /var/spool/pkg;$pkgtrans -s `pwd` /tmp/$packagename $pkg)\n";
system ("(cd /var/spool/pkg;$pkgtrans -s `pwd` /tmp/$packagename $pkg)");
print "Zip package\n";
system ("gzip /tmp/$packagename");
print "Move package\n";
system ("mv /tmp/$packagename.gz ..");
print "Done. (../$packagename.gz)\n";

# The subroutines

sub getvar {
        my $questionname = "@_";
    #    print "$questions{$questionname} [$default{\"$questionname\"}]: ";
        my $answer = ""; #<STDIN>;
        chomp $answer;
        $$questionname = $answer;
        $$questionname = $default{$questionname} if ($$questionname eq "");
}


syntax highlighted by Code2HTML, v. 0.9.1