Index: /trunk/debbuild
===================================================================
--- /trunk/debbuild	(revision 37)
+++ /trunk/debbuild	(revision 38)
@@ -94,5 +94,6 @@
 # This is the form of $pkgdata{pkgname}{meta}
 # meta includes Summary, Name, Version, Release, Group, Copyright,
-#	Source, URL, Packager, BuildRoot, Description
+#	Source, URL, Packager, BuildRoot, Description, BuildReq(uires),
+#	Requires, Provides
 # 10/31/2005 Maybe this should be flatter?  -kgd
 my %pkgdata;
@@ -100,4 +101,5 @@
 # Files listing.  Embedding this in %pkgdata would be, um, messy.
 my %filelist;
+my $buildreq;
 
 # Scriptlets
@@ -133,4 +135,6 @@
   # this also generates most of the shell script required.
   parse_spec();
+  die "Can't build $pkgdata{main}{name}:  build requirements not met.\n"
+    if !checkbuildreq();
 }
 
@@ -341,5 +345,5 @@
 	while (<SPECFILE>) {
 	  redo LINE if /^\%/;
-	  if (my ($dname,$dvalue) = (/^(Summary|Group|Version):\s+(.+)$/i)) {
+	  if (my ($dname,$dvalue) = (/^(Summary|Group|Version|Requires|Provides):\s+(.+)$/i)) {
 	    $dname =~ tr/[A-Z]/[a-z]/;
 	    $pkgdata{$subname}{$dname} = $dvalue;
@@ -547,4 +551,10 @@
 	  $pkgdata{main}{$patchname} = $patchbits[$#patchbits];
 	}
+      } elsif (/^buildreq(?:uires)?:\s+(.+)/i) {
+	$buildreq .= ", $1";
+      } elsif (/^requires:\s+(.+)/i) {
+	$pkgdata{main}{requires} .= ", $1";
+      } elsif (/^provides:\s+(.+)/i) {
+	$pkgdata{main}{provides} .= ", $1";
       }
 #Name: suwrap
@@ -678,4 +688,12 @@
     }
 
+    # Get the "Depends" (Requires) a la RPM.  Ish.  We strip the leading
+    # comma and space here (if needed) in case there were "Requires" specified
+    # in the spec file - those would precede these.
+    ($pkgdata{$pkg}{depends} .= getreqs("$buildroot/$pkg")) =~ s/^, //;
+
+    # Do this here since we're doing {depends}...
+    $pkgdata{$pkg}{provides} =~ s/^, //;
+
     # Gotta do this next, otherwise the control file has nowhere to go.  >:(
     mkdir "$buildroot/$pkg/DEBIAN";
@@ -701,4 +719,6 @@
 	"Architecture: i386\n".
 	"Maintainer: $pkgdata{main}{packager}\n".
+	"Depends: $pkgdata{$pkg}{depends}\n".
+	"Provides: $pkgdata{$pkg}{provides}\n".
 	"Description: $pkgdata{$pkg}{summary}\n";
     $control .= "$pkgdata{$pkg}{desc}\n";
@@ -827,4 +847,104 @@
   print "Wrote source package $pkgsrcname in $topdir/SDEBS.\n";
 }
+
+
+## checkbuildreq()
+# Checks the build requirements (if any)
+# Spits out a rude warning and returns a true-false error if any
+# requirements are not met.
+sub checkbuildreq {
+  return 0 if $buildreq eq '';	# No use doing extra work.
+
+  my $reqflag = 1;  # unset iff a buildreq is missing
+
+  $buildreq =~ s/^, //;	# Strip the leading comma and space
+  my @reqlist = split /,\s+/, $buildreq;
+
+  foreach my $req (@reqlist) {
+    my ($pkg,$rel,$ver);
+
+    # We have two classes of requirements - versioned and unversioned.
+    if ($req =~ /[><=]/) {
+      # Pick up the details of versioned buildreqs
+      ($pkg,$rel,$ver) = ($req =~ /([a-z0-9._-]+)\s+([><=]+)\s+([a-z0-9._-]+)/);
+    } else {
+      # And the unversioned ones.
+      $pkg = $req;
+      $rel = '>=';
+      $ver = 0;
+    }
+
+    my @pkglist = qx { dpkg-query --showformat '\${status}\t\${version}\n' -W $pkg };
+# need to check if no lines returned - means a bad buildreq
+    my ($reqstat,undef,undef,$reqver) = split /\s+/, $pkglist[0];
+    if ($reqstat !~ /install/) {
+      print " * Missing build-dependency $pkg!\n";
+      $reqflag = 0;
+    } else {
+# gotta be a better way to do this... :/
+      if ($rel eq '>=' && !($reqver ge $ver)) {
+	print " * Buildreq $pkg is installed, but wrong version ($reqver):  Need $ver\n";
+	$reqflag = 0;
+      }
+      if ($rel eq '>' && !($reqver gt $ver)) {
+	print " * Buildreq $pkg is installed, but wrong version ($reqver):  Need $ver\n";
+	$reqflag = 0;
+      }
+      if ($rel eq '<=' && !($reqver le $ver)) {
+	print " * Buildreq $pkg is installed, but wrong version ($reqver):  Need $ver\n";
+	$reqflag = 0;
+      }
+      if ($rel eq '<' && !($reqver lt $ver)) {
+	print " * Buildreq $pkg is installed, but wrong version ($reqver):  Need $ver\n";
+	$reqflag = 0;
+      }
+      if ($rel eq '=' && !($reqver eq $ver)) {
+	print " * Buildreq $pkg is installed, but wrong version ($reqver):  Need $ver\n";
+	$reqflag = 0;
+      }
+    } # end not installed/installed check
+  } # end req loop
+
+  return $reqflag;
+} # end checkbuildreq()
+
+
+## getreqs()
+# Find out which libraries/packages are required for any
+# executables and libs in a given file tree.
+# (Debian doesn't have soname-level deps;  just package-level)
+sub getreqs() {
+  my $pkgtree = $_[0];
+
+  print "checking reqs on executables in $pkgtree...\n";
+  my @reqlist = qx { find $pkgtree -type f -perm 755 | grep -v $pkgtree/etc | xargs ldd };
+
+  my %reqs;
+  my $reqlibs;
+
+  foreach (@reqlist) {
+    next if /^$pkgtree/;
+    next if m|/lib/ld-linux.so|;
+    my ($req) = (/^\s+([a-z0-9._-]+)/);
+
+    $reqlibs .= " $req";
+  }
+
+  foreach (qx { dpkg -S $reqlibs }) {
+    my ($libpkg,undef) = split /:\s+/;
+    $reqs{$libpkg} = 1;
+  }
+
+  my $deplist;
+  foreach (keys %reqs) {
+    $deplist .= ", $_";
+  }
+
+# For now, we're done.  We're not going to meddle with versions yet.
+# Among other things, it's messier than handling "simple" yes/no "do
+# we have this lib?" deps.  >:(
+
+  return $deplist;
+} # end getreqs()
 
 
@@ -960,4 +1080,8 @@
 
 
+__END__
+
+
+
 =head1 NAME
 
