#!/bin/sh -e
# See http://jdebp.uk./FGA/slashpackage.html
if test \! -d package || test \! -d source
then
	echo "You are not in the right directory." 1>&2
	exit 100
fi

export CDPATH=

# Create and populate the build directory.
install -d -m 0755 build
( cd source && ( 2>/dev/null pax -w -r -l -u -- * ../build/ || true ) )

# Rebuild.
if ! > /dev/null expr -- "${MAKEFLAGS}" : "--jobs" &&
   ! > /dev/null expr -- "${REDOFLAGS}" : "--jobs" &&
   test -z "${cpus}"
then
	case "`uname`" in
	Linux) cpus="`grep -F -c processor /proc/cpuinfo`" ;;
	*BSD) cpus="`sysctl -n hw.ncpu`" ;;
	esac
fi
redo --directory build ${cpus:+--jobs "${cpus}"} -- all

exec ./package/makeinstall
