#!/bin/sh
#
BUILD_DIR=`dirname $0`
# Get mermsemr_api root directory
cd ..
WORK_DIR=`pwd`
MAILSEND="$WORK_DIR/src/mailsend"
LIBCONFIG="$WORK_DIR/src/libconfig-1.3.1"
# Checking user
if [ "$USER" = "root" ]
then
    echo "Configuring as root"
    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
make
# 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/mermsemr_api/mermsemr_api_$LUSER/g | sed s/MERMSEMR_API/MERMSEMR_API_$UUSER/g > config.m4
cat ../build/php_mermsemr_api_ns.h | sed s/MERMSEMR_API/MERMSEMR_API_$UUSER/g | sed s/mermsemr_api/mermsemr_api_$LUSER/g > include/php_mermsemr_api_ns.h
cat ../build/php_mermsemr_api.h | sed s/mermsemr_api/mermsemr_api_$LUSER/g | sed s/MERMSEMR_API/MERMSEMR_API_$UUSER/g > include/php_mermsemr_api.h
cat ../build/mermsemr_api.cc | sed s/tmermsemrapit/mermsemr_api_$LUSER/g | sed s/MERMSEMR_API/MERMSEMR_API_$UUSER/g > core/mermsemr_api.cc

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


