--- old/build/buildctl	Wed Mar 26 11:41:33 2014
+++ new/build/buildctl	Wed Mar 26 11:41:33 2014
@@ -165,6 +165,10 @@
 	if [ -z "$tobuild" ] || [ "$tobuild" == "all" ]; then
 		batch_flag="-b"
 		for tgt in "${!fulltargets[@]}"; do
+                        # Uncomment the echo line if you want to see a
+                        # one-package-per-line status of what's building in
+                        # /tmp/debug.<PID>.
+                        # echo "Target = $tgt" >> /tmp/debug.$$
 			build $tgt
 		done
 	else
--- old/build/caiman/build.sh	Wed Mar 26 11:41:34 2014
+++ new/build/caiman/build.sh	Wed Mar 26 11:41:34 2014
@@ -117,7 +117,7 @@
     logmsg "Getting Closed Source Bins..."
     for bin in on-closed-bins.i386.tar.bz2 on-closed-bins-nd.i386.tar.bz2 ; do
         if [[ ! -f $bin ]]; then
-	    get_resource illumos-gate/$bin
+	    get_resource illumos-gate/$bin || logerr "Unable to get $bin"
         fi
         logcmd tar xpf $bin
     done
--- old/build/illumos-kvm/build.sh	Wed Mar 26 11:41:34 2014
+++ new/build/illumos-kvm/build.sh	Wed Mar 26 11:41:34 2014
@@ -36,7 +36,13 @@
 # Default to building tip, but site.sh can force a specific commit checkout.
 COMMIT=$KVM_ROLLBACK
 SRC_REPO=https://github.com/joyent/illumos-kvm.git
-KERNEL_SOURCE=/code/omnios-$RELVER/illumos-omnios
+if [ -d ${PREBUILT_ILLUMOS:-/dev/null} ]; then
+    logmsg "Using pre-built Illumos at $PREBUILT_ILLUMOS (may need to wait...)"
+    wait_for_prebuilt
+    KERNEL_SOURCE=$PREBUILT_ILLUMOS
+else
+    KERNEL_SOURCE=/code/omnios-$RELVER/illumos-omnios
+fi
 PROTO_AREA=$KERNEL_SOURCE/proto/root_i386
 PATCHDIR=patches.$PROG
 PKG=driver/virtualization/kvm
@@ -108,7 +114,13 @@
 # Default to building tip, but site.sh can force a specific commit checkout.
 COMMIT=$KVM_CMD_ROLLBACK
 SRC_REPO=https://github.com/joyent/illumos-kvm-cmd.git
-KERNEL_SOURCE=/code/omnios-$RELVER/illumos-omnios
+if [ -d $PREBUILT_ILLUMOS:-/dev/null ]; then
+    logmsg "Using pre-built Illumos at $PREBUILT_ILLUMOS (may need to wait...)"
+    wait_for_prebuilt
+    KERNEL_SOURCE=$PREBUILT_ILLUMOS
+else
+    KERNEL_SOURCE=/code/omnios-$RELVER/illumos-omnios
+fi
 KVM_DIR=$TMPDIR/illumos-kvm-$VER
 PATCHDIR=patches.$PROG
 PKG=system/kvm
--- old/build/illumos/build.sh	Wed Mar 26 11:41:34 2014
+++ new/build/illumos/build.sh	Wed Mar 26 11:41:34 2014
@@ -165,9 +165,11 @@
 push_pkgs() {
     logmsg "Entering $CODEMGR_WS"
     pushd $CODEMGR_WS > /dev/null
-    logmsg "Intentional pause: Last chance to sanity-check before publication!"
     logmsg "Pushing illumos pkgs to $PKGSRVR..."
-    ask_to_continue
+    if [[ -z $BATCH ]]; then
+        logmsg "Intentional pause: Last chance to sanity-check before publication!"
+        ask_to_continue
+    fi
     logcmd pkgrecv -s packages/i386/nightly-nd/repo.redist/ -d $PKGSRVR 'pkg:/*'
     logmsg "Leaving $CODEMGR_WS"
     popd > /dev/null
@@ -175,11 +177,26 @@
 
 init
 prep_build
-sunstudio_location
-clone_source
-modify_build_script
-closed_bins
-build_tools
-build_pkgs
-push_pkgs
+if [ -d ${PREBUILT_ILLUMOS:-/dev/null} ]; then
+    wait_for_prebuilt
+    # Check for existing packages, or for freshly built ones if we pwaited.
+    if [ -d $PREBUILT_ILLUMOS/packages/i386/nightly-nd/repo.redist ]; then
+        logmsg "Using illumos-omnios pre-compiled at $PREBUILT_ILLUMOS"
+        CODEMGR_WS=$PREBUILT_ILLUMOS
+        push_pkgs
+    else
+        logmsg "No $PREBUILT_ILLUMOS/packages/i386/nightly-nd/repo.redist"
+        if [[ -z $BATCH ]]; then
+            ask_to_continue
+        fi
+    fi
+else
+    sunstudio_location
+    clone_source
+    modify_build_script
+    closed_bins
+    build_tools
+    build_pkgs
+    push_pkgs
+fi
 clean_up
--- old/lib/functions.sh	Wed Mar 26 11:41:34 2014
+++ new/lib/functions.sh	Wed Mar 26 11:41:34 2014
@@ -1137,5 +1137,32 @@
     eval "$NEWNAME_FUNC"
 }
 
+# Called by builds that need a PREBUILT_ILLUMOS actually finished.
+wait_for_prebuilt() {
+    if [ ! -d ${PREBUILT_ILLUMOS:-/dev/null} ]; then
+	echo "wait_for_prebuilt() called w/o PREBUILT_ILLUMOS. Bailing."
+	clean_up
+	exit 1
+    fi
+
+    # -h means symbolic link. That's what nightly does.
+    if [ ! -h $PREBUILT_ILLUMOS/log/nightly.lock ]; then
+	return
+    fi
+
+    nightly_pid=`ls -lt $PREBUILT_ILLUMOS/log/nightly.lock | awk -F. '{print $4}'`
+    # Wait for nightly to be finished if it's running.
+    logmsg "Waiting for illumos nightly build $nightly_pid to be finished."
+    pwait $nightly_pid
+    if [ -h $PREBUILT_ILLUMOS/log/nightly.lock ]; then
+        logmsg "Nightly lock present, but build not running.  Bailing."
+        if [[ -z $BATCH ]]; then
+            ask_to_continue
+        fi
+        clean_up
+        exit 1
+    fi
+}
+
 # Vim hints
 # vim:ts=4:sw=4:et:
--- old/lib/site.sh	Wed Mar 26 11:41:34 2014
+++ new/lib/site.sh	Wed Mar 26 11:41:34 2014
@@ -8,6 +8,11 @@
 #
 #PKGSRVR=file:///$MYDIR/../tmp.repo/
 
+# Uncommenting this line will use a pre-built illumos-omnios, instead of having
+# us build it.  NOTE: A build of illumos-omnios can be launched concurrently in
+# conjunction with setting this variable. See functions.sh:wait_for_prebuilt().
+#PREBUILT_ILLUMOS=$HOME/build/prebuild
+
 # These two should be uncommented and set to specific git changeset IDs
 # if illumos-kvm and illumos-kvm-cmd get too far ahead of illumos-{gate,omnios}.
 #KVM_ROLLBACK=a8ea37e8deb99265682c66c23f787f704e77fb91
