$NetBSD$

--- ../FreeBSD/lib/buildscript	2011-10-16 00:52:55.000000000 +0000
+++ lib/buildscript
@@ -8,7 +8,7 @@ cleanup() {
 
   if [ -e ${dir}/.keep ]; then
     cd ${dir}
-    objdir=$(make -V WRKDIR)
+    objdir=`bmake -V '\${WRKDIR}'`
     tar cfjC /tmp/work.tbz ${objdir}/.. work
   fi
 
@@ -36,7 +36,6 @@ add_pkg() {
     for i in $pkgs; do
       echo "pkg_add $i"
       base=$(basename $i .tgz)
-      base=$(basename $base .tbz)
       if pkg_info -q -e $base; then
         echo "skipping $base, already added"
       else
@@ -48,6 +47,8 @@ add_pkg() {
       fi
     done
   fi
+  # pkgsrc requires PKG_PATH variable be unset
+  unset PKG_PATH
 }
 
 del_pkg() {
@@ -58,6 +59,8 @@ del_pkg() {
   if [ ! -z "${pkgs}" ]; then
     recursion=1
     dellist=""
+    locklist=""
+    recursion_virgin=1
     while [ $recursion -eq 1 ]; do
       unset delpkg nextpkg
       recursion=0
@@ -72,6 +75,27 @@ del_pkg() {
         fi
       done
       pkgs="${nextpkg}"
+      # Apparently pkgsrc packages can remain installed and in the /var
+      # database even after pkg_delete -f command.  This could send the
+      # script into an infinite recursion loop because Tinderbox doesn't
+      # think that can happen.  To recover from this, if we find a repeating
+      # recursion pattern with an empty delete pattern, bail out.
+      if [ -z "${delpkg}" ]; then
+        if [ "${recursion}" -eq 1 ]; then
+          if [ "${recursion_virgin}" -eq 1 ]; then
+            locklist="${pkgs}"
+            recursion_virgin=0
+          else
+            if [ "${pkgs}" = "${locklist}" ]; then
+              delpkg="${pkgs}"
+              pkgs=""
+              recursion=0
+            fi
+          fi
+        fi
+      else
+        recursion_virgin=1
+      fi
       if [ "$dellist" != "" -a "$dellist" = "$delpkg" ]; then
         echo "deleted list =\""$dellist"\", packages to delete ="\"$delpkg\" #"
         echo "The following packages were left behind (perhaps your dependency list is incomplete):"
@@ -91,6 +115,37 @@ del_pkg() {
     done
   fi
   find /var/db/pkg -type f -empty -delete
+  # pkgsrc requires PKG_PATH variable be unset
+  unset PKG_PATH
+}
+
+get_package_options() {
+    # If the option file exists, it's stored in PKG_OPTIONS_FILE env var.
+    # The format is ${DISTNAME}:jobs override:options list
+    # example: gcc-4.6.1::-gcc-fortran
+    #          In this example, there is no makejob override, fortran removed
+    # example: gawk-3.1.8:1:portals
+    #          In the above example, MAKE_JOBS=1, portals option added
+    # Don't include any "nbX" suffixes, that is ${PKGNAME}.
+    # Use the "DISTNAME" definition in the makefile.
+
+    INFO_DISTNAME=$1
+    MAKEJOBS_OVERRIDE=
+    SPECIFIC_OPTIONS=
+    if [ -z "${PKG_OPTIONS_FILE}" ]; then
+	return
+    fi
+
+    instructions=`grep ${INFO_DISTNAME}: ${PKG_OPTIONS_FILE}`
+    if [ -z "${instructions}" ]; then
+	return
+    fi
+
+    test_override=`echo ${instructions} | awk 'BEGIN {FS=":"}; /^.+:[0-9]+:/ {print $2}'`
+    if [ -n "{test_override}" ]; then
+	MAKEJOBS_OVERRIDE="${test_override}"
+    fi
+    SPECIFIC_OPTIONS="`echo ${instructions} | awk 'BEGIN {FS=":"}; /^.+:[0-9]*:/ {print $3}'`"
 }
 
 dir=$1
@@ -102,35 +157,42 @@ FD=$5
 BD=$6
 RD=$7
 TD=$8
-PLISTCHECK=$9
+pkgname=$9
+PLISTCHECK=${10}
 
 L=$(echo ${LOCALBASE} | sed 's,^/,,')
 
+export NO_PKGTOOLS_REQD_CHECK=1
+export WRKOBJDIR=/work
+
+cd $dir || exit 1
+
+INFO_OPTVAR=`bmake -V '\${PKG_OPTIONS_VAR}'`
+get_package_options `bmake -V '\${DISTNAME}'`
+
 if [ $phase = 1 ]; then
 
-  cd $dir || exit 1
+  INFO_MAINTAINER=`bmake -V '\${MAINTAINER}'`
+  INFO_PREFIX=`bmake -V '\${PREFIX}'`
   echo "build started at $(date)"
   echo "port directory: ${dir}"
   echo "building for:  $(uname -rm)"
-  echo "maintained by: $(make maintainer)"
-  echo "Makefile ident: $(ident ${dir}/Makefile | grep 'FreeBSD:' | sed 's/^[ \t]*//')"
-  echo "prefixes: LOCALBASE=${L} PREFIX=$(make -V PREFIX)"
+  echo "maintained by: ${INFO_MAINTAINER}"
+  echo "Makefile ident: $(ident ${dir}/Makefile | grep 'NetBSD:' | sed 's/^[ \t]*//')"
+  echo "prefixes: LOCALBASE=${L} PREFIX=${INFO_PREFIX}"
   echo "Begin Configuration:"
   echo "---Begin Environment---"
-  printenv
+  printenv | sort
   echo "---End Environment---"
   echo ""
   echo "---Begin OPTIONS List---"
-  make showconfig
+  if [ -n "${INFO_OPTVAR}" -a -n "${SPECIFIC_OPTIONS}" ]; then
+      bmake show-options ${INFO_OPTVAR}="${SPECIFIC_OPTIONS}"
+  else
+      bmake show-options
+  fi
   echo "---End OPTIONS List---"
   echo ""
-  optionsfile=$(make -V OPTIONSFILE)
-  if [ -f "${optionsfile}" ]; then
-      echo "---Begin OPTIONS configuration---"
-      cat ${optionsfile}
-      echo "---End OPTIONS configuration---"
-      echo ""
-  fi
   echo "End Configuration."
 
   echo "FETCH_DEPENDS=${FD}"
@@ -165,6 +227,7 @@ if [ $phase = 1 ]; then
 ./${L}/share/xml
 ./${L}/etc/gconf
 ./var/db/fontconfig
+./distcache
 EOF
 
   # Record a "pristine" mtree.
@@ -173,11 +236,11 @@ EOF
   add_pkg $FD
 
   cd $dir || exit 1
-  pkgname=$(make package-name)
+
   echo "================================================================"
   echo "====================<phase 1: make checksum>===================="
 
-  if /pnohang $TIMEOUT /tmp/make.log1 ${pkgname} make checksum; then
+  if /pnohang $TIMEOUT /tmp/make.log1 ${pkgname} bmake checksum; then
     cat /tmp/make.log1
     echo "0" > /tmp/status
   else
@@ -186,36 +249,41 @@ EOF
 
 else
 
-  cd $dir || exit 1
-  pkgname=$(make package-name)
-
   echo "================================================================"
   echo "====================<phase 2: make extract>===================="
 
   add_pkg ${ED}
+  
+  # For an unfathomable reason, pkgsrc insists on bringing in build
+  # dependencies in order to extract the files.  Not only is this
+  # annoying if you want to check the contents of the tarball and you
+  # have to build all the dependencies first, it means this script
+  # has to be reordered where build dependencies come in very early.
+  
+  add_pkg ${BD}
+
   cd $dir
-  /pnohang $TIMEOUT /tmp/make.log2 ${pkgname} make extract || cleanup 2
+  # The config options need to come here because make extract pulls in 
+  # make configure first, which is where this input is needed.
+  if [ -n "${INFO_OPTVAR}" -a -n "${SPECIFIC_OPTIONS}" ]; then
+      /pnohang $TIMEOUT /tmp/make.log2 ${pkgname} bmake extract ${INFO_OPTVAR}="${SPECIFIC_OPTIONS}" || cleanup 2
+  else
+      /pnohang $TIMEOUT /tmp/make.log2 ${pkgname} bmake extract || cleanup 2
+  fi
   cat /tmp/make.log2
   del_pkg ${ED}
 
-  # Fetch depends still need to be here for 'make extract' since that target
-  # always reruns 'make fetch' due to the lack of fetch cookie (and no place
-  # to put it since WRKDIR isn't created by 'make fetch')
-  del_pkg $FD
-
   echo "================================================================"
   echo "====================<phase 3: make patch>===================="
   add_pkg ${PD}
   cd $dir
-  /pnohang $TIMEOUT /tmp/make.log3 ${pkgname} make patch || cleanup 3
+  /pnohang $TIMEOUT /tmp/make.log3 ${pkgname} bmake patch || cleanup 3
   cat /tmp/make.log3
   del_pkg ${PD}
 
   echo "================================================================"
   echo "====================<phase 4: make build>===================="
 
-  add_pkg ${BD}
-
   # Files we do not care about changing between pre-build and post-cleanup
   cat > /tmp/mtree.buildexclude <<EOF
 ./var/log/*
@@ -247,7 +315,7 @@ EOF
   fi
 
   cd $dir
-  portdir=$(echo ${dir} | sed -e 's|^/usr/ports/||' -e 'y/\//_/')
+  portdir=$(echo ${dir} | sed -e 's|^/usr/pkgsrc/||' -e 'y/\//_/')
 
   if [ -f .sleepme ]; then
       set > /tmp/.set_${portdir}
@@ -259,32 +327,32 @@ EOF
       rm -f /tmp/.set_${portdir}
   fi
 
-  /pnohang $TIMEOUT /tmp/make.log4 ${pkgname} make build || cleanup 4
+  # MAKE_JOBS normally defined in $LOCALBASE/etc/mk.conf, but it's
+  # inside the tarball.  By default it's not set, so we override it here.
+  # We can use the options file to specify exactly the number of jobs we want
+
+  if [ -n "${MAKEJOBS_OVERRIDE}" ]; then
+	echo "=> Number jobs set to ${MAKEJOBS_OVERRIDE} in options file"
+	PORT_JOBS="MAKE_JOBS=${MAKEJOBS_OVERRIDE}"
+  else
+	ncpus=$(/sbin/sysctl hw.ncpu | awk '{print $2}')
+	if [ "${npcus}" = "1" ]; then
+		echo "=> Single job, only 1 CPU"
+		PORT_JOBS="MAKE_JOBS=1"
+	else
+		factor=$(echo "$ncpus*2+1" | /usr/bin/bc)
+		echo "=> Multiple CPUs: ${factor} jobs set"
+		PORT_JOBS="MAKE_JOBS=${factor}"
+	fi
+  fi
+
+  /pnohang $TIMEOUT /tmp/make.log4 ${pkgname} bmake build ${PORT_JOBS} || cleanup 4
   cat /tmp/make.log4
 
   echo "================================================================"
   echo "====================<phase 5: make test>===================="
 
-  pkg_info | awk '{print $1}' | sort > /tmp/pkgs_pre_test
-  add_pkg ${TD}
-  pkg_info | awk '{print $1}' | sort > /tmp/pkgs_post_test      
-  cd $dir
-  /pnohang $TIMEOUT /tmp/make.log5 ${pkgname} make -k regression-test
-  cat /tmp/make.log5
-
-  RTD=`comm -3 /tmp/pkgs_pre_test /tmp/pkgs_post_test | tr -d '\t'`
-  del_pkg ${RTD}
-  rm /tmp/pkgs_pre_test /tmp/pkgs_post_test
-
-  mtree -X /tmp/mtree.buildexclude -x -f /tmp/mtree.prebuild -p / | egrep -v "^(${L}/var|${L}/lib/X11/xserver/SecurityPolicy|${L}/share/nls/POSIX|${L}/share/nls/en_US.US-ASCII|etc/services|compat |${L} |etc/manpath.config|etc/.*.bak|${L}/info/dir|${L}/lib/X11/fonts/.*/fonts\.|${L}/man/..( |/man. )|${L}/lib/X11/fonts/TrueType|${L}/etc/gconf/gconf.xml.defaults/%gconf-tree.*.xml|usr/X11R6 )" > /tmp/list.preinstall
-
-  if [ -s /tmp/list.preinstall ]; then
-      echo "================================================================"
-      echo "Fatal error: filesystem was touched prior to 'make install' phase"
-      cat /tmp/list.preinstall
-      echo "================================================================"
-      cleanup 0
-  fi
+  echo "Pkgsrc does not have per-package regression test capability, skipped."
 
   echo "================================================================"
   echo "====================<phase 6: make install>===================="
@@ -306,7 +374,7 @@ EOF
   mtree -X /tmp/mtree.exclude -xcn -k uid,gid,mode -p / > /tmp/mtree
 
   cd $dir
-  if /pnohang $TIMEOUT /tmp/make.log6 ${pkgname} make install; then
+  if /pnohang $TIMEOUT /tmp/make.log6 ${pkgname} bmake install; then
     cat /tmp/make.log6
     echo "0" > /tmp/status
   else
@@ -316,14 +384,15 @@ EOF
   echo "================================================================"
   echo "====================<phase 7: make package>===================="
   cd $dir
-  if /pnohang $TIMEOUT /tmp/make.log7 ${pkgname} make package; then
+  echo "Starting make process for ${pkgname}"
+  if /pnohang $TIMEOUT /tmp/make.log7 ${pkgname} bmake package; then
       echo "0" > /tmp/status
   else
       echo "1" > /tmp/status
   fi
 
   cat /tmp/make.log7
-  prefix=$(make -V PREFIX)
+  prefix=`bmake -V '\${PREFIX}'`
   du -skx / | awk '{print $1}' > /tmp/size
   del_pkg ${pkgname}
 
@@ -349,14 +418,18 @@ EOF
       echo "================================================================"
   fi
 
-
   echo
+  echo "=== Deleting /var/db/pkg/pkgdb.byfile.db"
+  rm -f /var/db/pkg/pkgdb.byfile.db
+
   echo "=== Checking filesystem state"
 
   if [ -s /tmp/list4 ]; then
     echo "list of extra files and directories in / (not present before this port was installed but present after it was deinstalled)"
     cat /tmp/list4
-    touch /tmp/leftovers
+    # Files left over here are okay as long as they are gone by list 6
+    # Just list them for informational purposes, but don't act on it.
+    # touch /tmp/leftovers
   fi
   if [ -s /tmp/list5 ]; then
     echo "list of files present before this port was installed but missing after it was deinstalled)"
@@ -373,9 +446,10 @@ EOF
   fi
 
   # BUILD_DEPENDS need to be present at install-time, e.g. gmake
+  # FETCH_DEPENDS contains the digests which is needed throught the process
   # Concatenate and remove duplicates
-  BRD=$(echo $BD $RD | tr ' ' '\n' | sort -u | tr '\n' ' ')
-  del_pkg ${BRD}
+  BRFD=$(echo $BD $RD $FD | tr ' ' '\n' | sort -u | tr '\n' ' ')
+  del_pkg ${BRFD}
   cd /var/db/pkg
   if [ $(echo $(echo * | wc -c)) != 2 ]; then
     echo "leftover packages:" *
@@ -395,9 +469,6 @@ EOF
       grep ' missing$' /tmp/list3 > /tmp/list5
       grep -vE ' (extra|missing)$' /tmp/list3 > /tmp/list6
       if [ -n "${PLISTCHECK}" ]; then
-	  if grep -vqE "(${L})/etc/" /tmp/list4; then
-	      #echo "1" > /tmp/status
-	  fi
 	  if [ -s /tmp/list5 ]; then
 	      #echo "1" > /tmp/status
 	  fi
@@ -430,7 +501,7 @@ EOF
 
   if [ -e ${dir}/.keep ]; then
     cd ${dir}
-    objdir=$(make -V WRKDIR)
+    objdir=`bmake -V '\${WRKDIR}'`
     tar cfjC /tmp/work.tbz ${objdir}/.. work
   fi
 
