Index: /trunk/debbuild
===================================================================
--- /trunk/debbuild	(revision 6)
+++ /trunk/debbuild	(revision 7)
@@ -13,4 +13,5 @@
 use strict;
 use warnings;
+use Fcntl;	# for sysopen flags
 
 # Program flow:
@@ -33,4 +34,6 @@
 my $tarball;
 my $srcpkg;
+my $cmdbuildroot;
+my $tarballdir;	# This should really be initialized, but the coding makes it, um, ugly.
 
 # Initialized globals
@@ -41,4 +44,5 @@
 	);
 my $topdir = "/usr/src/debian";
+my $buildroot = "/var/tmp/%{name}-%{version}-%{release}.root".int(rand(99998)+1);
 
 # "Constants"
@@ -85,4 +89,5 @@
 my $buildscript;
 my $installscript;
+my $cleanscript;
 
 # Functions
@@ -92,26 +97,36 @@
 die "Not enough arguments\n" if #$argv == 0;
 
+##main
+
 load_userconfig();
 parse_cmd();
 
+if ($cmdopts{type} eq 'b') {
+  # Need to read the spec file to find the tarball.  Note that
+  # this also generates most of the shell script required.
+  parse_spec();
+}
+
 # Hokay.  Need to:
 # Execute prep if *not* --short-circuit
-prep() if ($cmdopts{short} ne 'y');
-
-if ($cmdopts{stage} =~ /ciab/) {
-  # Execute build if bc
-  # Execute build if bi, ba, bb and NOT --short-circuit
-  build() if ( ($cmdopts{stage} eq 'c') ||
-	(($cmdopts{stage} =~ /iab/) && ($cmdopts{short} ne 'y'))
-	);
-
-  # -> Execute 
-  install() if ($cmdopts{short} ne 'y');
+if ($cmdopts{stage} eq 'p' || ($cmdopts{stage} =~ /[cilabs]/ && $cmdopts{short} ne 'y')) {
+  prep();
+}
+if ($cmdopts{stage} eq 'c' || ($cmdopts{stage} =~ /[ilabs]/ && $cmdopts{short} ne 'y')) {
+  build();
+}
+if ($cmdopts{stage} =~ /[ilabs]/) {
+  install();
+}
+if ($cmdopts{stage} eq 'a') {
   binpackage();
-}
-
-srcpackage();
-
-
+  srcpackage();
+}
+if ($cmdopts{stage} eq 'b') {
+  binpackage();
+}
+if ($cmdopts{stage} eq 's') {
+  srcpackage();
+}
 
 # Just in case.
@@ -128,5 +143,5 @@
     open USERMACROS,"<$homedir/.debmacros";
     while (<USERMACROS>) {
-      # And we also only handle the %_topdir macro at the moment.
+      # And we also only handle a few macros at the moment.
       if (/^\%_topdir/) {
 	my (undef,$tmp) = split /\s+/, $_;
@@ -152,7 +167,8 @@
   foreach (@ARGV) {
     chomp;
-    $prevopt = $_;
+
     # Is it an option?
     if (/^-/) {
+
       # Is it a long option?
       if (/^--/) {
@@ -185,9 +201,9 @@
       # on which one we meet.
       if ($prevopt eq '--buildroot') {
-	# Set buildroot
+	$cmdbuildroot = $_;
       } else {
 	if ($cmdopts{type} eq 's') {
 	  # Source package
-	  if (!/\.src\.(deb|rpm)$/) {
+ 	  if (!/\.src\.(deb|rpm)$/) {
 	    die "Can't --rebuild with $_\n";
 	  }
@@ -200,5 +216,6 @@
       }
     }
-  }
+    $prevopt = $_;
+  } # foreach @ARGV
 
   # Some cross-checks.  rpmbuild limits --short-circuit to just
@@ -245,32 +262,4 @@
 
 
-## prep()
-# Unpacks the tarball from the SOURCES directory to the BUILD directory.
-# Speaks gzip and bzip2.
-# Finishes by applying patches in %prep section of spec file
-sub prep {
-  if ($cmdopts{type} eq 'b') {
-    # Need to read the spec file to find the tarball
-    parse_spec();
-
-    # Need to find the filename part of the tarball.  In theory, we
-    # could go out to the URL and retrieve it, but that's Messy.
-# want to make this local
-    $pkgdata{main}{source} =~ s|.+/([^/]+)$|$1|;
-
-    if ($pkgdata{main}{source} =~ /(.+)\.tar\.gz$/) {
-      -e "$topdir/BUILD/$1" && system "rm -rf $topdir/BUILD/$1";
-#      system "cd $topdir/BUILD;tar -zxvvf $topdir/SOURCES/$pkgdata{main}{source}";
-    } elsif ($pkgdata{main}{source} =~ /(.+)\.tar\.bz2$/) {
-      -e "$topdir/BUILD/$1" && system "rm -rf $topdir/BUILD/$1";
-      system "cd $topdir/BUILD;tar -jxvvf $topdir/SOURCES/$pkgdata{main}{source}";
-    } else {
-      die "Unknown source tarball format\n";
-    }
-    # And now for patches.  (not)
-  }
-} # end prep()
-
-
 ## parse_spec()
 # Parse the .spec file once we've.... uh....
@@ -298,8 +287,11 @@
 	    # Parse out the %setup macro.  Note that we aren't supporting
 	    # most of RPM's %setup features.
-	    $prepscript .= "cd $topdir/BUILD\n".
-		( /\s+-n\s+([^\s]+)\s+/ ?
-		  "rm -rf $1\n" : "rm -rf $pkgdata{main}{name}-$pkgdata{main}{version}\n" ).
-		"tar -".
+	    $prepscript .= "cd $topdir/BUILD\n";
+	    if ( /\s+-n\s+([^\s]+)\s+/ ) {
+	      $tarballdir = $1;
+	    } else {
+	      $tarballdir = "$pkgdata{main}{name}-$pkgdata{main}{version}";
+	    }
+	    $prepscript .= "rm -rf $tarballdir\ntar -".
 		( $pkgdata{main}{source} =~ /\.tar\.gz$/ ? "z" : "" ).
 		( $pkgdata{main}{source} =~ /\.tar\.bz2$/ ? "j" : "" ).
@@ -324,4 +316,5 @@
 	# %build.  This is pretty much just a shell script.  There
 	# *are* a few macros, but we're not going to deal with them yet.
+	$buildscript .= "cd $tarballdir\n";
 	while (<SPECFILE>) {
 	  redo LINE if /^\%/;
@@ -330,4 +323,5 @@
       }
       if (/^\%install/) {
+	$installscript .= "cd $tarballdir\n";
 	while (<SPECFILE>) {
 	  redo LINE if /^\%/;
@@ -336,4 +330,8 @@
       }
       if (/^\%clean/) {
+	while (<SPECFILE>) {
+	  redo LINE if /^\%/;
+	  $cleanscript .= $_;
+	}
       }
       if (/^\%post/) {
@@ -361,4 +359,6 @@
       } elsif (/^packager:\s+(.+)/i) {
 	$pkgdata{main}{packager} = $1;
+      } elsif (/^buildroot:\s+(.+)/i) {
+	$buildroot = $1;
       } elsif (/^source:\s+(.+)/i) {
 	$pkgdata{main}{source} = $1;
@@ -378,9 +378,48 @@
     }
   }
-  # Parse and replace macros in $pkgdata{}{}
+  # Parse and replace macros
   # Start with the ones I use
-  $pkgdata{main}{source} =~ s/\%\{name\}/$pkgdata{main}{name}/;
-  $pkgdata{main}{source} =~ s/\%\{version\}/$pkgdata{main}{version}/;
+#  $pkgdata{main}{source} =~ s/\%\{name\}/$pkgdata{main}{name}/;
+#  $pkgdata{main}{source} =~ s/\%\{version\}/$pkgdata{main}{version}/;
+
+# Scriptlet core.  Use /g to make sure all occurrences get tweaked.
+  # "real" globals (apply to all packages the same way)
+  $scriptletbase =~ s/\%\{_topdir\}/$topdir/g;
+
+  # "fake" globals (may be tweaked by package)
+  $buildroot = $cmdbuildroot if $cmdbuildroot;
+  $scriptletbase =~ s/\%\{buildroot\}/$buildroot/g;
+
+  # sub-package(compatible) stuff
+  $scriptletbase =~ s/\%\{name\}/$pkgdata{main}{name}/g;
+  $scriptletbase =~ s/\%\{version\}/$pkgdata{main}{version}/g;
+  $scriptletbase =~ s/\%\{release\}/$pkgdata{main}{release}/g;
+
 } # end parse_spec()
+
+
+## prep()
+# Unpacks the tarball from the SOURCES directory to the BUILD directory.
+# Speaks gzip and bzip2.
+# Finishes by applying patches in %prep section of spec file
+sub prep {
+  {
+    # create script filename
+    my $prepscriptfile = "/var/tmp/deb-tmp.prep.".int(rand(99998)+1);
+    sysopen(PREPSCRIPT, $prepscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
+	or die $!;
+    print PREPSCRIPT $scriptletbase;
+    print PREPSCRIPT $prepscript;
+    close PREPSCRIPT;
+
+    # execute
+    print "Calling \%prep script $prepscriptfile...\n";
+    system("/bin/sh -e $prepscriptfile") == 0
+	or die "Can't exec: $!\nalso: $?";
+
+    # and clean up
+    unlink $prepscriptfile;
+  }
+} # end prep()
 
 
@@ -389,8 +428,44 @@
 # to do as rpm does and actually create a little shell script.
 sub build {
+  # create script filename
+  my $buildscriptfile = "/var/tmp/deb-tmp.build.".int(rand(99998)+1);
+  sysopen(BUILDSCRIPT, $buildscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
+	or die $!;
+  print BUILDSCRIPT $scriptletbase;
+  print BUILDSCRIPT $buildscript;
+  close BUILDSCRIPT;
+
+  # execute
+  print "Calling \%build script $buildscriptfile...\n";
+  system("/bin/sh -e $buildscriptfile") == 0
+	or die "Can't exec: $!\nalso: $?";
+
+  # and clean up
+  unlink $buildscriptfile;
 } # end build()
 
 
-sub install {}
+## install()
+# Creates and executes %install script(let)
+sub install {
+  # create script filename
+  my $installscriptfile = "/var/tmp/deb-tmp.inst.".int(rand(99998)+1);
+  sysopen(INSTSCRIPT, $installscriptfile, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW)
+	or die $!;
+  print INSTSCRIPT $scriptletbase;
+  print INSTSCRIPT $cleanscript;	# Clean up our install target before installing into it.
+  print INSTSCRIPT $installscript;
+  close INSTSCRIPT;
+
+  # execute
+  print "Calling \%install script $installscriptfile...\n";
+  system("/bin/sh -e $installscriptfile") == 0
+	or die "Can't exec: $!\nalso: $?";
+
+  # and clean up
+  unlink $installscriptfile;
+} # end install()
+
+
 sub binpackage {}
 sub srcpackage {}
