#!/usr/bin/env bash
## Copyright (C) 2017 Jeremiah Orians
## This file is part of mescc-tools.
##
## mescc-tools is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## mescc-tools is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with mescc-tools.  If not, see <http://www.gnu.org/licenses/>.

# kaem build file for bootstrapping
# Build bin folder for binaries
mkdir -p bin

# First build blood-elf
# Generate elf footer for debugging
/usr/bin/env blood-elf \
	-f ../mescc-tools-seed/blood-elf.M1 \
	-o ../mescc-tools-seed/blood-elf-footer.M1

# Assemble
/usr/bin/env M1 \
	--LittleEndian \
	--architecture x86 \
	-f ../mescc-tools-seed/x86_defs.M1 \
	-f ../mescc-tools-seed/libc-core.M1 \
	-f ../mescc-tools-seed/blood-elf.M1 \
	-f ../mescc-tools-seed/blood-elf-footer.M1 \
	-o ../mescc-tools-seed/blood-elf.hex2

# Link
/usr/bin/env hex2 \
	--LittleEndian \
	--architecture x86 \
	--BaseAddress 0x8048000 \
	-f ../mescc-tools-seed/ELF-i386-debug.hex2 \
	-f ../mescc-tools-seed/blood-elf.hex2 \
	-o bin/blood-elf \
	--exec_enable

# Secondly build hex2-linker
# use our newly generated blood-elf to create the elf footer
./bin/blood-elf \
	-f ../mescc-tools-seed/hex2.M1 \
	-o ../mescc-tools-seed/hex2-footer.M1

# Assemble
/usr/bin/env M1 \
	--LittleEndian \
	--architecture x86 \
	-f ../mescc-tools-seed/x86_defs.M1 \
	-f ../mescc-tools-seed/libc-core.M1 \
	-f ../mescc-tools-seed/hex2.M1 \
	-f ../mescc-tools-seed/hex2-footer.M1 \
	-o ../mescc-tools-seed/hex2.hex2

# Link
/usr/bin/env hex2 \
	--LittleEndian \
	--architecture x86 \
	--BaseAddress 0x8048000 \
	-f ../mescc-tools-seed/ELF-i386-debug.hex2 \
	-f ../mescc-tools-seed/hex2.hex2 \
	-o bin/hex2 \
	--exec_enable

# Finally build M1-macro
# Generate elf footer for debugging
./bin/blood-elf \
	-f ../mescc-tools-seed/M1.M1 \
	-o ../mescc-tools-seed/M1-footer.M1

# Assemble
/usr/bin/env M1 \
	--LittleEndian \
	--architecture x86 \
	-f ../mescc-tools-seed/x86_defs.M1 \
	-f ../mescc-tools-seed/libc-core.M1 \
	-f ../mescc-tools-seed/M1.M1 \
	-f ../mescc-tools-seed/M1-footer.M1 \
	-o ../mescc-tools-seed/M1.hex2

# Link
./bin/hex2 \
	--LittleEndian \
	--architecture x86 \
	--BaseAddress 0x8048000 \
	-f ../mescc-tools-seed/ELF-i386-debug.hex2 \
	-f ../mescc-tools-seed/M1.hex2 \
	-o bin/M1 \
	--exec_enable
