#!/bin/sh -x -e

SRC=/usr/src

# if the src is an update, eg: to -current, as well as config,
# you might need to rebuild make:
cp /usr/bin/make /usr/bin/make.old
cd $SRC/usr.bin/make && make && make install
cd $SRC/usr.bin/xinstall && make && make install
# and at times in the past, /bin/sh, flex, etc

# magic build variables
export DESTDIR=/../.                    # hack from Hell
export OBJECT_FMT=a.out
export BOOTSTRAP_ELF=YESSIREE

# update .mk files for new magic
cd $SRC/share/mk && make install

# Clean any old objects
cd $SRC && make cleandir

# You may need this:
cd $SRC/gnu && make depend

# update compiler tools
cd $SRC/gnu/usr.bin/binutils && make
cd ../gas.new && make
cd ../ld.new && make
cd ../egcs && make

# (you may want to copy the old a.out tools here, just in case)
cd $SRC/gnu/usr.bin/binutils && make install
cd ../gas.new && make install
cd ../ld.new && make install
cd ../egcs && make install

# You may need this:
### cd $SRC/gnu/lib/libbfd && make

# now building ELF natively
export OBJECT_FMT=ELF

# update include files and base libraries
cd $SRC && make includes
cd $SRC/lib/csu && make && make install
cd $SRC/lib && make && make install

# build the dynamic linker (needs libc_pic.a)
cd $SRC/libexec/ld.elf_so && make && make install

# finish the build
cd $SRC/gnu/lib && make && make install
cd $SRC && make && make install
