$NetBSD: patch-aa,v 1.1.1.1 2000/12/09 00:44:05 wiz Exp $

--- bsdpak.pl.orig	Thu Nov  9 08:32:49 2000
+++ bsdpak.pl	Fri Nov 10 10:36:04 2000
@@ -16,7 +16,23 @@
 my @indexPackagesOutput;
 
 my $currentPackagesCommand = '/usr/sbin/pkg_info -aI';
-my $indexPackagesCommand = 'cat /usr/ports/INDEX';
+
+my $indexPackagesDir;
+if (-d "/usr/ports") {
+  $indexPackagesDir = '/usr/ports';
+} elsif (-d "/usr/pkgsrc") {
+  $indexPackagesDir = '/usr/pkgsrc';
+} else {
+  print "Could not find package directory.\n";
+  exit(1);
+}
+
+if (! -f "$indexPackagesDir/INDEX") {
+  print "Could not find $indexPackagesDir/INDEX\nI suggest:\n  cd $indexPackagesDir;make index\n\n";
+  exit(1);
+}
+
+my $indexPackagesCommand = "cat $indexPackagesDir/INDEX";
 
 
 main();
@@ -89,10 +105,10 @@
 --index=
   Specify a path or url to use for the index file.
 
-By default this program uses /usr/ports/INDEX for the index
-packages, and pkg_info output for current packages. Current
-info is not yet customizeable (you shouldn't have to change it)
-but the index path is, with --index= .. it can be a path or url.
+By default this program uses /usr/ports/INDEX or /usr/pkgsrc/INDEX
+for the index packages, and pkg_info output for current packages.
+Current info is not yet customizeable (you shouldn't have to change
+it) but the index path is, with --index= .. it can be a path or url.
 
 EOF
 }
@@ -101,7 +117,14 @@
   if($flags{'index'}) {
     my $indexLine = $flags{'index'};
     if($indexLine =~ m/ftp:\/\// || $indexLine =~ m/http:\/\//) {
-      $indexPackagesCommand = "fetch -o - $flags{'index'} 2>&1";
+      my $fetchCommand;
+      `ftp about: >/dev/null 2>&1`;
+      if ($? == 0) {
+        $fetchCommand = 'ftp';
+      } else {
+        $fetchCommand = 'fetch';
+      }
+      $indexPackagesCommand = "$fetchCommand -o - $flags{'index'} 2>/dev/null";
     }
     else {
       if(-e $flags{'index'}) { $indexPackagesCommand = "cat $flags{'index'}"; }
@@ -115,7 +138,7 @@
   @currentPackagesOutput = `$currentPackagesCommand`;
   @indexPackagesOutput = `$indexPackagesCommand`;
 
-  if($indexPackagesOutput[0] =~ m/fetch: -:/) {
+  if($? != 0) {
     print "The file $flags{'index'} does not exist!\n";
     exit;
   }  
@@ -186,7 +209,10 @@
 
 
 sub UpgradePackages {
-  my(%currentCompare, @printOut, $printOutLine, $packageName, $packageNameVer, $indexVersion, $currentVersion, $indexRefcount, $currentRefcount, $packagePath, $versionCode, $comment, $rc);
+  my(%currentCompare, @removePass, $removePassLine, @buildPass, $buildPassLine, @printOut, $printOutLine, $packageName, $packageNameVer, $indexVersion, $currentVersion, $indexRefcount, $currentRefcount, $packagePath, $versionCode, $comment, $rc);
+
+  $printOutLine = "# UpgradePackages script generated by bsdpak $version\n\n# DETAIL\n";
+  push @printOut, $printOutLine;
   
   foreach $packageName (keys %currentPackages) {
     $packageNameVer = "$packageName-$currentPackages{$packageName}{'version'}";
@@ -238,10 +264,25 @@
     $currentCompare{'comment'} = $comment;
    
     if($currentCompare{'versioncode'} eq "<") {
-      $printOutLine = "\#\n\# $currentCompare{'name'}\n\# $currentCompare{'comment'}\n\#\ncd $currentCompare{'path'}\nmake && pkg_delete -f $currentCompare{'namever'}\nmake install\nmake clean\n\n";
-      push(@printOut, $printOutLine);
+      $printOutLine = "\# $currentCompare{'name'} $currentCompare{'cversion'} $currentCompare{'comment'}\n";
+      push @printOut, $printOutLine;
+
+      $removePassLine = "pkg_delete -f -R $currentCompare{'namever'}\n";
+      push(@removePass, $removePassLine);
+      $buildPassLine = <<__EOF__;
+cd $currentCompare{'path'}
+make update
+make clean-update clean-depends
+__EOF__
+      push(@buildPass, $buildPassLine);
     } 
   }
+  $printOutLine = "\n# REMOVE PASS\n";
+  push @printOut, $printOutLine;
+  push @printOut, @removePass;
+  $printOutLine = "\n# BUILD PASS\n";
+  push @printOut, $printOutLine;
+  push @printOut, @buildPass;
   print @printOut;
 }
 
@@ -336,10 +377,11 @@
   while(1) {
     ($p1, $v1) = split(/\./, $v1, 2);
     ($p2, $v2) = split(/\./, $v2, 2);
-  
+ 
     if (($p1 eq "") && ($p2 eq "")) { return 0; }
-    elsif ($p1 != $p2) { return $p1 <=> $p2; }
-    elsif ($p1 ne $p2) { return $p1 cmp $p2; }
+    if (($p1 =~ /^\d*$/) && ($p2 =~ /^\d*$/)) {
+      if ($p1 != $p2) { return $p1 <=> $p2; }
+    } elsif ($p1 ne $p2) { return $p1 cmp $p2; }
   }
 }
 
