#!/usr/bin/env fish

# Linux by Intent build environment for fish.
# Source this from a dedicated build shell instead of placing the values
# in config.fish.

set -e CC CXX CPPFLAGS CFLAGS CXXFLAGS LDFLAGS
set -e LD_LIBRARY_PATH PKG_CONFIG_PATH PKG_CONFIG_LIBDIR DESTDIR

if not set -q LBI_ROOT
    set -gx LBI_ROOT "$HOME/linux-by-intent"
end

if not set -q LBI_SOURCES
    set -gx LBI_SOURCES "$LBI_ROOT/sources"
end

if not set -q LBI_TOOLS
    set -gx LBI_TOOLS "$LBI_ROOT/tools"
end

if not set -q LBI_BOOT
    set -gx LBI_BOOT "/boot"
end

if not set -q LBI_ESP_MOUNT
    set -gx LBI_ESP_MOUNT "$LBI_ROOT/boot/efi"
end

if not set -q LBI_ARCH
    set -gx LBI_ARCH "x86_64"
end

if not set -q LBI_TARGET
    set -gx LBI_TARGET "$LBI_ARCH-lbi-linux-musl"
end

if not set -q LWI_MAKE_JOBS
    set -gx LWI_MAKE_JOBS (getconf _NPROCESSORS_ONLN 2>/dev/null; or printf '1')
end

if not set -q LBI_LOCAL_MIRROR
    set -gx LBI_LOCAL_MIRROR ""
end

if not set -q LWI_CFLAGS
    set -gx LWI_CFLAGS ""
end

if not set -q LWI_CXXFLAGS
    set -gx LWI_CXXFLAGS "$LWI_CFLAGS"
end

if not set -q LWI_MAKE_FLAGS
    set -gx LWI_MAKE_FLAGS "-j$LWI_MAKE_JOBS"
end

if not set -q LBI_CUSTOM_LDFLAGS
    set -gx LBI_CUSTOM_LDFLAGS ""
end

if not set -q LBI_BOOTLOADER_ID
    set -gx LBI_BOOTLOADER_ID "LinuxByIntent"
end

if test -d "$LBI_TOOLS/bin"
    if not contains -- "$LBI_TOOLS/bin" $PATH
        set -gx PATH "$LBI_TOOLS/bin" $PATH
    end
end

# Reusable build-system helpers.
# Any extra flags passed to these functions are forwarded as-is and can
# override defaults by appearing later on the command line.

function lbi_configure
    ./configure \
        --prefix=/system \
        --bindir=/system/binaries \
        --sbindir=/system/systembinaries \
        --libdir=/system/libraries \
        --libexecdir=/system/systembinaries \
        --includedir=/system/headers \
        --sysconfdir=/system/configuration \
        --localstatedir=/system/variable \
        --mandir=/system/documentation/man-pages \
        --infodir=/system/documentation/info \
        $argv
end

function lbi_meson
    set -l lbi_meson_builddir build
    set -l lbi_meson_args $argv

    if test (count $argv) -gt 0
        if not string match -qr '^-' -- $argv[1]
            set lbi_meson_builddir $argv[1]
            if test (count $argv) -gt 1
                set lbi_meson_args $argv[2..-1]
            else
                set lbi_meson_args
            end
        end
    end

    meson setup "$lbi_meson_builddir" \
        --prefix=/system \
        --bindir=/system/binaries \
        --sbindir=/system/systembinaries \
        --libdir=/system/libraries \
        --libexecdir=/system/systembinaries \
        --includedir=/system/headers \
        --sysconfdir=/system/configuration \
        --localstatedir=/system/variable \
        --mandir=/system/documentation/man-pages \
        --infodir=/system/documentation/info \
        $lbi_meson_args
end

function lbi_cmake
    set -l lbi_cmake_builddir build
    set -l lbi_cmake_args $argv

    if test (count $argv) -gt 0
        if not string match -qr '^-' -- $argv[1]
            set lbi_cmake_builddir $argv[1]
            if test (count $argv) -gt 1
                set lbi_cmake_args $argv[2..-1]
            else
                set lbi_cmake_args
            end
        end
    end

    cmake -S . -B "$lbi_cmake_builddir" \
        -DCMAKE_INSTALL_PREFIX=/system \
        -DCMAKE_INSTALL_BINDIR=/system/binaries \
        -DCMAKE_INSTALL_SBINDIR=/system/systembinaries \
        -DCMAKE_INSTALL_LIBDIR=/system/libraries \
        -DCMAKE_INSTALL_LIBEXECDIR=/system/systembinaries \
        -DCMAKE_INSTALL_INCLUDEDIR=/system/headers \
        -DCMAKE_INSTALL_SYSCONFDIR=/system/configuration \
        -DCMAKE_INSTALL_LOCALSTATEDIR=/system/variable \
        -DCMAKE_INSTALL_MANDIR=/system/documentation/man-pages \
        -DCMAKE_INSTALL_INFODIR=/system/documentation/info \
        $lbi_cmake_args
end

umask 022
