PROFAN_PATH="/home/pf4/Documents/GitHub/profanOS"

CFLAGS="-fPIC -m32 -march=i686 -ffreestanding -fno-exceptions -fno-stack-protector -nostdinc -nostdlib -D__profanOS__ -U__linux__ -I $PROFAN_PATH/include/zlibs -I $PROFAN_PATH/include/addons"
LDFLAGS="-m elf_i386 -nostdlib -T $PROFAN_PATH/tools/link_elf.ld -lc -L $PROFAN_PATH/out/zlibs $PROFAN_PATH/out/make/entry_elf.o"

EXTRAFLAGS="-Wall -Werror=implicit-function-declaration -Wno-char-subscripts -Wno-pointer-sign -funsigned-char -I . -ffunction-sections -fdata-sections -fno-asynchronous-unwind-tables -fno-strict-aliasing"

rm -Rf out

mkdir -p out/make

SUCCESS=0
FAIL=0

for file in $(ls toys/*/*.c lib/*.c main.c fuck.c); do
    log_file=out/log/${file%.c}.log
    mkdir -p $(dirname $log_file)
    gcc $CFLAGS $EXTRAFLAGS -c $file -o out/make/$(basename ${file%.c}.o) 2> $log_file
    if [ $? -ne 0 ]; then
        echo "[ KO ] $file"
        FAIL=$((FAIL+1))
    else
        echo "       $file"
        rm $log_file
        SUCCESS=$((SUCCESS+1))
    fi
done

echo "Compiled $SUCCESS files successfully, $FAIL failed."

ld $LDFLAGS out/make/*.o -o toybox.elf

ls -l toybox.elf
cp toybox.elf /home/pf4/Documents/GitHub/profanOS/out/zapps/f/
