$NetBSD: patch-aa,v 1.5 2011/12/04 21:55:01 marino Exp $

--- ../FreeBSD/lib/tc_command.sh	2011-11-20 13:11:37.088510000 +0000
+++ lib/tc_command.sh
@@ -27,21 +27,46 @@
 # $MCom: portstools/tinderbox/lib/tc_command.sh,v 1.101.2.48 2011/11/15 07:04:35 marcus Exp $
 #
 
-export _defaultUpdateHost="cvsup18.FreeBSD.org"
+export _defaultUpdateHost="cvsup.netbsd.se"
 export _defaultUpdateType="CSUP"
+export _defaultDragonHost="http://mirror.physik-pool.tu-berlin.de/DragonFlyBSD/dflysnap"
+export _defaultDragonType="RELEASE"
+export _defaultGitSrcHost="koan.bondconsult.net"
 
 #---------------------------------------------------------------------------
 # Generic routines
 #---------------------------------------------------------------------------
+commandTreeChecks () {
+    JT_updateCmd=${1}
+    JT_name=${2}
+    JT_mechanism=${3}
+
+    if [ ! -x "${JT_updateCmd}" ]; then
+	echo "ERROR: ${JT_name} ${JT_mechanism}: ${JT_updateCmd} missing"
+	return 1
+    fi
+
+    if [ -d ${treeDir} ]; then
+	echo "${JT_name}: cleaning out old directories"
+	cleanDirs ${JT_name} ${treeDir}
+    fi
+
+    if [ ! -d ${treeDir} ]; then
+	echo "${JT_name}: creating top-level directory"
+	mkdir -p ${treeDir} >/dev/null 2>&1
+    fi
+    return 0
+}
+
 generateUpdateCode () {
     case ${1} in
 
     "jail")	  treeDir=$(tinderLoc jail ${2})
-		  updateCollection="src-all"
+		  updateCollection="not-applicable"
 		  ;;
 
     "portstree")  treeDir=$(tinderLoc portstree ${2})
-		  updateCollection="ports-all"
+		  updateCollection="netbsd-pkgsrc"
 		  ;;
 
     *)		  echo "ERROR: ${1} ${2}: unknown tree type"
@@ -64,84 +89,89 @@ generateUpdateCode () {
 		fi
 		;;
 
-    "LFTP")
-    		if [ -z "${5}" -o "${5}" = "UNUSED" ]; then
-		    echo "ERROR: ${1} ${2}: no tag specified for ${3}"
+    "RELEASE")  # ONLY USED FOR DRAGONFLY ISO RETRIEVAL (JAILS)
+		updateArch=$(uname -p)
+		if [ -z "${5}" -o "${5}" = "UNUSED" ]; then
+		    echo "ERROR: ${1} ${2}: no release number specified for ${3} (e.g. 2.10.1)"
 		    exit 1
 		fi
 
-		updateArch="${7}"
-		if [ -z "${updateArch}" ]; then
-		    updateArch=$(uname -p)
-		fi
-
-		updateCmd="/usr/local/bin/lftp"
+		updateCmd=/usr/bin/fetch
+		iso_image="dfly-${updateArch}-${5}_REL.iso.bz2"
+		iso_server=${4}
 
-		if [ ! -x "${updateCmd}" ]; then
-		    echo "ERROR: ${2} ${3}: ${updateCmd} missing"
+		commandTreeChecks ${updateCmd} ${2} ${3}
+		if [ "$?" -eq "1" ]; then
 		    exit 1
 		fi
 
-		if [ -d ${treeDir} ]; then
-		    echo "${2}: cleaning out old directories"
-		    cleanDirs ${2} ${treeDir}
-		fi
-		if [ ! -d ${treeDir} ]; then
-		    echo "${2}: creating top-level directory"
-		    mkdir -p ${treeDir} >/dev/null 2>&1
-		fi
-
 		( echo "#!/bin/sh"
+		  echo "if [ -d ${treeDir}/obj ]; then"
+		  echo "  echo Jail ${2} cannot be updated!"
+		  echo "  echo It was created from an official release ISO"
+		  echo "  exit 1"
+		  echo "fi"
 		  echo "mkdir -p ${treeDir}/sets"
 		  echo "cd ${treeDir}/sets"
-		  echo "${updateCmd} -c \"open ftp://${4}/pub/FreeBSD/releases/${updateArch}/${5}/; mirror base\""
-		  echo "${updateCmd} -c \"open ftp://${4}/pub/FreeBSD/releases/${updateArch}/${5}/; mirror dict\""
-		  if [ "${updateArch}" = "amd64" ]; then
-		      echo "${updateCmd} -c \"open ftp://${4}/pub/FreeBSD/releases/${updateArch}/${5}/; mirror lib32\""
-		  fi
-		  echo "${updateCmd} -c \"open ftp://${4}/pub/FreeBSD/releases/${updateArch}/${5}/; mirror proflibs\""
-		  echo "${updateCmd} -c \"open ftp://${4}/pub/FreeBSD/releases/${updateArch}/${5}/; mirror src\""
-		  echo "cd src"
-		  echo "sed -i \"\" 's|usr/src|src|' install.sh"
-		  echo "export DESTDIR=${treeDir}"
-		  echo "mkdir ${treeDir}/src"
-		  echo "yes | sh ./install.sh all"
+		  echo "echo \"SERVER: ${iso_server}/iso-images\""
+		  echo "echo \" IMAGE: ${iso_image}\""
+		  echo "${updateCmd} ${iso_server}/iso-images/${iso_image}"
+		  echo "mkdir -p ../tmp ../obj"
+		  echo "echo \"Initial physical jail setup complete.\""
 		) > ${treeDir}/update.sh
 		chmod +x ${treeDir}/update.sh
 		;;
 
-    "CVSUP"|"CSUP")
-    		if [ -z "${5}" -o "${5}" = "UNUSED" ]; then
-		    echo "ERROR: ${1} ${2}: no tag specified for ${3}"
-		    exit 1
-		fi
-
-		updateCmd=""
-		if [ "${3}" = "CVSUP" ]; then
-		    updateCmd="/usr/local/bin/cvsup"
-		elif [ "${3}" = "CSUP" ]; then
-		    if [ -x /usr/bin/csup ]; then
-			updateCmd="/usr/bin/csup"
+    "SNAPSHOT") # ONLY USED FOR DRAGONFLY SNAPSHOT RETRIEVAL (JAILS)
+		updateArch=$(uname -p)
+		if [ "${5}" = "LATEST" ]; then
+		    iso_image="DragonFly-${updateArch}-LATEST-ISO.iso.bz2"
+		else
+		    namechk=`echo ${5} | awk '/^20[12][0-9][01][0-9][0-3][0-9]-DEV-v[2-9]\.[0-9][0-9]?\.[0-9]\.[0-9][0-9]?[0-9]?[0-9]?\.g[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]?$/'`
+		    if [ -n "${namechk}" ]; then
+			iso_image="images/DragonFly-${updateArch}-${5}.iso.bz2"
 		    else
-			updateCmd="/usr/local/bin/csup"
+			echo "ERROR: ${1} ${2}: tag for ${3} must either equal 'LATEST' or "
+			echo "it must be in the format YYYYMMDD-DEV-va.b.c.d.gxxxxxx"
+			exit 1
 		    fi
 		fi
-		if [ -z "${updateCmd}" ]; then
-		    echo "ERROR: ${2}: unable to determine updateCmd for ${3}"
+
+		updateCmd=/usr/bin/fetch
+		iso_server=${4}
+
+		commandTreeChecks ${updateCmd} ${2} ${3}
+		if [ "$?" -eq "1" ]; then
 		    exit 1
 		fi
-		if [ ! -x "${updateCmd}" ]; then
-		    echo "ERROR: ${2} ${3}: ${updateCmd} missing"
+
+		( echo "#!/bin/sh"
+		  echo "if [ -d ${treeDir}/obj ]; then"
+		  echo "  echo Jail ${2} cannot be updated!"
+		  echo "  echo It was created from a daily snapshot ISO"
+		  echo "  exit 1"
+		  echo "fi"
+		  echo "mkdir -p ${treeDir}/sets"
+		  echo "cd ${treeDir}/sets"
+		  echo "echo \"SERVER: ${iso_server}/snapshots/${updateArch}\""
+		  echo "echo \" IMAGE: ${iso_image}\""
+		  echo "${updateCmd} ${iso_server}/snapshots/${updateArch}/${iso_image}"
+		  echo "mkdir -p ../tmp ../obj"
+		  echo "echo \"Initial physical jail setup complete.\""
+		) > ${treeDir}/update.sh
+		chmod +x ${treeDir}/update.sh
+		;;
+
+    "CSUP")	# ONLY USED FOR PKGSRC UPDATES FROM NETBSD (PORTSTREES)
+		if [ -z "${5}" -o "${5}" = "UNUSED" ]; then
+		    echo "ERROR: ${1} ${2}: no tag specified for ${3}"
 		    exit 1
 		fi
 
-		if [ -d ${treeDir} ]; then
-		    echo "${2}: cleaning out old directories"
-		    cleanDirs ${2} ${treeDir}
-		fi
-		if [ ! -d ${treeDir} ]; then
-		    echo "${2}: creating top-level directory"
-		    mkdir -p ${treeDir} >/dev/null 2>&1
+		updateCmd=/usr/pkg/bin/csup
+		commandTreeChecks ${updateCmd} ${2} ${3}
+		if [ "$?" -eq "1" ]; then
+		    exit 1
 		fi
 
 		( echo "*default host=${4}"
@@ -159,49 +189,42 @@ generateUpdateCode () {
 		) > ${treeDir}/update.sh
 		chmod +x ${treeDir}/update.sh
 		;;
-    "SVN")
-    		if [ -z "${8}" ]; then
-		    echo "ERROR: ${1} ${2}: no protocol specified for ${3}"
-		    exit 1
-		fi
 
-    		if [ -z "${9}" ]; then
-		    echo "ERROR: ${1} ${2}: no host directory specified for ${3}"
+    "GIT")	# ONLY USED FOR RETREIVING DRAGONFLY SOURCE FOR BUILDING JAIL
+		if [ -z "${5}" -o "${5}" = "UNUSED" ]; then
+		    echo "ERROR: ${1} ${2}: no git branch specified for ${3}"
 		    exit 1
 		fi
 
-		updateCmd="/usr/local/bin/svn"
-
-		if [ ! -x "${updateCmd}" ]; then
-		    echo "ERROR: ${2} ${3}: ${updateCmd} missing"
+		updateCmd=/usr/pkg/bin/git
+		commandTreeChecks ${updateCmd} ${2} ${3}
+		if [ "$?" -eq "1" ]; then
 		    exit 1
 		fi
 
-		if [ -d ${treeDir} ]; then
-		    echo "${2}: cleaning out old directories"
-		    cleanDirs ${2} ${treeDir}
-		fi
-
-		if [ ! -d ${treeDir} ]; then
-		    echo "${2}: creating top-level directory"
-		    mkdir -p ${treeDir} >/dev/null 2>&1
-		fi
-
-		case ${1} in
-		"jail")		treeSubDir="src"
-		;;
-		"portstree")	treeSubDir="ports"
-		;;
-		esac
-
 		( echo "#!/bin/sh"
-		  echo "cd ${treeDir}"
-		  echo "if [ ! -d ${treeDir}/${treeSubDir} ]; then"
-		  echo "${updateCmd} co ${8}://${4}/${9} ${treeSubDir}"
+		  echo "echo 'SERVER: ${4}'"
+		  echo "echo 'BRANCH: ${5}'"
+		  echo "if [ -d ${treeDir}/src ]; then"
+		  echo "  cd ${treeDir}/src"
 		  echo "else"
-		  echo "cd ${treeDir}/${treeSubDir}"
-		  echo "${updateCmd} up"
+		  echo "  echo 'Creating shallow source repository.'"
+		  echo "  mkdir ${treeDir}/src"
+		  echo "  cd ${treeDir}/src"
+		  echo "  mkdir ../tmp ../obj"
+		  echo "  ${updateCmd} init"
+		  echo "  ${updateCmd} remote add origin git://${4}/dragonfly.git"
+		  echo "  ${updateCmd} fetch --depth=1 origin"
+		  echo "  ${updateCmd} branch master origin/master"
+		  echo "  echo 'Repository creation complete.'"
+		  echo "fi"
+		  echo "BRANCH=\`${updateCmd} branch | /usr/bin/grep -w ${5}\`"
+		  echo "if [ \"\${BRANCH}\" = \"\" ]; then"
+		  echo "  ${updateCmd} branch ${5} origin/${5}"
 		  echo "fi"
+		  echo "echo 'Ready to pull updates from branch.'"
+		  echo "${updateCmd} checkout ${5}"
+		  echo "${updateCmd} pull"
 		) > ${treeDir}/update.sh
 		chmod +x ${treeDir}/update.sh
 		;;
@@ -223,6 +246,15 @@ setupDefaults () {
     if [ -z "${defaultUpdateType}" ]; then
         export defaultUpdateType=${_defaultUpdateType}
     fi
+    if [ -z "${defaultDragonHost}" ]; then
+        export defaultDragonHost=${_defaultDragonHost}
+    fi
+    if [ -z "${defaultDragonfType}" ]; then
+        export defaultDragonType=${_defaultDragonType}
+    fi
+    if [ -z "${defaultGitSrcHost}" ]; then
+        export defaultGitSrcHost=${_defaultGitSrcHost}
+    fi
 }
 
 tcExists () {
@@ -257,16 +289,21 @@ updateTree () {
 
     echo "${name}: updating ${what} with ${updateCmd}"
 
+    logpipe=/tmp/tbox_logpipe
+    mkfifo ${logpipe}
+    tee ${dir}/update.log < ${logpipe} &
     if [ "${updateCmd}" = "USER" ]; then
-        eval ${dir}/update.sh ${name} > ${dir}/update.log 2>&1
+        ${dir}/update.sh ${name} > ${logpipe}
     else
-	eval ${dir}/update.sh > ${dir}/update.log 2>&1
+	${dir}/update.sh > ${logpipe}
     fi
     if [ $? -ne 0 ]; then
+        rm ${logpipe}
 	echo "updateTree: ${what} ${name}: update failed"
 	echo "    see ${dir}/update.log for more details"
 	return 1
     fi
+    rm ${logpipe}
 }
 
 #---------------------------------------------------------------------------
@@ -274,8 +311,8 @@ updateTree () {
 #---------------------------------------------------------------------------
 
 Setup () {
-    MAN_PREREQS="lang/perl5.[81]*"
-    OPT_PREREQS="lang/php[45] databases/pear-MDB2 www/php[45]-session archivers/p5-Compress-Bzip2"
+    MAN_PREREQS="perl"
+    OPT_PREREQS="php php*-pear-MDB2 p5-Compress-Bzip2"
     PREF_FILES="tinderbox.ph"
     README="$(tinderLoc scripts README)"
     TINDERBOX_URL="http://tinderbox.marcuscom.com/"
@@ -555,7 +592,7 @@ Upgrade () {
     for jail in ${jails}; do
 	f=$(tinderLoc jail ${jail})
 	ucmd=$(${tc} getUpdateCmd -j ${jail} 2>/dev/null)
-	if [ x"${ucmd}" != x"CVSUP" -a x"${ucmd}" != x"CSUP" -a x"${ucmd}" != x"NONE" ]; then
+	if [ x"${ucmd}" != x"CSUP" -a x"${ucmd}" != x"NONE" ]; then
 	    if [ -f "${ucmd}" ]; then
 		mv -f "${ucmd}" "${f}/update.sh"
 		chmod +x "${f}/update.sh"
@@ -570,11 +607,8 @@ Upgrade () {
 		    tinderEcho "WARN: You must manually set the update command for ${jail} to \"USER\" using the query ${query}."
 		fi
 	    fi
-	elif [ x"${ucmd}" = x"CVSUP" -o x"${ucmd}" = x"CSUP" ]; then
-	    updateCmd="/usr/bin/csup"
-	    if [ x"${ucmd}" = x"CVSUP" ]; then
-		updateCmd="/usr/local/bin/cvsup"
-	    fi
+	elif [ x"${ucmd}" = x"CSUP" ]; then
+	    updateCmd="/usr/pkg/bin/csup"
 	    ( echo "#!/bin/sh"
 	      echo "${updateCmd} ${f}/supfile"
 	    ) > ${f}/update.sh
@@ -588,7 +622,7 @@ Upgrade () {
     for portstree in ${portstrees}; do
 	f=$(tinderLoc portstree ${portstree})
 	ucmd=$(${tc} getUpdateCmd -p ${portstree} 2>/dev/null)
-	if [ x"${ucmd}" != x"CVSUP" -a x"${ucmd}" != x"CSUP" -a x"${ucmd}" != x"NONE" ]; then
+	if [ x"${ucmd}" != x"CSUP" -a x"${ucmd}" != x"NONE" ]; then
 	    if [ -f "${ucmd}" ]; then
 		mv -f "${ucmd}" "${f}/update.sh"
 		chmod +x "${f}/update.sh"
@@ -603,17 +637,14 @@ Upgrade () {
 		    tinderEcho "WARN: You must manually set the update command for ${portstree} to \"USER\" using the query ${query}."
 		fi
 	    fi
-	elif [ x"${ucmd}" = x"CVSUP" -o x"${ucmd}" = "CSUP" ]; then
-	    updateCmd="/usr/bin/csup"
-	    if [ x"${ucmd}" = x"CVSUP" ]; then
-		updateCmd="/usr/local/bin/cvsup"
-	    fi
+	elif [ x"${ucmd}" = "CSUP" ]; then
+	    updateCmd="/usr/pkg/bin/csup"
 	    ( echo "#!/bin/sh"
 	      echo "${updateCmd} ${f}/supfile"
 	    ) > ${f}/update.sh
 	    chmod +x ${f}/update.sh
-	    if [ -f "${f}/ports-supfile" ]; then
-		mv -f "${f}/ports-supfile" "${f}/supfile"
+	    if [ -f "${f}/pkgsrc-supfile" ]; then
+		mv -f "${f}/pkgsrc-supfile" "${f}/supfile"
 	    fi
 	fi
     done
@@ -787,38 +818,55 @@ buildJail () {
 	return 1
     fi
 
-    if [ "${updateCmd}" = "LFTP" ]; then
-	export DESTDIR=${J_TMPDIR}
-	cd ${jailBase}/sets/base && yes | sh ./install.sh > ${jailBase}/world.tmp 2>&1
+    if [ "${updateCmd}" = "RELEASE" -o "${updateCmd}" = "SNAPSHOT" ]; then
+	iso_image=`ls ${jailBase}/sets`
+	/usr/bin/tar -xf ${jailBase}/sets/${iso_image} -C ${J_TMPDIR} > ${jailBase}/world.tmp 2>&1
 	rc=$?
-	if [ ${rc} -eq 0 -a -d "${jailBase}/sets/lib32" ]; then
-	    cd ${jailBase}/sets/lib32 && yes | sh ./install.sh >> ${jailBase}/world.tmp 2>&1
-	    rc=$?
+	if [ ${rc} -ne 0 ]; then
+	    echo "ERROR: extract world failed - see ${jailBase}/world.tmp"
+	    buildJailCleanup 1 ${jailName} ${J_SRCDIR}
+	    return 1
 	fi
+	DFVERS=`awk '/^#define __DragonFly_version/ {print $3}' < ${J_TMPDIR}/usr/include/sys/param.h`
+	MAJOR=`echo $DFVERS | awk '{print substr($0,1,1)}'`
+	MINOR=`echo $DFVERS | awk '{print substr($0,2,3)}' | sed 's/0*//'`
+	if [ `expr $MINOR % 2` -eq 0 ]; then
+	    fakebranch="BRANCH=\"RELEASE_${MAJOR}_${MINOR}\""
+	else
+	    fakebranch="BRANCH=\"DEVELOPMENT_${MAJOR}_${MINOR}\""
+	fi
+	mkdir -p ${jailBase}/src/sys/sys ${jailBase}/src/sys/conf
+	echo ${fakebranch} > ${jailBase}/src/sys/conf/newvers.sh
+	cp ${J_TMPDIR}/usr/include/sys/param.h ${jailBase}/src/sys/sys
+	rm -rf ${J_TMPDIR}/usr/src
+	cp -R ${jailBase}/src ${J_TMPDIR}/usr
+	mkdir ${J_TMPDIR}/usr/4bootstrap
+	mv ${J_TMPDIR}/usr/pkg/bin/bmake   ${J_TMPDIR}/usr/4bootstrap
+	mv ${J_TMPDIR}/usr/pkg/etc/mk.conf ${J_TMPDIR}/usr/4bootstrap
+	mv ${J_TMPDIR}/usr/pkg/share/mk    ${J_TMPDIR}/usr/4bootstrap
+	mv ${J_TMPDIR}/usr/pkg/sbin/pkg_*  ${J_TMPDIR}/usr/4bootstrap
+	rm -rf ${J_TMPDIR}/usr/pkg ${J_TMPDIR}/usr/src-sys.tar.bz2
+	rm -rf ${J_TMPDIR}/var/db/pkg
 	execute_hook "postJailBuild" "JAIL=${jailName} DESTDIR=${J_TMPDIR} JAIL_ARCH=${jailArch} MY_ARCH=${myArch} JAIL_OBJDIR=${JAIL_OBJDIR} SRCBASE=${SRCBASE} PB=${pb} RC=${rc}"
-	if [ ${rc} -ne 0 ]; then
-	    echo "ERROR: world failed - see ${jailBase}/world.tmp"
+	if [ $? -ne 0 ]; then
+	    echo "buildJail: Terminating Jail build since hook postJailBuild failed."
 	    buildJailCleanup 1 ${jailName} ${J_SRCDIR}
+	    return 1
 	fi
     else
         # Make world
         echo "${jailName}: making world"
 
-        # determine if we're cross-building world
-        crossEnv=""
-        if [ "${jailArch}" != "${myArch}" ]; then
-	    crossEnv="TARGET_ARCH=${jailArch}"
-        fi
-
         ncpus=$(/sbin/sysctl hw.ncpu | awk '{print $2}')
-        factor=$(echo "$ncpus*2+1" | /usr/bin/bc -q)
+        factor=$(echo "$ncpus*2+1" | /usr/bin/bc)
 
         if [ -n "${NO_JAIL_JOBS}" ]; then
 	    factor=1
         fi
 
-        cd ${SRCBASE} && env DESTDIR=${J_TMPDIR} ${crossEnv} \
-	    make -j${factor} -DNO_CLEAN world > ${jailBase}/world.tmp 2>&1
+        cd ${SRCBASE} && env DESTDIR=${J_TMPDIR} \
+           make -DNO_GCC41 -DNO_GAMES -DNOPROFILE -j${factor} world \
+           > ${jailBase}/world.tmp 2>&1
         rc=$?
         execute_hook "postJailBuild" "JAIL=${jailName} DESTDIR=${J_TMPDIR} JAIL_ARCH=${jailArch} MY_ARCH=${myArch} JAIL_OBJDIR=${JAIL_OBJDIR} SRCBASE=${SRCBASE} PB=${pb} RC=${rc}"
         if [ ${rc} -ne 0 ]; then
@@ -829,21 +877,91 @@ buildJail () {
         # Make a complete distribution
         echo "${jailName}: making distribution"
 
-        # determine if we're cross-building world
-        crossEnv=""
-        if [ "${jailArch}" != "${myArch}" ]; then
-	    crossEnv="TARGET_ARCH=${jailArch}"
-        fi
-        cd ${SRCBASE} && env DESTDIR=${J_TMPDIR} ${crossEnv} \
-	    make distribution > ${jailBase}/distribution.tmp 2>&1
+        cd ${SRCBASE}/etc && env DESTDIR=${J_TMPDIR} \
+	    make -m ${J_TMPDIR}/usr/share/mk distribution > ${jailBase}/distribution.tmp 2>&1
         if [ $? -ne 0 ]; then
 	    echo "ERROR: distribution failed - see ${jailBase}/distribution.tmp"
 	    buildJailCleanup 1 ${jailName} ${J_SRCDIR}
+	    return 1
+	fi
+
+        # Make upgrade required to link device drivers (e.g. <dev/video/...)
+        echo "${jailName}: making world upgrade"
+
+        cd ${SRCBASE} && env DESTDIR=${J_TMPDIR} \
+            make upgrade > ${jailBase}/upgrade.tmp 2>&1
+        if [ $? -ne 0 ]; then
+	    echo "ERROR: world upgrade failed - see ${jailBase}/upgrade.tmp"
+	    buildJailCleanup 1 ${jailName} ${J_SRCDIR}
+	    return 1
 	fi
+
+        # Bootstrap pkgsrc tools using host machine's pkgsrc for source files
+        echo "${jailName}: bootstrapping pkgsrc basic tools"
+
+        CYCLE="devel/bmake \
+               sysutils/install-sh \
+               pkgtools/bootstrap-mk-files \
+               pkgtools/libnbcompat \
+               pkgtools/pkg_install \
+               archivers/bzip2 \
+               archivers/libarchive \
+               security/openssl \
+               net/libfetch \
+               devel/zlib \
+               bootstrap \
+               mk"
+
+        PBS=/usr/pkgsrcbs
+        WKZONE=${J_TMPDIR}${PBS}
+        mkdir ${WKZONE}
+        for component in ${CYCLE}; do
+          mkdir -p ${WKZONE}/${component}
+          dossiers=`ls -A /usr/pkgsrc/${component} | grep -vE '^(README|work$)'`
+          for dossier in ${dossiers}; do
+            cp -R /usr/pkgsrc/${component}/${dossier} ${WKZONE}/${component}
+          done
+        done
+
+        mount_devfs ${J_TMPDIR}/dev
+        chroot ${J_TMPDIR} ${PBS}/bootstrap/bootstrap --prefix=/usr/pkg \
+            --workdir=${PBS}/work > ${jailBase}/pkgsrc.tmp 2>&1
+
+        if [ $? -ne 0 ]; then
+            umount ${J_TMPDIR}/dev
+            echo "ERROR: pkgsrc bootstrap failed - see ${jailBase}/pkgsrc.tmp"
+            buildJailCleanup 1 ${jailName} ${J_SRCDIR}
+            return 1
+        fi
+        umount ${J_TMPDIR}/dev
+
+        mkdir ${J_TMPDIR}/usr/4bootstrap
+
+        # Create appropriate mk.conf, same as ISO version
+        ( echo ".ifdef BSD_PKG_MK       # begin pkgsrc settings"
+          echo "PKG_DBDIR=              /var/db/pkg"
+          echo "LOCALBASE=              /usr/pkg"
+          echo "VARBASE=                /var"
+          echo "PKG_TOOLS_BIN=          /usr/pkg/sbin"
+          echo "PKGMANDIR=              man"
+          echo "FETCH_CMD=              /usr/bin/ftp"
+          echo ".endif                  # end pkgsrc settings"
+        ) > ${J_TMPDIR}/usr/4bootstrap/mk.conf
+
+        mv ${J_TMPDIR}/usr/pkg/bin/bmake   ${J_TMPDIR}/usr/4bootstrap
+        mv ${J_TMPDIR}/usr/pkg/share/mk    ${J_TMPDIR}/usr/4bootstrap
+        mv ${J_TMPDIR}/usr/pkg/sbin/pkg_*  ${J_TMPDIR}/usr/4bootstrap
+        rm -rf ${J_TMPDIR}/usr/pkg ${J_TMPDIR}/var/db/pkg ${WKZONE}
+
+        execute_hook "postJailBuild" "JAIL=${jailName} DESTDIR=${J_TMPDIR} JAIL_ARCH=${jailArch} MY_ARCH=${myArch} JAIL_OBJDIR=${JAIL_OBJDIR} SRCBASE=${SRCBASE} PB=${pb} RC=${rc}"
+        if [ $? -ne 0 ]; then
+            echo "buildJail: Terminating Jail build since hook postJailBuild failed."
+            buildJailCleanup 1 ${jailName} ${J_SRCDIR}
+            return 1
+        fi
     fi
 
     # Various hacks to keep the ports building environment happy
-    touch -f ${J_TMPDIR}/etc/fstab
 
     MTREE_DIR=${SRCBASE}/etc/mtree
     mtree -deU -f ${MTREE_DIR}/BSD.root.dist \
@@ -855,11 +973,8 @@ buildJail () {
     mtree -deU -f ${MTREE_DIR}/BSD.local.dist \
 	  -p ${J_TMPDIR}/usr/local >/dev/null 2>&1
 
-    date '+%Y%m%d' > ${J_TMPDIR}/var/db/port.mkversion
     mkdir -p ${J_TMPDIR}/var/run
 
-    rm -f ${J_TMPDIR}/usr/lib/aout/lib*_p.a
-
     # Create the jail tarball
     echo "${jailName}: creating tarball"
     jailDir=$(tinderLoc jail ${jailName})
@@ -873,7 +988,7 @@ buildJail () {
     fi
 
     # Move new logfiles into place
-    for logfile in world distribution
+    for logfile in world distribution pkgsrc upgrade
     do
 	rm -f ${jailBase}/${logfile}.log
 	mv -f ${jailBase}/${logfile}.tmp ${jailBase}/${logfile}.log 2>/dev/null
@@ -927,29 +1042,25 @@ createJail () {
     jailArch=$(uname -m)
     mountSrc=""
     init=1
-    protocol=""
-    updateHostDirectory=""
 
     setupDefaults
-    updateHost=${defaultUpdateHost}
-    updateType=${defaultUpdateType}
+    updateHostISO=${defaultDragonHost}
+    updateHostSRC=${defaultGitSrcHost}
+    updateType=${defaultDragonType}
 
     # argument handling
-    while getopts a:d:j:m:t:u:CD:H:IP: arg >/dev/null 2>&1
+    while getopts d:j:m:t:u:H:I arg >/dev/null 2>&1
     do
 	case "${arg}" in
 
-	a)	jailArch="${OPTARG}";;
 	d)	descr="${OPTARG}";;
 	j)	jailName="${OPTARG}";;
 	m)	mountSrc="${OPTARG}";;
 	t)	updateTag="${OPTARG}";;
 	u)	updateType="${OPTARG}";;
-	C)	updateCompress=1;;
-	D)	updateHostDirectory="${OPTARG}";;
-	H)	updateHost="${OPTARG}";;
+	H)	updateHostISO="${OPTARG}"
+		updateHostSRC="${OPTTAG}";;
 	I)	init=0;;
-	P)	protocol="${OPTARG}";;
 	?)	return 1;;
 
 	esac
@@ -963,7 +1074,7 @@ createJail () {
 
     valid=$(echo ${jailName} | awk '{if (/^[[:digit:]]/) {print;}}')
     if [ -z "${valid}" ]; then
-	echo "createJail: jail name must begin with a FreeBSD major version"
+	echo "createJail: jail name must begin with a DragonFly BSD major version"
 	return 1
     fi
 
@@ -977,10 +1088,20 @@ createJail () {
 	return 1
     fi
 
+    if [ "${updateType}" = "CSUP" ]; then
+    	echo "createJail: CSUP type cannot be used for DragonFly sources"
+	return 1
+    fi
+
+    updateHost=${updateHostISO}
+    if [ "${updateType}" = "GIT" ]; then
+	updateHost=${updateHostSRC}
+    fi
+
+
     echo "${jailName}: initializing tree"
     generateUpdateCode jail ${jailName} ${updateType} ${updateHost} \
-		       ${updateTag} ${updateCompress} ${jailArch} \
-		       ${protocol} ${updateHostDirectory}
+		       ${updateTag} ${updateCompress} ${jailArch}
 
     echo -n "${jailName}: adding to datastore... "
 
@@ -1091,28 +1212,25 @@ createPortsTree () {
     init=1
     mountSrc=""
     portsTreeName=""
-    protocol=""
-    updateHostDirectory=""
+    cvsTag="."
 
     setupDefaults
     updateHost=${defaultUpdateHost}
     updateType=${defaultUpdateType}
 
     # argument handling
-    while getopts d:m:p:u:w:CD:H:IP: arg >/dev/null 2>&1
+    while getopts d:m:t:p:w:CH:I arg >/dev/null 2>&1
     do
 	case "${arg}" in
 
 	d)	descr="${OPTARG}";;
 	m)	mountSrc="${OPTARG}";;
 	p)	portsTreeName="${OPTARG}";;
-	u)	updateType="${OPTARG}";;
+	t)	cvsTag="${OPTARG}";;
 	w)	cvswebUrl="${OPTARG}";;
 	C)	updateCompress=1;;
-	D)	updateHostDirectory="${OPTARG}";;
 	H)	updateHost="${OPTARG}";;
 	I)	init=0;;
-	P)	protocol="${OPTARG}";;
 	?)	return 1;;
 
 	esac
@@ -1134,10 +1252,15 @@ createPortsTree () {
 	return 1
     fi
 
+    if [ "${updateType}" != "CSUP" ]; then
+    	echo "createPortsTree: The defaultUpdateType variable in env was changed to ${updateType}"
+    	echo "Please change it back to 'CSUP', which is the only method supported to get pkgsrc"
+    	return 1
+    fi
+
     echo "${portsTreeName}: initializing tree"
     generateUpdateCode portstree ${portsTreeName} ${updateType} \
-		       ${updateHost} "." ${updateCompress} "" \
-		       ${protocol} ${updateHostDirectory}
+		       ${updateHost} ${cvsTag} ${updateCompress}
 
     # add portstree to datastore
     echo -n "${portsTreeName}: adding to datastore... "
@@ -1192,6 +1315,9 @@ enterBuild () {
         esac
     done
 
+    tc=$(tinderLoc scripts tc)
+    HOST_WORKDIR=$(${tc} configGet | awk -F= '/^HOST_WORKDIR/ {print $2}')
+
     if [ -z "${portDir}" ]; then
 	echo "enterBuild: no port specified"
 	return 1
@@ -1214,7 +1340,7 @@ enterBuild () {
     fi
 
     sleepName=$(echo ${portDir} | sed -e 'y/\//_/')
-    portFullDir=${buildRoot}/usr/ports/${portDir}
+    portFullDir=${buildRoot}/usr/pkgsrc/${portDir}
 
     if [ ! -d ${portFullDir} ]; then
 	echo "enterBuild: Build environment does not exist yet, sleeping."
@@ -1238,7 +1364,7 @@ enterBuild () {
 	sleep 15
     done
 
-    echo 
+    echo
     cp $(tinderLoc scripts lib/enterbuild) ${buildRoot}/root
     chroot ${buildRoot} /root/enterbuild ${portDir}
     rm -f ${buildRoot}/tmp/.sleep_${sleepName}
@@ -1325,7 +1451,6 @@ makeBuild () {
 resetBuild () {
     # set up defaults
     build=""
-    nullfs=""
     cleandistfiles="0"
 
     # argument handling
@@ -1334,7 +1459,6 @@ resetBuild () {
 	case "${arg}" in
 
 	b)	build="${OPTARG}";;
-	n)	nullfs="-n";;
 	?)	exit 1;;
 
 	esac
@@ -1490,22 +1614,23 @@ tinderbuild_setup () {
     echo "tinderbuild: Finalizing chroot environment"
 
     # Mount ports/
-    if ! requestMount -t buildports -b ${build} -r ${nullfs}; then
+    if ! requestMount -t buildports -b ${build} -r ; then
 	echo "tinderbuild: cannot mount ports source"
 	tinderbuild_cleanup 1
     fi
-    ln -sf ../a/ports ${buildRoot}/usr/ports
+    rm -rf ${buildRoot}/usr/pkgsrc
+    ln -s ../a/pkgsrc ${buildRoot}/usr/pkgsrc
 
     # Mount src/
-    if ! requestMount -t buildsrc -b ${build} -r ${nullfs}; then
+    if ! requestMount -t buildsrc -b ${build} -r ; then
 	echo "tinderbuild: cannot mount jail source"
 	tinderbuild_cleanup 1
     fi
 
     # For use by pnohang
     # XXX: though killall may not work since it's a dynamic executable
-    cp -p /rescue/mount /rescue/umount ${buildRoot}/sbin
-    cp -p /rescue/ps ${buildRoot}/bin
+    cp -p /sbin/mount /sbin/umount ${buildRoot}/sbin
+    cp -p /bin/ps ${buildRoot}/bin
 
     # Mount /dev, since we're going to be chrooting shortly
     mount -t devfs devfs ${buildRoot}/dev >/dev/null 2>&1
@@ -1522,12 +1647,6 @@ tinderbuild_setup () {
 	tinderbuild_cleanup 1
     fi
 
-    # Hack to fix some recent pkg_add problems in some releases
-    pitar=$(tinderLoc jail ${jail})/pkg_install.tar
-    if [ -f ${pitar} ]; then
-	tar -C ${buildRoot} -xf ${pitar}
-    fi
-
     # Handle the distfile cache
     if [ -n "${DISTFILE_CACHE}" ]; then
 	if ! requestMount -t builddistcache -b ${build} \
@@ -1557,7 +1676,7 @@ tinderbuild_setup () {
 	mkdir -p ${ccacheDir} $(tinderLoc buildccache ${build})
 
 	if ! requestMount -t buildccache -b ${build} \
-		-s ${ccacheDir} ${nullfs}; then
+		-s ${ccacheDir} ; then
 	    echo "tinderbuild: cannot mount ccache"
 	    tinderbuild_cleanup 1
 	fi
@@ -1574,8 +1693,7 @@ tinderbuild_setup () {
 
 	mkdir -p ${optionsDir} $(tinderLoc buildoptions ${build})
 
-	if ! requestMount -t buildoptions -b ${build} \
-	    	-s ${optionsDir} ${nullfs}; then
+	if ! requestMount -t buildoptions -b ${build} -s ${optionsDir} ; then
 	    echo "tinderbuild: cannot mount options"
 	    tinderbuild_cleanup 1
 	fi
@@ -1658,10 +1776,9 @@ tinderbuild () {
     cleanpackages=0
     init=0
     jobs=1
-    onceonly=0
+    twice=0
     onlymake=0
     noduds=""
-    nullfs=""
     pbargs=""
     skipmake=0
     updateports=0
@@ -1708,9 +1825,8 @@ tinderbuild () {
 	x-fetch-original)	pbargs="${pbargs} -fetch-original";;
 	x-noclean)		pbargs="${pbargs} -noclean";;
 	x-nolog)		pbargs="${pbargs} -nolog";;
-	x-nullfs)		pbargs="${pbargs} -nullfs"; nullfs="-n";;
 	x-plistcheck)		pbargs="${pbargs} -plistcheck";;
-	x-onceonly)		onceonly=1;;
+	x-twice)		twice=1;;
 
 	-*)			return 1;;
 	*)			ports="${ports} $1";;
@@ -1836,7 +1952,8 @@ tinderbuild () {
 		echo "tinderbuild: cannot mount portstree: ${portstree}"
 		exit 1
 	    fi
-	    env PORTSDIR=$(tinderLoc portstree ${portstree})/ports \
+	    env PORTSDIR=$(tinderLoc portstree ${portstree})/pkgsrc \
+	        OPTNFILE=$(tinderLoc options $build)/pkg_options \
 		$(tinderLoc scripts lib/makemake) ${noduds} ${build} ${ports}
 	)
 	if [ $? -ne 0 ]; then
@@ -1855,10 +1972,6 @@ tinderbuild () {
 
     # Set up the chrooted environment
     osmajor=$(echo ${jail} | sed -E -e 's|(^[[:digit:]]+).*$|\1|')
-    if [ ${osmajor} -lt 6 ]; then
-	echo "tinderbuild: unhandled OS version: ${osmajor}"
-	tinderbuild_cleanup 1
-    fi
 
     tinderbuild_setup
 
@@ -1866,7 +1979,7 @@ tinderbuild () {
     ${tc} updateBuildStatus -b ${build} -s PORTBUILD
     tinderbuild_phase 0 ${jobs} ${pkgDir}
     error=$?
-    if [ ${onceonly} -ne 1 ]; then
+    if [ ${twice} -eq 1 ]; then
 	if [ ${error} -ne 0 ] ; then
 	    tinderbuild_setup
 	fi
@@ -1885,21 +1998,46 @@ init () {
 	mkdir -p ${pb}/${dir}
     done
 
-    read -p "Enter a default cvsup host [${_defaultUpdateHost}]: " host
+    read -p "Enter a default cvsup server for pkgsrc [${_defaultUpdateHost}]: " host
     if [ -z "${host}" ]; then
 	host=${_defaultUpdateHost}
     fi
 
-    read -p "Enter a default update type or command [${_defaultUpdateType}]: " type
-    if [ -z "${type}" ]; then
-	type=${_defaultUpdateType}
+    # Update type is not optional, it's CSUP only, so we won't ask.
+
+    defaultDragonType="n"
+    while [ "${defaultDragonType}" != "RELEASE" -a "${defaultDragonType}" != "SNAPSHOT" ]; do
+	read -p "Enter a DragonFly branch type [${_defaultDragonType}]: " defaultDragonType
+	if [ -z "${defaultDragonType}" ]; then
+	    defaultDragonType=${_defaultDragonType}
+	fi
+    done
+
+    if [ "${defaultDragonType}" = "RELEASE" ]; then
+	echo "Server format: (http|ftp)://(host)/path_to_iso-images_directory"
+    else
+	echo "Server format: (http|ftp)://(host)/path_to_snapshots_directory"
     fi
+    read -p "Enter a default ISO server for DragonFly [${_defaultDragonHost}]: " dragonhost
+    if [ -z "${dragonhost}" ]; then
+	dragonhost=${_defaultDragonHost}
+    fi
+
+    read -p "Enter a default Git server for DragonFly [${_defaultGitSrcHost}]: " gitsrchost
+    if [ -z "${gitsrchost}" ]; then
+	gitsrchost=${_defaultGitSrcHost}
+    fi
+
+    # Git Update type for Git is limited to git://, so we won't ask
 
     globalenv=$(tinderLoc scripts etc/env)/GLOBAL
     echo "export defaultUpdateHost=${host}" >> ${globalenv}
-    echo "export defaultUpdateType=${type}" >> ${globalenv}
+    echo "export defaultUpdateType=${_defaultUpdateType}" >> ${globalenv}
+    echo "export defaultDragonHost=${dragonhost}" >> ${globalenv}
+    echo "export defaultDragonType=${_defaultDragonType}" >> ${globalenv}
+    echo "export defaultGitSrcHost=${gitsrchost}" >> ${globalenv}
 
-    tinderEcho "Default update host and type have been set.  These can be changed later by modifying ${globalenv}."
+    tinderEcho "The defaults have been set.  These can be changed later by modifying ${globalenv}."
 
     return 0
 }
@@ -1948,7 +2086,8 @@ addPortToBuild () {
     buildenv ${jail} ${portsTree} ${build}
     buildenvNoHost ${build}
 
-    export PORTSDIR=$(tinderLoc portstree ${portsTree})/ports
+    export PORTSDIR=$(tinderLoc portstree ${portsTree})/pkgsrc
+    export OPTNFILE=$(tinderLoc options $build)/pkg_options
     if [ -z "${portDir}" ]; then
 	${tc} addPortToOneBuild -b ${build} ${norecurse}
     else
@@ -1958,36 +2097,7 @@ addPortToBuild () {
         fi
         ${tc} addPortToOneBuild -b ${build} -d ${portDir} ${norecurse}
     fi
-    if [ ${options} -eq 1 -a ${OPTIONS_ENABLED} -eq 1 ]; then
-	pdirs=""
-	if [ -z "${portDir}" ]; then
-	    pdirs=$(${tc} getPortsForBuild -b ${build} 2>/dev/null)
-	else
-	    pdirs="${PORTSDIR}/${portDir}"
-	fi
-	rmconfig=true
-	if [ ${cleanOptions} -eq 1 ]; then
-	    if [ -z "${norecurse}" ]; then
-		rmconfig="make rmconfig-recursive"
-	    else
-		rmconfig="make rmconfig"
-	    fi
-	fi
-	for pdir in ${pdirs}; do
-	    if [ -d ${pdir} ]; then
-	        export TERM=${save_TERM}
-	        read -p "Generating options for ${build}; hit Enter to continue..." key
-	        echo ""
-	        if [ -z "${norecurse}" ]; then
-		    ( cd ${pdir} && ${rmconfig} \
-		      && make -k config-recursive )
-	        else
-		    ( cd ${pdir} && ${rmconfig} \
-		      && make config )
-	        fi
-	    fi
-	done
-    fi
+    # FreeBSD-style interactive option entering not supported yet
 
     if [ -n "${save_SRCBASE}" ]; then
 	export SRCBASE=${save_SRCBASE}
@@ -2174,10 +2284,7 @@ copyBuild () {
 		if [ ! -d ${destOptionsDir} ]; then
 		    mkdir -p ${destOptionsDir}
 		fi
-		(
-		  cd ${srcOptionsDir}
-		  tar -cpf - . | tar -C ${destOptionsDir} -xpf -
-		)
+		cp ${srcOptionsDir}/pkg_options ${destOptionsDir}
 	    else
 		echo "copyBuild: not copying OPTIONS to ${dest} since it has no OPTIONS directory"
 	    fi
@@ -2293,13 +2400,13 @@ tbcleanup () {
 	pathFound=0
         for portstree in ${portstrees} ; do
 	    path=$(tinderLoc portstree ${portstree})
-	    path="${path}/ports/${port}/Makefile"
+	    path="${path}/pkgsrc/${port}/Makefile"
 	    if [ -e ${path} ]; then
 	        if [ ${cleanDistfiles} = 1 ]; then
 		    oldcwd=${PWD}
 		    path=$(tinderLoc portstree ${portstree})
-		    cd "${path}/ports/${port}"
-		    distinfo=$(env PORTSDIR="${path}/ports" make -V MD5_FILE)
+		    cd "${path}/pkgsrc/${port}"
+		    distinfo=$(env PORTSDIR="${path}/pkgsrc" bmake -V MD5_FILE)
 		    if [ -f "${distinfo}" ]; then
 			for df in $(grep '^MD5' ${distinfo} | awk -F '[\(\)]' '{print $2}'); do
 			    if ! grep -q "^${df}\$" ${disttmp}; then
@@ -2383,7 +2490,7 @@ tbcleanup () {
 	    fi
 
 	    path=$(tinderLoc portstree ${portstree})
-	    path="${path}/ports/${port}/Makefile"
+	    path="${path}/pkgsrc/${port}/Makefile"
 
 	    if [ ! -e ${path} ]; then
 		echo "Removing build port database entry for nonexistent port ${build}/${port}"
