#!/bin/bash
MULTICPU="-j"
BUILDFLAGS=" -fomit-frame-pointer -DNDEBUG -static "
POSTALFLAGS=" -DLOCALE=1 -DSTART_MENU_ADDON_ITEM "
MAKERSPIX="false"

while getopts "SFRM" option;
do
    case $option in                        
        S)  echo "MultiCPU make disabled"
            MULTICPU="";;                
        F)  echo "Making softfloat"
            cd SoftFloat/build/Linux-386-GCC && make $MULTICPU libsoftfloat.a && cd ../../../
            exit;;   
        R)  echo "Building RSPiX"
            MAKERSPIX="true";;        
        M)  echo "MSTATS ENABLED"
            BUILDFLAGS+=" -DMSTATS ";;        
    esac
done

if [ $MAKERSPIX == "true" ]; then
make rspix BUILDFLAGS="${BUILDFLAGS}" POSTALFLAGS="${POSTALFLAGS}" $MULTICPU
fi
cd Postal && make BUILDFLAGS="${BUILDFLAGS}" POSTALFLAGS="${POSTALFLAGS}" $MULTICPU &&
cd ../PVR && make BUILDFLAGS="${BUILDFLAGS}" $MULTICPU &&
cd ../ && cd ./AICA && make BUILDFLAGS="${BUILDFLAGS}" POSTALFLAGS="${POSTALFLAGS}" $MULTICPU &&
cd ../ && cd ./MAPLE && make BUILDFLAGS="${BUILDFLAGS}" POSTALFLAGS="${POSTALFLAGS}" $MULTICPU &&
cd ../ && make BUILDFLAGS="${BUILDFLAGS}" POSTALFLAGS="${POSTALFLAGS}" $MULTICPU &&

sh-elf-objcopy -R .stack -O binary postal.elf output.bin && scramble output.bin 1st_read.bin && cp 1st_read.bin ./build/disc/1st_read.bin

#sh-elf-objcopy -R .stack -O binary postal.elf output.bin && scramble output.bin 1st_read.bin && cp 1st_read.bin ./build/disc/1st_read.bin &&\
#makedisc ./postal.cdi ./ ./IP.BIN "POSTAL" --audio && rundll32 user32.dll,MessageBeep;