Index: /trunk/debbuild
===================================================================
--- /trunk/debbuild	(revision 13)
+++ /trunk/debbuild	(revision 14)
@@ -76,7 +76,4 @@
   export RPM_BUILD_ROOT
 );
-#my $tmp = `dpkg-architecture`;	# Debian buildarch goop.
-#$tmp =~ s/^(.*)$/  $1/g;
-#print $tmp;
 foreach (`dpkg-architecture`) {
   s/=(.+)/="$1"/;
@@ -103,8 +100,4 @@
 my $cleanscript;
 
-# Functions
-sub prep;
-sub parse_spec;
-
 die "Not enough arguments\n" if #$argv == 0;
 
@@ -129,21 +122,26 @@
 
 # Hokay.  Need to:
-# Execute prep if *not* --short-circuit
+# -> prep if -.p OR (-.[cilabs] AND !--short-circuit)
 if ($cmdopts{stage} eq 'p' || ($cmdopts{stage} =~ /[cilabs]/ && $cmdopts{short} ne 'y')) {
   prep();
 }
+# -> build if -.c OR (-.[ilabs] AND !--short-circuit)
 if ($cmdopts{stage} eq 'c' || ($cmdopts{stage} =~ /[ilabs]/ && $cmdopts{short} ne 'y')) {
   build();
 }
+# -> install if -.[ilabs]
 if ($cmdopts{stage} =~ /[ilabs]/) {
   install();
 }
+# -> binpkg and srcpkg if -.a
 if ($cmdopts{stage} eq 'a') {
   binpackage();
   srcpackage();
 }
+# -> binpkg if -.b
 if ($cmdopts{stage} eq 'b') {
   binpackage();
 }
+# -> srcpkg if -.s
 if ($cmdopts{stage} eq 's') {
   srcpackage();
@@ -201,4 +199,5 @@
 	}
       } else {
+	# Not a long option
 	if (/^-[bt]/) {
 	  if ($cmdopts{stage} eq 's') {
@@ -206,4 +205,5 @@
 	    die "Can't use $_ with --rebuild\n";
 	  } else {
+	    # Capture the type (from "bare" files or tarball) and the stage (prep, build, etc)
 	    ($cmdopts{stage}) = (/^-[bt]([pcilabs])/);
 	    ($cmdopts{type}) = (/^-([bt])[pcilabs]/);
@@ -215,5 +215,7 @@
 	}
       }
-    } else {
+
+    } else { # Not an option argument
+
       # --buildroot is the only option that takes an argument
       # Therefore, any *other* bare arguments are the spec file,
@@ -241,4 +243,5 @@
   # Some cross-checks.  rpmbuild limits --short-circuit to just
   # the "compile" and "install" targets - with good reason IMO.
+  # Note that --short-circuit with -.p is not really an error, just redundant.
   # NB - this is NOT fatal, just ignored!
   if ($cmdopts{short} eq 'y' && $cmdopts{stage} =~ /[labs]/) {
@@ -246,4 +249,5 @@
     $cmdopts{short} = 'n';
   }
+
   # Valid options, with example arguments (if any):
 # Build from .spec file; mutually exclusive:
@@ -283,13 +287,15 @@
 
 ## parse_spec()
-# Parse the .spec file once we've.... uh....
+# Parse the .spec file.
 sub parse_spec {
   open SPECFILE,"<$specfile";
 
 LINE: while (<SPECFILE>) {
-    next if /^#/;
-    next if /^\s+$/;
+    next if /^#/;	# Ignore comments...
+    next if /^\s+$/;	# ... and blank lines.
+
     if (/^\%/) {
       # A macro that needs further processing.
+
       if (/^\%description/) {
 	$pkgdata{main}{desc} .= $_;
@@ -308,11 +314,10 @@
 
 	# Replace some core macros
-	$pkgdata{main}{source} =~ s/\%\{name\}/$pkgdata{main}{name}/;
-	$pkgdata{main}{source} =~ s/\%\{version\}/$pkgdata{main}{version}/;
+	$pkgdata{main}{source} = expandmacros($pkgdata{main}{source},'gp');
 
 PREPSCRIPT: while (<SPECFILE>) {
 	  if (/^\%setup/) {
 	    # Parse out the %setup macro.  Note that we aren't supporting
-	    # most of RPM's %setup features.
+	    # many of RPM's %setup features.
 	    $prepscript .= "cd $topdir/BUILD\n";
 	    if ( /\s+-n\s+([^\s]+)\s+/ ) {
@@ -362,7 +367,4 @@
 	  if (/^\%makeinstall/) {
             $installscript .= expandmacros($_,'igbp');
-print $installscript;
-#print "\n$tmp\n"; die;
-#            $installscript .= $tmp;
           } else {
             last INSTALLSCRIPT if /^\%/;
@@ -386,5 +388,7 @@
 	# Danger! Danger!
       }
-    } else {
+
+    } else {	# Data from the spec file "header"
+
       if (/^summary:\s+(.+)/i) {
 	$pkgdata{main}{summary} = $1;
@@ -424,8 +428,6 @@
     }
   }
-  # 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}/;
+
+  # Parse and replace some more macros.  More will be replaced even later.
 
   # Expand macros as necessary.
@@ -439,7 +441,5 @@
 
 ## 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
+# Writes and executes the %prep script (mostly) built while reading the spec file.
 sub prep {
   # Replace some things here just to make sure.
@@ -457,5 +457,5 @@
   print "Calling \%prep script $prepscriptfile...\n";
   system("/bin/sh -e $prepscriptfile") == 0
-	or die "Can't exec: $!\nalso: $?";
+	or die "Can't exec: $!\n";
 
   # and clean up
@@ -465,6 +465,5 @@
 
 ## build()
-# Execute commands provided as a shell script.  It may prove simpler
-# to do as rpm does and actually create a little shell script.
+# Writes and executes the %build script (mostly) built while reading the spec file.
 sub build {
   # Expand the macros
@@ -479,10 +478,8 @@
   close BUILDSCRIPT;
 
-#print "'$scriptletbase$buildscript'";
-#exit 0;
   # execute
   print "Calling \%build script $buildscriptfile...\n";
   system("/bin/sh -e $buildscriptfile") == 0
-	or die "Can't exec: $!\nalso: $?";
+	or die "Can't exec: $!\n";
 
   # and clean up
@@ -492,5 +489,5 @@
 
 ## install()
-# Creates and executes %install script(let)
+# Writes and executes the %install script (mostly) built while reading the spec file.
 sub install {
   # Expand the macros
@@ -509,5 +506,5 @@
   print "Calling \%install script $installscriptfile...\n";
   system("/bin/sh -e $installscriptfile") == 0
-	or die "Can't exec: $!\nalso: $?";
+	or die "Can't exec: $!\n";
 
   # and clean up
@@ -518,10 +515,13 @@
 ## binpackage()
 # Creates the binary .deb package from the installed tree in $buildroot.
+# Writes and executes a shell script to do so.
+# Creates miscellaneous files required by dpkg-deb to actually build the package file.
 sub binpackage {
-  # Some checks on the .deb file location
+  # Make sure we have somewhere to write the .deb file
   if (!-e "$topdir/DEBS/i386") {
 #	"if [ -e $topdir/DEBS/i386 ]; then\n\tmkdir $topdir/DEBS/i386\nfi\n".
     mkdir "$topdir/DEBS/i386";
   }
+
   # Gotta do this first, otherwise the control file has nowhere to go.  >:(
   mkdir "$buildroot/DEBIAN";
@@ -534,5 +534,5 @@
   print DEBSCRIPT "tree $buildroot\n".
 	"fakeroot dpkg-deb -b $buildroot $topdir/DEBS/i386/$pkgdata{main}{name}_$pkgdata{main}{version}-$pkgdata{main}{release}_i386.deb\n";
-
+    # %$&$%@#@@#%@@@ Debian and their horrible ugly package names.  >:(
   close DEBSCRIPT;
 
@@ -545,12 +545,7 @@
 	"Description: $pkgdata{main}{desc}\n";
 
-eval {
   open CONTROL, ">$buildroot/DEBIAN/control";
   print CONTROL $control;
   close CONTROL;
-};
-if ($@) {
-  print $@;
-}
 
 #Package: httpd
@@ -567,13 +562,11 @@
 # http://home.samfundet.no/~sesse/mpm-itk/.
 
-
   # execute
   print "Creating .deb for $pkgdata{main}{name}...\n";
   system("/bin/sh -e $debscriptfile") == 0
-	or die "Can't exec: $!\nalso: $?";
+	or die "Can't exec: $!\n";
 
   # and clean up
   unlink $debscriptfile;
-
 } # end binpackage()
 
@@ -584,7 +577,9 @@
 ## expandmacros()
 # Expands all %{blah} macros in the passed string
+# Split up a bit with some sections so we don't spend time trying to
+# expand macros that are only used in a few specific places.
 sub expandmacros {
   my $macrostring = shift;
-  my $section = shift;	# We'll want to split things up a bit eventually.
+  my $section = shift;
 
   # To allow the FHS-ish %configure, %make, and %makeinstall to work The Right Way.
@@ -637,19 +632,21 @@
     # over the "default" ones.
 
-    $macrostring =~ s|%{_mandir}|%{_datadir}/man|g;	#/usr/share/man
-    $macrostring =~ s|%{_infodir}|%{_datadir}/info|g;	#/usr/share/info
-    $macrostring =~ s|%{_oldincludedir}|$prefix/usr/include|g;	#/usr/include
+    # Now we cascade the macros introduced above.  >_<
+									# Wot ot to go theah:
+    $macrostring =~ s|%{_mandir}|%{_datadir}/man|g;			#/usr/share/man
+    $macrostring =~ s|%{_infodir}|%{_datadir}/info|g;			#/usr/share/info
+    $macrostring =~ s|%{_oldincludedir}|$prefix/usr/include|g;		#/usr/include
     $macrostring =~ s|%{_includedir}|$prefix%{_prefix\}/include|g;	#/usr/include
     $macrostring =~ s|%{_libdir}|$prefix%{_exec_prefix}/%{_lib}|g;	#/usr/lib
-    $macrostring =~ s|%{_lib}|lib|g;						#?
+    $macrostring =~ s|%{_lib}|lib|g;					#?
     $macrostring =~ s|%{_localstatedir}|$prefix/var|g;			#/var
     $macrostring =~ s|%{_sharedstatedir}|$prefix%{_prefix}/com|g;	#/usr/com WTF?
-    $macrostring =~ s|%{_sysconfdir}|$prefix/etc|g;				#/etc
+    $macrostring =~ s|%{_sysconfdir}|$prefix/etc|g;			#/etc
     $macrostring =~ s|%{_datadir}|$prefix%{_prefix}/share|g;		#/usr/share
     $macrostring =~ s|%{_libexecdir}|$prefix%{_exec_prefix}/libexec|g;	#/usr/libexec
-    $macrostring =~ s|%{_sbindir}|$prefix%{_exec_prefix}/sbin|g;		#/usr/sbin
+    $macrostring =~ s|%{_sbindir}|$prefix%{_exec_prefix}/sbin|g;	#/usr/sbin
     $macrostring =~ s|%{_bindir}|$prefix%{_exec_prefix}/bin|g;		#/usr/bin
     $macrostring =~ s|%{_exec_prefix}|$prefix%{_prefix}|g;		#/usr
-    $macrostring =~ s|%{_prefix}|/usr|g;			#/usr
+    $macrostring =~ s|%{_prefix}|/usr|g;				#/usr
   } # done with config section
 
