#!/bin/sh
# Build script for conky (CMake, Ninja generator). RISKY per task brief:
# conky's native stats backend is Linux/BSD/Solaris-specific and illumos is
# not an upstream-supported OS name (it only recognizes "SunOS").
#
# GOOD NEWS: conky already ships a real Solaris/illumos kstat-based stats
# backend (src/data/os/solaris.cc, gated on OS_SOLARIS, which is set from
# `CMAKE_SYSTEM_NAME MATCHES "SunOS"`). Hammerhead's uname -s is
# "Hammerhead", not "SunOS", so that gate never fires and CMake instead
# hits ConkyPlatformChecks.cmake's final `if(NOT OS_LINUX AND NOT ...)
# message(FATAL_ERROR "platform not supported")`. Patch the OS_SOLARIS
# gate to also match "Hammerhead" (see patches/001-illumos-os-detect.patch
# for the readable diff) so the REAL native kstat backend builds - the
# M3 config's ${exec}-based widgets don't strictly need this, but it's a
# strictly better outcome than a stub, and Hammerhead does ship libkstat.
set -e
cd "$SRCDIR"

sed -i \
    -e 's/if(CMAKE_SYSTEM_NAME MATCHES "SunOS")/if(CMAKE_SYSTEM_NAME MATCHES "SunOS" OR CMAKE_SYSTEM_NAME MATCHES "Hammerhead")/' \
    -e 's/else(CMAKE_SYSTEM_NAME MATCHES "SunOS")/else(CMAKE_SYSTEM_NAME MATCHES "SunOS" OR CMAKE_SYSTEM_NAME MATCHES "Hammerhead")/' \
    -e 's/endif(CMAKE_SYSTEM_NAME MATCHES "SunOS")/endif(CMAKE_SYSTEM_NAME MATCHES "SunOS" OR CMAKE_SYSTEM_NAME MATCHES "Hammerhead")/' \
    cmake/ConkyPlatformChecks.cmake

# solaris.cc is missing get_battery_power_draw() - every OTHER platform
# backend (linux.cc, freebsd.cc, netbsd.cc, haiku.cc, darwin.mm) implements
# it, but upstream conky's Solaris/illumos backend never got battery-draw
# support (real Solaris/illumos servers rarely have batteries). common.cc
# calls it unconditionally regardless of platform -> link failure
# ("undefined reference to get_battery_power_draw"). Add the same
# "not implemented" stub every other unsupported-metric function in this
# file already uses (get_battery_short_status, get_acpi_fan, ...).
cat >> src/data/os/solaris.cc <<'EOF'

void get_battery_power_draw(char *buffer, unsigned int n, const char *bat) {
  /* Not implemented - illumos/Solaris kstat has no standard battery
   * power-draw metric; real servers rarely have batteries. */
  (void)bat;
  if (buffer && n > 0) memset(buffer, 0, n);
}
EOF

# Same Platform-module gotcha as base/tint2 and base/libjpeg-turbo: CMake
# has no Platform/Hammerhead.cmake, so map it onto the (real) SunOS module
# for shared-lib/UNIX conventions.
mkdir -p "$SRCDIR/.cmake/Platform"
echo 'include(Platform/SunOS)' > "$SRCDIR/.cmake/Platform/Hammerhead.cmake"

export PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PREFIX/share/pkgconfig:/usr/lib/pkgconfig:$PKG_CONFIG_PATH"

# -DRELEASE=ON: cmake/Conky.cmake only shells out to `git rev-parse` (for a
# -pre-<sha> dev version string) when RELEASE is unset; the tarball has no
# .git and Hammerhead has no git binary yet, so this is a hard
# `message(FATAL_ERROR "Unable to find program 'git'")` without it.
cmake -B build -G Ninja \
    -DCMAKE_MODULE_PATH="$SRCDIR/.cmake" \
    -DCMAKE_PREFIX_PATH="$PREFIX" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="$PREFIX" \
    -DCMAKE_INSTALL_RPATH="$PREFIX/lib" \
    -DCMAKE_C_FLAGS="-D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS -fpermissive -fcommon" \
    -DCMAKE_CXX_FLAGS="-D__EXTENSIONS__ -D_POSIX_PTHREAD_SEMANTICS" \
    -DBUILD_TESTING=OFF \
    -DBUILD_X11=ON \
    -DBUILD_LUA_CAIRO=ON \
    -DBUILD_WLAN=OFF \
    -DBUILD_CURL=OFF \
    -DBUILD_IRC=OFF \
    -DBUILD_HTTP=OFF \
    -DBUILD_ICAL=OFF \
    -DBUILD_NCURSES=OFF \
    -DBUILD_DOCS=OFF \
    -DBUILD_I18N=OFF \
    -DRELEASE=ON

ninja -C build -j${JOBS:-1}
DESTDIR="$PKGDIR" ninja -C build install
