#!/bin/bash

# Installation prefix
PREFIX=$HOME/local/e
# Where to store the output of configure, etc.
OUTPUT=/tmp/compile_e
# Where to store the output of LLVM's static analysis
STATIC=$OUTPUT/static_efl
# Where the source code is being kept
SRCDIR=$PWD/src/trunk/
# Where to keep dbus services
DBUSPREFIX=$HOME/.local/share/dbus-1/services

# EFL packages to be compiled
EFL_PACKAGES="eina eet embryo evas ecore efreet edje emotion e_dbus expedite imlib2 ethumb PROTO/exchange e PROTO/libeweather TMP/st/elementary PROTO/eyelight PROTO/eyelight_edit PROTO/eupnp"
# Specific Python Bindings to be compiled
EFL_BINDINGS="evas ecore edje emotion e_dbus elementary"
# List of E extra modules to be compiled
EFL_MODULES="alarm bling calendar cpu diskio drawer efm_nav efm_path flame forecasts mem net news penguins places rain snow taskbar tclock wlan"
# List of binaries that need to be suid
EFL_SUID_BIN="$PREFIX/lib/enlightenment/utils/enlightenment_sys $PREFIX/lib/enlightenment/modules/cpufreq/linux-gnu-i686-ver-pre-svn-05/freqset" 

#SB="scan-build --use-cc=distcc"
SB="scan-build"

MAKEOPTIONS="-j3"

BLACK='\E[30;40m'
RED='\E[31;40m'
GREEN='\E[32;40m'
YELLOW='\E[33;40m'
BLUE='\E[34;40m'
MAGENTA='\E[35;40m'
CYAN='\E[36;40m'
WHITE='\E[37;40m'
# Colour-enabled echo
# $1 -> message to be printed
# $2 -> colour escape sequence to be used
cecho() {
    echo -ne "$2"
    echo "$1"
    tput sgr0
}  

print_begin() {
    echo -n "BUILDING $1 : "
}

print_success() {
    cecho "DONE" $GREEN
}

print_fail() {
    cecho "FAIL" $RED
}

die() {
    print_fail
    echo "ERROR: $@"
    exit 1
}

build() {
    PACKAGE=$1
    if [ "x" != "x$SB" ] ; then 
        SBOPT="-analyze-headers --experimental-checks -k -v -v -v"
        SBOUT="-o $STATIC/$PACKAGE"
    fi
    print_begin $PACKAGE
    pushd $PACKAGE > /dev/null
    mkdir -p $OUTPUT/$PACKAGE
    if [ "x" != "x$SB" ] ; then
        mkdir -p $STATIC/$PACKAGE
    fi
    if [ -f Makefile ] ; then
        make distclean &> /dev/null
    fi
    CFLAGS="-O2" $SB ./autogen.sh --prefix=$PREFIX --with-dbus-services=$DBUSPREFIX --disable-amalgamation &> $OUTPUT/$PACKAGE/output-autogen.log || die "Failed configuring $i"
    $SB $SBOUT $SBOPT make $MAKEOPTIONS  &> $OUTPUT/$PACKAGE/output-make.log || die "Failed building $i"
    make install &> $OUTPUT/$PACKAGE/output-install.log || die "Failed installing $i"
    make clean &> /dev/null
    print_success
    popd > /dev/null
}

build_efl() {
    for i in $EFL_PACKAGES ; do 
        build $i
    done
}

build_efl_extra_modules() {
    for i in $EFL_MODULES ; do
        build E-MODULES-EXTRA/$i
    done
}

build_binding() {
    print_begin python-$1
    mkdir -p $OUTPUT/python-$1
    rm -rf python-$1/build
    cd python-$1 || die "Unable to cd to python-$1"
    ./build.sh --prefix=$PREFIX --force &> $OUTPUT/python-$i/output-build.log || die "Failed building python-$1"
    cd ..
    print_success
}

build_bindings() {
    mkdir -p $PREFIX/lib/python2.6/site-packages
    pushd BINDINGS/python > /dev/null
    for i in $EFL_BINDINGS ; do
        build_binding $i
    done
    popd > /dev/null
}

fix_permissions() {
    for i in $EFL_SUID_BIN ; do
        sudo chown root:root $i
        sudo chmod +s $i
    done
}

if [ -d $STATIC ] ; then
    rm -rf $STATIC
fi

cd $SRCDIR

build_efl
build_efl_extra_modules
build_bindings
fix_permissions


