#!/usr/bin/env bash
set -e
if [ "$GOOS" = "" -o "$GOARCH" = "" ]; then
echo "usage: GOOS=os GOARCH=arch ./bootstrap.bash [-force]" >&2
exit 2
fi
forceflag=""
if [ "$1" = "-force" ]; then
forceflag=-force
shift
fi
targ="../../go-${GOOS}-${GOARCH}-bootstrap"
if [ -e $targ ]; then
echo "$targ already exists; remove before continuing"
exit 2
fi
unset GOROOT
src=$(cd .. && pwd)
echo "#### Copying to $targ"
cp -Rp "$src" "$targ"
cd "$targ"
echo
echo "#### Cleaning $targ"
chmod -R +w .
rm -f .gitignore
if [ -e .git ]; then
git clean -f -d
fi
echo
echo "#### Building $targ"
echo
cd src
./make.bash --no-banner $forceflag
gohostos="$(../bin/go env GOHOSTOS)"
gohostarch="$(../bin/go env GOHOSTARCH)"
goos="$(../bin/go env GOOS)"
goarch="$(../bin/go env GOARCH)"
cd ..
if [ "$goos" = "$gohostos" -a "$goarch" = "$gohostarch" ]; then
true
else
rm -f bin/go_${goos}_${goarch}_exec
mv bin/*_*/* bin
rmdir bin/*_*
rm -rf "pkg/${gohostos}_${gohostarch}" "pkg/tool/${gohostos}_${gohostarch}"
fi
rm -rf pkg/bootstrap pkg/obj .git
echo ----
echo Bootstrap toolchain for "$GOOS/$GOARCH" installed in "$(pwd)".
echo Building tbz.
cd ..
tar cf - "go-${GOOS}-${GOARCH}-bootstrap" | bzip2 -9 >"go-${GOOS}-${GOARCH}-bootstrap.tbz"
ls -l "$(pwd)/go-${GOOS}-${GOARCH}-bootstrap.tbz"
exit 0