From 1fb261bb971f30ee9956dfef6b983c7e57f4b72a Mon Sep 17 00:00:00 2001
From: Juan RP <xtraeme@gmail.com>
Date: Tue, 22 Apr 2014 16:01:05 +0200
Subject: [PATCH] xbps-src: use default hostdir if unset; added support for x86
 chroots ootb.

- If -H (hostdir) is unset, it now defaults to xbps-packages/hostdir. This way
  a single hostdir can be shared by multiple masterdirs without any tweak.

- If bootstrap pkg are installed via 'binary-bootstrap arch', add support to
  reexec xbps-src with linux32 to allow 32bit builds without any tweak or
  additional commands.
---
 .gitignore |  1 +
 xbps-src   | 40 ++++++++++++++++++++++++++--------------
 2 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/.gitignore b/.gitignore
index 3ec49bf967c..dcf16bc7eb3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,4 +4,5 @@
 \#*#
 *.o
 masterdir*
+hostdir*
 etc/conf
diff --git a/xbps-src b/xbps-src
index 5f28b3d823f..88945b4595b 100755
--- a/xbps-src
+++ b/xbps-src
@@ -119,6 +119,7 @@ Options:
      Absolute path to a directory to be bind mounted at <masterdir>/host.
      The host directory stores binary packages, sources and package dependencies
      downloaded from remote repositories.
+     If unset defaults to xbps-packages/hostdir.
 
  -h  Usage output.
 
@@ -131,6 +132,7 @@ Options:
  -m  <masterdir>
      Absolute path to a directory to be used as masterdir.
      The masterdir is the main directory to build/store/compile packages.
+     If unset defaults to xbps-packages/masterdir.
 
  -N  Disable use of remote repositories to resolve dependencies.
 
@@ -175,6 +177,10 @@ check_config_vars() {
     fi
     if [ -z "$XBPS_MASTERDIR" ]; then
         export XBPS_MASTERDIR="${XBPS_DISTDIR}/masterdir"
+    fi
+    if [ -z "$XBPS_HOSTDIR" ]; then
+        export XBPS_HOSTDIR="${XBPS_DISTDIR}/hostdir"
+        [ ! -d $XBPS_HOSTDIR ] && mkdir -p $XBPS_HOSTDIR
     fi
 	if [ -d "$XBPS_MASTERDIR" -a ! -w "$XBPS_MASTERDIR" ]; then
 		echo "ERROR: not enough perms for masterdir $XBPS_MASTERDIR."
@@ -228,12 +234,12 @@ install_bbotstrap() {
             _subarch="-${XBPS_TARGET_PKG#*-}"
         fi
 	fi
-	${_bootstrap_arch} xbps-install -S ${XBPS_INSTALL_ARGS} -c host/repocache -r $XBPS_MASTERDIR -y base-chroot${_subarch}
+	${_bootstrap_arch} xbps-install -S ${XBPS_INSTALL_ARGS} -c ${XBPS_HOSTDIR}/repocache -r $XBPS_MASTERDIR -y base-chroot${_subarch}
 	if [ $? -ne 0 ]; then
 		msg_error "Failed to install bootstrap packages!\n"
 	fi
 	# Reconfigure base-directories.
-	xbps-reconfigure -r $XBPS_MASTERDIR -f base-directories &>/dev/null
+	XBPS_ARCH=$XBPS_TARGET_PKG xbps-reconfigure -r $XBPS_MASTERDIR -f base-directories &>/dev/null
 	msg_normal "Installed bootstrap successfully!\n"
 	chroot_prepare $XBPS_TARGET_PKG || msg_error "Failed to initialize chroot!\n"
 }
@@ -294,21 +300,23 @@ readonly XBPS_MACHINE=$(uname -m)
 #
 # main()
 #
+XBPS_OPTIONS=
+
 while getopts "a:CfghH:Ij:Lm:No:r:V" opt; do
 	case $opt in
-	a) readonly XBPS_CROSS_BUILD="$OPTARG";;
-	C) readonly XBPS_KEEP_ALL=1;;
-	f) readonly XBPS_BUILD_FORCEMODE=1;;
-	g) readonly XBPS_DEBUG_PKGS=1;;
-	H) readonly XBPS_HOSTDIR="$(readlink -m $OPTARG 2>/dev/null)";;
+	a) readonly XBPS_CROSS_BUILD="$OPTARG"; XBPS_OPTIONS+="-a $OPTARG ";;
+	C) readonly XBPS_KEEP_ALL=1; XBPS_OPTIONS+="-C ";;
+	f) readonly XBPS_BUILD_FORCEMODE=1; XBPS_OPTIONS+="-f ";;
+	g) readonly XBPS_DEBUG_PKGS=1; XBPS_OPTIONS+="-g ";;
+	H) readonly XBPS_HOSTDIR="$(readlink -m $OPTARG 2>/dev/null)"; XBPS_OPTIONS+="-H $XBPS_HOSTDIR ";;
 	h) usage && exit 0;;
-	I) readonly XBPS_SKIP_DEPS=1;;
-	j) readonly XBPS_MAKEJOBS="$OPTARG";;
-	L) export NOCOLORS=1;;
-	m) readonly XBPS_MASTERDIR=$(readlink -m $OPTARG 2>/dev/null);;
-	N) readonly XBPS_SKIP_REMOTEREPOS=1;;
-	o) readonly XBPS_BUILD_OPTS="$OPTARG";;
-	r) readonly XBPS_ALT_REPOSITORY="$OPTARG";;
+	I) readonly XBPS_SKIP_DEPS=1; XBPS_OPTIONS+="-I ";;
+	j) readonly XBPS_MAKEJOBS="$OPTARG"; XBPS_OPTIONS+="-j $OPTARG ";;
+	L) export NOCOLORS=1; XBPS_OPTIONS+="-L ";;
+	m) readonly XBPS_MASTERDIR=$(readlink -m $OPTARG 2>/dev/null); XBPS_OPTIONS+="-m $XBPS_MASTERDIR ";;
+	N) readonly XBPS_SKIP_REMOTEREPOS=1; XBPS_OPTIONS+="-N ";;
+	o) readonly XBPS_BUILD_OPTS="$OPTARG"; XBPS_OPTIONS+="-o $OPTARG ";;
+	r) readonly XBPS_ALT_REPOSITORY="$OPTARG"; XBPS_OPTIONS+="-r $OPTARG ";;
 	V) echo $XBPS_SRC_VERSION && exit 0;;
 	--) shift; break;;
 	esac
@@ -409,6 +417,10 @@ fi
 
 if [ -s $XBPS_MASTERDIR/.xbps_chroot_init ]; then
     export XBPS_ARCH=$(cat $XBPS_MASTERDIR/.xbps_chroot_init)
+    if [ "$XBPS_MACHINE" = "x86_64" -a "$XBPS_ARCH" = "i686" -a -z "$IN_CHROOT" ]; then
+        # reexec itself via linux32
+        exec linux32 $0 ${XBPS_OPTIONS} $@
+    fi
 fi
 
 export XBPS_SHUTILSDIR XBPS_CROSSPFDIR XBPS_TRIGGERSDIR \