#!/bin/bash

set -eu

SCRIPT_PATH="$( cd "$(dirname "$0")" ; pwd -P )"
SCRIPT_PATH_W="$(cygpath -w "${SCRIPT_PATH}")"
RTL_PATH="${SCRIPT_PATH_W}\\rtl\\"

GCC=i686-w64-mingw32-gcc
CFLAGS='-Os -std=c89 -fno-asynchronous-unwind-tables -c'

NASM=nasm
NFLAGS="-DOS_WINDOWS -f win32 -i${RTL_PATH}"

buildc() {
	[[ -f "${1}.o" ]] && rm "${1}.o"
	$GCC $CFLAGS "${1}.c" -o "${1}.o"
}

buildasm() {
	[[ -f "${1}.o" ]] && rm "${1}.o"
	$NASM $NFLAGS -o "${1}.o" "${1}.asm"
}

## Targets

cd rtl

buildasm pexl_rtl_pxobject
buildasm pexl_rtl_pxstring
buildasm pexl_rtl_memory
buildc pexl_rtl_pxvector

cd ../stdlib

buildasm conio
buildc file
buildc proc
buildc string

cd ..

read -p "Press any key to continue..."
