#!/usr/bin/env bash
source global/common.sh

# TODO: provide "modular" install option to avoid the traditional
# linux directory hierarchy on systems where this doesn't make sense.

check "prefix" "a directory to install to"
build="${build:-out}"

bindir="${bindir:-$prefix/bin}"
libdir="${libdir:-$prefix/lib}"

if test "$prefix" = "/" -o "$fakeroot" = yes; then
	say "installing docs and headers into usr instad of /"
	sharedir="${sharedir:-$prefix/usr/share}"
	incdir="${incdir:-$prefix/usr/include}"
else
	incdir="${incdir:-$prefix/include}"
	sharedir="${sharedir:-$prefix/share}"
fi

say "commencing install"
say " - prefix: $prefix"
say " - destinations:"
say "   * binaries: $bindir"
say "   * libraries: $libdir"
say "   * headers: $incdir"
say "   * shared files: $sharedir"

mkdir -p "$bindir" "$libdir" "$incdir" "$sharedir"

# Libraries
# These may or may not exist, depending on what library build is done, so check
say "installing libraries"
test -e "$build/libk.a"  && install -Dm644 -t "$libdir" "$build/libk.a"
test -e "$build/libk.so" && install -Dm644 -t "$libdir" "$build/libk.so"
test -e "$build/kboot.o" && install -Dm644 -t "$libdir" "$build/kboot.o"

# Binaries
say "installing binaries"
# *.testbin files should not be installed
install -Dm755 -t "$bindir" "$build/kgraft.attach"

# Includes
say "installing headers"
for inc in "$build"/k/*; do
	install -Dm644 -t "$incdir/k" "$inc"
done

# Documentation
say "installing documentation"
for man in "$build"/doc/man/*; do
	install -Dm644 -t "$sharedir/man" "$man"
done
for html in "$build"/doc/html/*; do
	install -Dm644 -t "$sharedir/doc/libk/html" "$html"
done
for pdf in "$build"/doc/pdf/*; do
	install -Dm644 -t "$sharedir/doc/libk/pdf" "$pdf"
done
say "install complete"
