#!/bin/zsh
set -e
cd "$SRCDIR"

./configure \
    --prefix=$PREFIX \
    --build=$BUILD_TRIPLE \
    --host=$BUILD_TRIPLE \
    --with-shared \
    --with-cxx-shared \
    --without-debug \
    --without-ada \
    --enable-widec \
    --enable-pc-files \
    --with-pkg-config-libdir=$PREFIX/lib/pkgconfig \
    --with-termlib \
    --with-ticlib

gmake -j${JOBS:-1}
gmake install DESTDIR="$PKGDIR"

# Create non-wide symlinks (libncurses.so -> libncursesw.so etc.)
# Many programs look for -lncurses not -lncursesw
cd "$PKGDIR$PREFIX/lib"
for lib in ncurses form panel menu tinfo tic; do
    if [ -f "lib${lib}w.so" ]; then
        ln -sf "lib${lib}w.so" "lib${lib}.so"
    fi
    # Also link the .a files
    if [ -f "lib${lib}w.a" ]; then
        ln -sf "lib${lib}w.a" "lib${lib}.a"
    fi
done

# Symlink ncursesw headers as ncurses headers
cd "$PKGDIR$PREFIX/include"
if [ -d ncursesw ]; then
    ln -sf ncursesw ncurses
    # Also link headers directly for programs that use -I$PREFIX/include
    for h in ncursesw/*.h; do
        ln -sf "$h" .
    done
fi
