#!/bin/sh
# Build script for tree
# A directory listing utility

set -e

echo "Building tree..."

# Enter source directory (GitHub archives extract with different name)
cd tree-2.1.1

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

# Build with appropriate flags for illumos
case "$(uname -s)" in
    SunOS|illumos)
        # illumos needs specific flags
        $MAKE CC=gcc CFLAGS="${CFLAGS} -D_XOPEN_SOURCE=600" prefix=/usr
        ;;
    *)
        $MAKE CC=gcc CFLAGS="${CFLAGS}" prefix=/usr
        ;;
esac

# Install to DESTDIR
echo "Installing to ${DESTDIR}..."
mkdir -p "${DESTDIR}/usr/bin"
mkdir -p "${DESTDIR}/usr/share/man/man1"

install -m 755 tree "${DESTDIR}/usr/bin/tree"
install -m 644 doc/tree.1 "${DESTDIR}/usr/share/man/man1/tree.1"

echo "Build complete!"
