#!/bin/sh
# Build script for ncurses
# Terminal UI library

set -e

echo "Building ncurses..."

cd ncurses-6.4

# On illumos, use GNU make
MAKE=gmake
if [ "$(uname -s)" != "SunOS" ]; then
    MAKE=make
fi

# Configure with common options
./configure \
    --prefix=/usr \
    --with-shared \
    --with-cxx-shared \
    --without-debug \
    --without-normal \
    --enable-widec \
    --enable-pc-files \
    --with-pkg-config-libdir=/usr/lib/pkgconfig

# Build
$MAKE ${MAKEFLAGS}

# Install to DESTDIR
echo "Installing to ${DESTDIR}..."
$MAKE DESTDIR="${DESTDIR}" install

# Create compatibility symlinks for non-wide-char programs
# Many programs expect -lncurses, but we built -lncursesw
cd "${DESTDIR}/usr/lib"
for lib in ncurses form panel menu; do
    ln -sf lib${lib}w.so lib${lib}.so 2>/dev/null || true
    ln -sf ${lib}w.pc /usr/lib/pkgconfig/${lib}.pc 2>/dev/null || true
done

# Also provide curses.h symlink
cd "${DESTDIR}/usr/include"
ln -sf ncursesw/curses.h curses.h 2>/dev/null || true
ln -sf ncursesw/ncurses.h ncurses.h 2>/dev/null || true

echo "Build complete!"
