#!/bin/sh

# ----------------------------
# Supposed to be called by Makefile "install" target
# ----------------------------

if [ ! -f configure.conf ]; then
    echo "configure.conf not found. Run configure."
    exit 1
fi
. ./configure.conf

INSTALL=/usr/bin/install

# Raydium headers
echo "- Raydium headers"
$INSTALL -d "$INCLUDES/raydium"
$INSTALL -d "$INCLUDES/raydium/headers"
$INSTALL -m 644 raydium/*.h "$INCLUDES/raydium"
$INSTALL -m 644 raydium/headers/* "$INCLUDES/raydium/headers"
ln -s "index.h" "$INCLUDES/raydium/index.c" 2> /dev/null

# ODE headers
echo "- (inner) ODE headers"
$INSTALL -d "$INCLUDES/raydium/ode/include/ode"
$INSTALL -m 644 raydium/ode/include/ode/* "$INCLUDES/raydium/ode/include/ode"

# PHP headers
echo "- (inner) PHP headers"
$INSTALL -d "$INCLUDES/raydium/php"
find raydium/php -name "*.h" -exec cp --parents {} "$INCLUDES" \;

# Raydium libs
echo "- Raydium libs"
$INSTALL -d "$LIBS"
cp -P libray* "$LIBS"
ldconfig

# rayphp
echo "- RayPHP scripts"
$INSTALL -d "$SHARE/rayphp"
$INSTALL -m 644 rayphp/* "$SHARE/rayphp" 2> /dev/null

# raydium-config
echo "- raydium-config"
tmp="raydium-config"
echo "#!/bin/sh" > "$tmp"
echo "# Generated by install.sh" >> "$tmp"
cat configure.conf >> "$tmp"
file='
includes() {
echo -n "-I$INCLUDES -I$INCLUDES/raydium/php/ -I$INCLUDES/raydium/php/main/ -I$INCLUDES/raydium/php/Zend -I$INCLUDES/raydium/php/TSRM -I$INCLUDES/raydium/ode/include/ "
}
libs() {
echo -n "-L$LIBS -lraydium "
}
cflags() {
echo -n "-DFORCE_LIBRAYDIUM $EXTRA_GCC_FLAGS "
includes
}
if [ "$1" = "--libs" ]; then
    libs
    echo ""
    exit 0
fi
if [ "$1" = "--includes" ]; then
    includes
    echo ""
    exit 0
fi
if [ "$1" = "--cflags" ]; then
    cflags
    echo ""
    exit 0
fi
echo "Usage: $0 [--libs|--includes|--cflags]"
exit 1
'
echo "$file" >> "$tmp"
$INSTALL -d "$BINS"
$INSTALL -m 755 "$tmp" "$BINS"

# Shouldn't we add docs & READMEs ?
# ...

echo "done."