#!/bin/sh
#
BUILD_DIR=`dirname $0`
# Get wrenchboard_api root directory
cd ..
WORK_DIR=`pwd`
MAILSEND="$WORK_DIR/src/mailsend"
LIBCONFIG="$WORK_DIR/src/libconfig-1.3.1"
# Checking user
if [ "$USER" = "toor" ]
then
    # This is legacy reference, we are using system 1.5 now
    echo "Configuring as root"
# WE USE SYSTEM NOW
#    LIBCONFIG_PREFIX="/usr"
#    echo "Building $LIBCONFIG ..."
#    cd $LIBCONFIG
#    #echo `pwd` $LIBCONFIG_PREFIX
#    CFLAGS=-fPIC CXXFLAGS=-fPIC ./configure --prefix=$LIBCONFIG_PREFIX
#    make
#    make install
fi
mkdir $WORK_DIR/logs
chmod a+w $WORK_DIR/logs
mkdir $WORK_DIR/src/modules
# Building mailsend
cd $MAILSEND
./configure --with-openssl=/usr
make
cp mailsend ../modules/
# Configure extension
cd $WORK_DIR/src/

# Patch files
LUSER=$(id -u -n)
UUSER=$(id -u -n | awk '{print toupper($0)}')
cat ../build/config.m4 | sed s/wrenchboard_api/wrenchboard_api_$LUSER/g | sed s/WRENCHBOARD_API/WRENCHBOARD_API_$UUSER/g > config.m4
cat ../build/php_wrenchboard_api_ns.h | sed s/WRENCHBOARD_API/WRENCHBOARD_API_$UUSER/g | sed s/wrenchboard_api/wrenchboard_api_$LUSER/g > include/php_wrenchboard_api_ns.h
cat ../build/php_wrenchboard_api.h | sed s/wrenchboard_api/wrenchboard_api_$LUSER/g | sed s/WRENCHBOARD_API/WRENCHBOARD_API_$UUSER/g > include/php_wrenchboard_api.h
cat ../build/wrenchboard_api.cc | sed s/twrenchboardapit/wrenchboard_api_$LUSER/g | sed s/WRENCHBOARD_API/WRENCHBOARD_API_$UUSER/g > core/wrenchboard_api.cc

# Generate extension
phpize
./configure --enable-wrenchboard_api_$LUSER
#_`id -u -n`
echo "done."
cd $BUILD_DIR
echo $BUILD_DIR
echo $WORK_DIR
echo $USER


