#!/bin/sh

CFLAGS='-std=gnu99 -m64 -Og -ggdb
-pedantic -Wall -Wextra -Wshadow
-Wcast-align -Wunused -Wconversion -Wmisleading-indentation
-Wdouble-promotion -Wformat=2 -Wbad-function-cast
-Wmissing-declarations
-Wmissing-prototypes -Wnested-externs -Werror
-Wno-override-init'

LDFLAGS='-fsanitize=address'

find ./ -name '*.c' -printf '%T@ %p\n' \
        | sort -r | head -n1 | cut -d' ' -f2 |  while read file
do
        out_file="$(basename "$file" | cut -d'.' -f1)"
        echo "------------------------------------"
        echo "Compiling file '${file}' into '${out_file}'"
        extras=''
        grep -q -e 'raylib.h' "$file"\
            && extras="${extra} -I./raylib-5.5_linux_amd64/include
                                -L./raylib-5.5_linux_amd64/lib -lraylib
                                -Wl,-rpath=./raylib-5.5_linux_amd64/lib"
        set -x
        gcc $CFLAGS $LDFLAGS $extras "$file" -o "$out_file"
        set +x
        echo "------------------------------------"
done
