#!/usr/bin/env bash

# Copyright 2022, FOSS-VG Developers and Contributers
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, version 3.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# version 3 along with this program.
# If not, see https://www.gnu.org/licenses/agpl-3.0.en.html

source scripts/lib.sh

# if unknown, figure out endianness
if [ -f .endianness ]; then
    echo "Endianness known:"
    cat .endianness
else
    echo "Endianness unknown."
    if [ ! -x resources/check_endianness ]; then
        echo "Building endianness test."
        $CXX_WITH_FLAGS -o resources/check_endianness resources/check_endianness.cpp
    fi
    echo "Probing endianness:"
    resources/check_endianness > .endianness
fi

# `.cpp` files in src/lib will be automatically picked up and compiled into
# dynamically linked libraries.
echo ">>> Building libs..."
create_directory bin/lib
for lib in $(find ./src/lib -name "*.cpp"); do
    COMPILE_COMMAND="$CXX_WITH_FLAGS -I ./include -fPIC -shared -o $(sed -e 's/^.\/src/.\/bin/;s/cpp$/so/' <<< $lib) $lib"
    echo $COMPILE_COMMAND
    $COMPILE_COMMAND &
    $WAIT_ANYWAY
done

wait

# Commands for every program need to be given individually because we can't
# just add all shared libraries to all programs.
# Or can we? Idk, cba to find out.

# How to build a tool: Tell the compiler where to find shared libraries and
#   which libraries to use.
# Example: shared libraries are in bin/lib => -Lbin/lib
#   one of the libraries in that directory is nbt.so => use with -l:nbt.so
# How to run a tool: specify the library path to use for the dynamic linker
#   when running a program
# Example: LD_LIBRARY_PATH=bin/lib bin/tools/dumpnbt
echo ">>> Building programs..."
create_directory bin/tools
# add compile commands to this array
COMPILE_COMMANDS=(
    "$CXX_WITH_FLAGS src/tools/dumpnbt.cpp -I./include -Lbin/lib -l:nbt.so -l:cli.so -o bin/tools/dumpnbt"
    "$CXX_WITH_FLAGS src/tools/arraydump.cpp -I./include -Lbin/lib -l:file.so -l:cli.so -o bin/tools/arraydump"
    "$CXX_WITH_FLAGS src/tools/baseconvert.cpp -I./include -Lbin/lib -l:cli.so -o bin/tools/baseconvert"
    "$CXX_WITH_FLAGS src/tools/zlibutil.cpp -I./include -Lbin/lib -l:cli.so -l:libz.so -l:file.so -o bin/tools/zlibutil"
    "$CXX_WITH_FLAGS -pthread src/tools/hexnet.cpp -I./include -Lbin/lib -l:cli.so -l:libsockpp.so -o bin/tools/hexnet"
    "$CXX_WITH_FLAGS -DFOSSVG_DEBUG src/fossvg.cpp -I./include -Lbin/lib -l:file.so -l:cli.so -lglfw -lvulkan -o bin/fossvg"
    "$CXX_WITH_FLAGS src/fossvgd.cpp -I./include -Lbin/lib -l:cli.so -o bin/fossvgd"
)
for command in ${!COMPILE_COMMANDS[@]}; do
    echo "${COMPILE_COMMANDS[command]}"
    ${COMPILE_COMMANDS[command]} &
    $WAIT_ANYWAY
done

wait

echo ">>> Compiling shaders..."
create_directory bin/shaders
for shader in $(find ./src/shaders -name "*.vsh"); do
    COMPILE_COMMAND="dependencies/shaderc/bin/glslc -o $(sed -e 's/^.\/src/.\/bin/;s/vsh$/vsh.spv/' <<< $shader) -fshader-stage=vertex $shader"
    echo $COMPILE_COMMAND
    $COMPILE_COMMAND &
    $WAIT_ANYWAY
done

for shader in $(find ./src/shaders -name "*.fsh"); do
    COMPILE_COMMAND="dependencies/shaderc/bin/glslc -o $(sed -e 's/^.\/src/.\/bin/;s/fsh$/fsh.spv/' <<< $shader) -fshader-stage=fragment $shader"
    echo $COMPILE_COMMAND
    $COMPILE_COMMAND &
    $WAIT_ANYWAY
done

wait
