Index: main/trunk/package-kits/linux/perllib/Greenstone/Package.pm
===================================================================
--- main/trunk/package-kits/linux/perllib/Greenstone/Package.pm	(revision 29583)
+++ main/trunk/package-kits/linux/perllib/Greenstone/Package.pm	(revision 29595)
@@ -8,4 +8,5 @@
 use File::Copy 'cp';
 use POSIX 'uname';
+use Storable 'dclone';
 use Greenstone::Helpers;
 use base 'Exporter';
@@ -127,5 +128,25 @@
 sub add_package {
     my $self = shift;
+    
+    # Add the (relatively) static files
     $self->add ("managers/$self->{config}->{MANAGER}", $self->{output});
+
+    if (exists $self->{config}->{SUB_PACKAGES}) {
+        my $packages = [];
+        for my $subpackage (sort @{$self->{config}->{SUB_PACKAGES}}) {
+            my $config = {};
+            for my $key (keys %{$self->{config}}) {
+                if ($key =~ /^${subpackage}_(.*)$/) {
+                    # print "setting '$1' to '$key' = '$self->{config}->{$key}'\n";
+                    $config->{$1} = $self->{config}->{$key};
+                }
+            }
+            # hashdump $self->{config};
+            push @{$packages}, $config;
+        }
+        $self->add_package_impl ($packages);
+    } else {
+        $self->add_package_impl;
+    }
 }
 
Index: main/trunk/package-kits/linux/perllib/Greenstone/Package/_apt.pm
===================================================================
--- main/trunk/package-kits/linux/perllib/Greenstone/Package/_apt.pm	(revision 29583)
+++ main/trunk/package-kits/linux/perllib/Greenstone/Package/_apt.pm	(revision 29595)
@@ -35,3 +35,51 @@
 }
 
+sub add_package_section {
+    my ($self, $out) = @_;
+    for my $line (
+        'Package: %NAME%',
+        'Architecture: %ARCHITECTURE%',
+        'Homepage: %HOMEPAGE%',
+        'Depends:',
+        '%DEPENDS',
+        'Description: %DESCRIPTION_SHORT',
+        '  %DESCRIPTION'
+    ) {
+        my $copy = $line;
+        $self->subst ($copy);
+        print $out $copy, "\n";
+    }
+}
+
+sub add_package_impl {
+    my ($self, $packages) = @_;
+    $self->add_install;
+    open my $CONTROL, '>', "/tmp/gspkg.control";
+    for my $line (
+        'Source: %NAME%',
+        'Section: java',
+        'Priority: optional',
+        'Maintainer: %MAINTAINER_NAME% <%MAINTAINER_EMAIL%>',
+        'BuildDepends:',
+        '%MAKEDEPENDS%',
+    ) {
+        my $copy = $line;
+        $self->subst ($copy);
+        print $CONTROL $copy, "\n";
+    }
+    if (defined $packages) {
+        my $config = $self->{config};
+        for my $package (@{$packages}) {
+            $package->{HOMEPAGE} = $config->{HOMEPAGE};
+            $self->{config} = $package;
+            $self->add_package_section ($CONTROL);
+        }
+        $self->{config} = $config;
+    } else {
+        $self->add_package_section ($CONTROL);
+    }
+    close $CONTROL;
+    $self->add ("/tmp/gspkg.control", "$self->{output}/debian/control");
+}
+
 1;
Index: main/trunk/package-kits/linux/perllib/Greenstone/Package/_pacman.pm
===================================================================
--- main/trunk/package-kits/linux/perllib/Greenstone/Package/_pacman.pm	(revision 29583)
+++ main/trunk/package-kits/linux/perllib/Greenstone/Package/_pacman.pm	(revision 29595)
@@ -39,7 +39,8 @@
 
 sub write_function {
-    my ($name, $out, @lines) = @_;
+    my ($self, $name, $out, @lines) = @_;
     print $out $name, "() {\n";
     for my $line (@lines) {
+        $self->subst ($line);
         print $out "\t", $line, "\n";
     }
@@ -54,24 +55,88 @@
         exists $self->{config}->{POST_REMOVE}) {
         my $name = "$self->{config}->{NAME}.install";
-        open my $INSTALL, '>', "/tmp/$name";
+        open my $INSTALL, '>', "/tmp/gspkg.$name";
         exists $self->{config}->{PRE_INSTALL} and do {
-            write_function "pre_install", $INSTALL, @{$self->{config}->{PRE_INSTALL}};
-            write_function "pre_upgrade", $INSTALL, @{$self->{config}->{PRE_INSTALL}};
+            $self->write_function ("pre_install", $INSTALL, @{$self->{config}->{PRE_INSTALL}});
+            $self->write_function ("pre_upgrade", $INSTALL, @{$self->{config}->{PRE_INSTALL}});
         };
         exists $self->{config}->{POST_INSTALL} and do {
-            write_function "post_install", $INSTALL, @{$self->{config}->{POST_INSTALL}};
-            write_function "post_upgrade", $INSTALL, @{$self->{config}->{POST_INSTALL}};
+            $self->write_function ("post_install", $INSTALL, @{$self->{config}->{POST_INSTALL}});
+            $self->write_function ("post_upgrade", $INSTALL, @{$self->{config}->{POST_INSTALL}});
         };
         exists $self->{config}->{PRE_REMOVE} and
-            write_function "pre_remove", $INSTALL, @{$self->{config}->{PRE_REMOVE}};
+            $self->write_function ("pre_remove", $INSTALL, @{$self->{config}->{PRE_REMOVE}});
         exists $self->{config}->{POST_REMOVE} and
-            write_function "post_remove", $INSTALL, @{$self->{config}->{POST_REMOVE}};
+            $self->write_function ("post_remove", $INSTALL, @{$self->{config}->{POST_REMOVE}});
         close $INSTALL;
-        $self->add ("/tmp/$name", "$self->{output}/$name");
-        $self->{config}->{INSTALL} = "install=$name";
+        $self->add ("/tmp/gspkg.$name", "$self->{output}/$name");
+        return "install=$name";
     } else {
-        $self->{config}->{INSTALL} = "";
+        return "";
     }
 }
 
+sub add_package_impl {
+    my ($self, $packages) = @_;
+    if (defined $packages) {
+        $self->{config}->{NAME} = '';
+        for my $package (@{$packages}) {
+            $self->{config}->{NAME} .= " $package->{NAME}";
+        }
+    }
+    open my $PKGBUILD, '>', "/tmp/gspkg.PKGBUILD";
+    for my $line (
+        '# Maintainer: %MAINTAINER_NAME% <%MAINTAINER_EMAIL%>',
+        'pkgname=(%NAME%)',
+        'pkgver=%VERSION%',
+        'pkgrel=%RELEASE%',
+        'url=%HOMEPAGE%',
+        'license=%LICENSE_SHORT%',
+        'arch=(any)',
+        'makedepends=(',
+        '%MAKEDEPENDS%',
+        ')',
+        'source=(',
+        '%SOURCES%',
+        ')',
+        'md5sums=(',
+        '%SOURCE_SUMS%',
+        ')',
+        'build() {',
+        '  cd ${srcdir}',
+        '  make build',
+        '}'
+    ) {
+        my $copy = $line;
+        $self->subst ($copy);
+        print $PKGBUILD $copy, "\n";
+    }
+    if (defined $packages) {
+        my $config = $self->{config};
+        for my $package (@{$packages}) {
+            $self->{config} = $package;
+            $self->write_function ("package_$package->{NAME}", $PKGBUILD,
+                'pkgdesc=%DESCRIPTION%',
+                'arch=(\'%ARCHITECTURE%\')',
+                'depends=(',
+                '%DEPENDS%',
+                ')',
+                'optdepends=(',
+                '%OPTDEPENDS%',
+                ')',
+                'provides=(',
+                '%PROVIDES%',
+                ')',
+                $self->add_install,
+                'DESTDIR=${pkgdir} make install_%NAME%',
+            );
+        }
+        $self->{config} = $config;
+    } else {
+        print $PKGBUILD $self->add_install, "\n";
+        $self->write_function ('package', $PKGBUILD, 'DESTDIR=${pkgdir} make install');
+    }
+    close $PKGBUILD;
+    $self->add ("/tmp/gspkg.PKGBUILD", "$self->{output}/PKGBUILD");
+}
+
 1;
Index: main/trunk/package-kits/linux/perllib/Greenstone/Package/_rpm.pm
===================================================================
--- main/trunk/package-kits/linux/perllib/Greenstone/Package/_rpm.pm	(revision 29583)
+++ main/trunk/package-kits/linux/perllib/Greenstone/Package/_rpm.pm	(revision 29595)
@@ -33,13 +33,84 @@
 sub add_install {
     my $self = shift;
-    $self->{config}->{INSTALL} = "";
+    my $ret = '';
     exists $self->{config}->{PRE_INSTALL} and
-        $self->{config}->{INSTALL} .= write_function "pre", @{$self->{config}->{PRE_INSTALL}};
+        $ret .= write_function "pre", @{$self->{config}->{PRE_INSTALL}};
     exists $self->{config}->{POST_INSTALL} and
-        $self->{config}->{INSTALL} .= write_function "post", @{$self->{config}->{POST_INSTALL}};
+        $ret .= write_function "post", @{$self->{config}->{POST_INSTALL}};
     exists $self->{config}->{PRE_REMOVE} and
-        $self->{config}->{INSTALL} .= write_function "preun", @{$self->{config}->{PRE_INSTALL}};
+        $ret .= write_function "preun", @{$self->{config}->{PRE_INSTALL}};
     exists $self->{config}->{POST_REMOVE} and
-        $self->{config}->{INSTALL} .= write_function "postun", @{$self->{config}->{POST_INSTALL}};
+        $ret .= write_function "postun", @{$self->{config}->{POST_INSTALL}};
+    return $ret;
+}
+
+sub add_package_impl {
+    my ($self, $packages) = @_;
+    open my $SPEC, '>', '/tmp/gspkg.spec';
+    for my $line (
+        'Name:          %NAME%',
+        'Version:       %VERSION%',
+        'Release:       %RELEASE%',
+        'License:       %LICENSE_SHORT%',
+        'URL:           %HOMEPAGE%',
+        'Source0:       %NAME%',
+        'AutoReqProv:   no',
+        'BuildRequires: %MAKEDEPENDS%',
+        '',
+        '%prep',
+        'cp -pr %SOURCE0 .',
+        '',
+        '%build',
+        'cd %NAME%',
+        'make %{?_smp_mflags}',
+        '',
+        '%install',
+        'cd %NAME%',
+        '%make_install',
+        '',
+        '%changelog',
+        ''
+    ) {
+        my $copy = $line;
+        $self->subst ($copy);
+        print $SPEC $copy, "\n";
+    }
+    if (defined $packages) {
+        my $config = $self->{config};
+        for my $package (@{$packages}) {
+            $self->{config} = $package;
+            for my $line (
+                '%package %NAME%',
+                '%requires',
+                '%DEPENDS%',
+                '%description',
+                '%DESCRIPTION%',
+                $self->add_install,
+                '%files',
+                '%FILES%'
+            ) {
+                my $copy = $line;
+                $self->subst ($copy);
+                print $SPEC $copy, "\n";
+            }
+        }
+        $self->{config} = $config;
+    } else {
+        for my $line (
+            '%requires',
+            '%DEPENDS%',
+            '%description',
+            '%DESCRIPTION%',
+            $self->add_install,
+            '%files',
+            '%FILES%'
+        ) {
+            my $copy = $line;
+            $self->subst ($copy);
+            print $SPEC $copy, "\n";
+        }
+    }
+    close $SPEC;
+    $self->add ("/tmp/gspkg.spec", "$self->{output}/$self->{config}->{NAME}.spec");
 }
 
