#/bin/sh
# Copyright 2004-2007,2009,2015-2016 BitMover, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is a script that creates a bootstrap MSYS environment that
# can be used when building bk.
#
# It is assumed to be run like this:
#    BK_USEMSYS=1 bk sh ./update_buildenv
#
# so it get executed by the MSYS shell from an existing bk installation.

# restrict path do only things of interest
PATH=/bin	# for sed
MSYSWINDIR=$(echo $WINDIR | sed 's,\(.\):\\,/\1/,')
PATH="/bin:$MSYSWINDIR/system32:$MSYSWINDIR"

# Add bk to end of PATH
if [ "$(mount | grep 'gnu\/bin')" ]
then
	BK_PATH=$(mount | tr 'A-Z' 'a-z' | sed -ne 's,\(.\):,/\1,' -e 's,\\,/,g' -e 's,/gnu/bin,,' -e 's, on /bin type.*,,p') 
else
	BK_PATH=$(mount | tr 'A-Z' 'a-z' | sed -ne 's,\(.\):,/\1,' -e 's,\\,/,g' -e 's,/gnu,,' -e 's, on /usr type.*,,p') 
fi
PATH="$PATH:$BK_PATH"

if [ -d /r/temp ]
then	BUILD=/r/build
else	BUILD=/c/build
fi
D=$BUILD/buildenv
mkdir -p $D

W32TOOLS=$BUILD/win32tools
if [ -d $W32TOOLS ]
then	(cd $W32TOOLS; bk pull)
else	bk clone bk://data.bitkeeper.com$_BUILD_PORT/win32tools $W32TOOLS
fi

# generate hash of this script
bk crypto -h - < $0 > $BUILD/HASH.want
bk get -S win32/msysver.sh
(cd win32; ./msysver.sh) >> $BUILD/HASH.want
bk prs -hnd:KEY: -r+ $W32TOOLS/ChangeSet >> $BUILD/HASH.want

# if we already have the right stuff, then exit.
cmp -s $D/HASH $BUILD/HASH.want && {
	rm -f $BUILD/HASH.want
	exit 0
}

test "$1" = "-i" && {
	echo "HASHes differ:"
	echo "HASH contains:"
	cat $D/HASH
	echo ----
	echo "HASH.want contains:"
	cat $BUILD/HASH.want
	echo ----
	bk prompt -g "Remove old buildenv?"
	test $? -eq 1 && {
		rm -f $BUILD/HASH.want
		exit 0
	}
}

echo removing old buildenv
rm -rf $D || {
	echo cannot remove $D
	exit 1
}

# build and install new msys
(cd win32; bk get -S Makefile; make MSYSBIN=$D msys)

bk gfiles -U $W32TOOLS | bk get -S -

# build mingw
echo extracting MinGW
mkdir $D/mingw
tar -C$D/mingw -xzf $W32TOOLS/binutils-2.13.90-20030111-1.tar.gz
tar -C$D/mingw -xzf $W32TOOLS/mingw-runtime-3.2.tar.gz

GCCVER=3.4.1
GCCVEREXTRA=20040711-1
echo extracting GCC
tar -C$D/mingw -xzf $W32TOOLS/gcc-core-${GCCVER}-${GCCVEREXTRA}.tar.gz
tar -C$D/mingw -xzf $W32TOOLS/gcc-g++-${GCCVER}-${GCCVEREXTRA}.tar.gz
tar -C$D/mingw -xzf $W32TOOLS/w32api-2.5.tar.gz

echo extracting bison and flex
tar -C$D/mingw -xzf $W32TOOLS/bison-1.875.tar.gz
tar -C$D/mingw -xzf $W32TOOLS/flex-2.5.4.tar.gz

echo extracting gdb
tar -C$D -xzf $W32TOOLS/gdb-5.1.1-1-mingw.tar.gz

echo extract perl
tar -C$D -xzf $W32TOOLS/perl.tar.gz

echo extract gperf
tar -C$D -xzf $W32TOOLS/gperf-3.0.1.tar.gz

echo extract groff
mkdir $D/groff
tar -C$D/groff -xzf $W32TOOLS/groff-1.19.1-alpha-bin.tar.gz

echo copying additional DLLs
cp $W32TOOLS/*.dll $D/mingw/bin

# create custom startup scripts with fixed PATH
rm -f $D/etc/profile
echo PATH=\"/bin:/mingw/bin:/mingw/libexec/gcc/mingw32/${GCCVER}:$BK_PATH:/perl/bin:/groff/bin:$MSYSWINDIR/system32:$MSYSWINDIR\" > $D/etc/profile
#echo HOME=`pwd` >> $D/etc/profile
echo "unset BK_USEMSYS" >> $D/etc/profile
echo "unset BK_OLDPATH" >> $D/etc/profile
echo "export MSYSBUILDENV=1" >> $D/etc/profile

WD=`cd $D; bk pwd -s`
echo "export GROFF_TMAC_PATH='$WD/groff/share/groff/site-tmac;$WD/groff/share/groff/1.19.1/tmac'" >> $D/etc/profile
echo "export GROFF_FONT_PATH=$WD/groff/share/groff/1.19.1/font" >> $D/etc/profile
echo "export GRAP_DEFINES=$WD/groff/share/grap/grap.defines" >> $D/etc/profile

# setup mountpoints
mkdir -p $D/build
rm -f $D/etc/fstab
if [ -d /r/temp ]
then	echo "r:/build /build" > $D/etc/fstab
else	echo "c:/build /build" > $D/etc/fstab
fi

echo PS1=\'BUILD \\W \\$ \' >> $D/etc/profile
cp $D/bin/sh.exe $D/bin/bash.exe

mv $BUILD/HASH.want $D/HASH
