#!/bin/sh -e

TARGET="$1"
ROOTSTRAP="$2"

if (echo " $@ " | grep -qE " (-h|--help) ") || [ x"$3" != x ]; then
	cat <<EOF
Usage: $0 [<TARGET>] [<ROOTSTRAP>]

Resets target, extracts a rootstrap (optional) and installs system files
to it.  If target is not specified, the currently active target is used.
EOF
	exit 1
fi

if [ x"$ROOTSTRAP" = x ] && (echo "$TARGET" | grep -qE "(/|\.tar\.|\.tgz$)"); then
	ROOTSTRAP="$TARGET"
	TARGET=
fi

if [ x"$TARGET" = x ]; then
	TARGET="`sb-conf current`"
fi

echo "Resetting $TARGET..."
sb-conf reset "$TARGET" --force

if [ x"$ROOTSTRAP" != x ]; then
	echo "Extracting rootstrap..."
	sb-conf rootstrap "$TARGET" "$ROOTSTRAP"
fi

echo "Installing system files..."
sb-conf install "$TARGET" --etc --devkits --fakeroot
