#!/bin/sh

#Please modify your PHOTOSPHERE_ROOT and THEALE_PATH
#THEALE_PATH="/home/kunic.chen/DFB/THEALE"
#PHOTOSPHERE_ROOT="/home/kunic.chen/DFB/DAILEO/Supernova"
MST_PREFIX=/vendor

function build()
{
test -z $THEALE_PATH && echo "the THEALE_PATH must be set!!" && exit 0
test -z $PHOTOSPHERE_ROOT && echo "the PHOTOSPHERE_ROOT must be set!!" && exit 0

CHIP=a3
if [ "$1" != "" ]; then
    CHIP=$1
fi

SHARED_LIB_PATH="$PHOTOSPHERE_ROOT/target/europe_dtv.$CHIP/mslib"
DFB_NEEDED_PATH="$PHOTOSPHERE_ROOT/develop/include"
UTOPIA_LIB_PATH="$PHOTOSPHERE_ROOT/target/europe_dtv.$CHIP/mslib/utopia/"
UTOPIA_INCLUDE_PATH="$PHOTOSPHERE_ROOT/develop/include/utopia/$CHIP"
#KERNEL_PATH="$KERNEL_SRC"
KERNEL_PATH="$THEALE_PATH/RedLion/2.6.35.8"

echo "======= DFB needed library ======="
echo $PHOTOSPHERE_ROOT
echo $SHARED_LIB_PATH
echo $DFB_NEEDED_PATH
echo $UTOPIA_LIB_PATH
echo $UTOPIA_INCLUDE_PATH
echo $KERNEL_PATH
echo "=================================="

LD_LIB_PATH="-L$SHARED_LIB_PATH -L$UTOPIA_LIB_PATH -lz -lm -fPIC -shared"
CFLAGS_SETTING=" -DMSOS_PROCESS_SAFT_MUTEX -gdwarf-2 -fPIC -w -mlittle-endian  -march=armv7-a -mcpu=cortex-a9 -mfpu=neon  -mfloat-abi=softfp -Wall -Wpointer-arith -Wstrict-prototypes -Winline -Wundef -fno-strict-aliasing -fno-optimize-sibling-calls -fno-exceptions -ffunction-sections -fdata-sections -O2 -pthread -I$DFB_NEEDED_PATH/png -I$DFB_NEEDED_PATH/zlib -I$DFB_NEEDED_PATH/jpeg -I$DFB_NEEDED_PATH/freetype/freetype2 -I$DFB_NEEDED_PATH/freetype -I$UTOPIA_INCLUDE_PATH -I$KERNEL_PATH/include -I$KERNEL_PATH/usr/include -I$KERNEL_PATH/drivers/mstar/include -I$KERNEL_PATH/arch/arm/include -I$THEALE_PATH/RedLion/Lunar/directfb/1.4.2/ -DMSOS_TYPE_LINUX -DPAGESIZE_LOG=12"
CPPFLAGS_SETTING=" -DMSOS_PROCESS_SAFT_MUTEX -gdwarf-2 -fPIC -w -mlittle-endian  -march=armv7-a -mcpu=cortex-a9 -mfpu=neon  -mfloat-abi=softfp -Wall -Wpointer-arith -Wstrict-prototypes -Winline -Wundef -fno-strict-aliasing -fno-optimize-sibling-calls -fno-exceptions -ffunction-sections -fdata-sections -O2 -pthread "

# **********************************************
# Reference Libs
# **********************************************
if [ "$CHIP" == "t2" ]; then
    MSTAR_LIB=" -lrt -lpthread -lapiGFX -lapiGOP -ldrvPQ -llinux -ldrvSERFLASH"
else
    MSTAR_LIB=" -lrt -lpthread -ldrvMVOP -lapiGFX -lapiGOP -llinux  -ldrvVE -lapiXC -lapiPNL -ldrvWDT -ldrvSAR "
fi
HW_DECODEJPEG_LIBS=" -lapiVDEC -ldrvAUDSP -lapiAUDIO -ldrvIPAUTH -lapiJPEG "
HW_DECODEGIF_LIBS="-lapiGPD"

echo "Running autoconf & automake"
autoreconf
autoconf
automake

./configure     CC="arm-none-linux-gnueabi-gcc -mlittle-endian" \
                LD="arm-none-linux-gnueabi-ld -EL"\
                CFLAGS="$CFLAGS_SETTING" \
                LDFLAGS="$LD_LIB_PATH" \
                CPPFLAGS="$CPPFLAGS_SETTING" \
                FREETYPE_CFLAGS="-I$DFB_NEEDED_PATH/freetype " \
                FREETYPE_LIBS="-lfreetype -lz" \
                LIBS="$MSTAR_LIB $HW_DECODEJPEG_LIBS $HW_DECODEGIF_LIBS " \
                --prefix=$MST_PREFIX \
                --sysconfdir=/config \
                --host=arm-none-linux-gnueabi \
                --build=i386-linux \
                --enable-jpeg \
                --enable-zlib \
                --enable-png \
                --enable-gif \
                --enable-gopc \
                --enable-vec \
                --enable-freetype \
                --enable-debug \
                --disable-profiling \
                --disable-unique \
                --with-gfxdrivers=mstargfx,mstargfx_g2 \
                --with-inputdrivers=mstarlinuxinput,mstarir,mstarkeypad,mstarloopbackinput \
                --enable-shared \
                --enable-static \
                --enable-multi \
                --with-tests=yes \
                --disable-fbdev \
                --disable-x11 \
                --disable-vnc \
                --disable-sdl \
                --enable-devmem \
                --enable-pmem \
                --enable-hwdecodejpeg\
                --disable-hwdecodegif\
                --disable-OBAMA_BUILD

#sed -i 's/ECHO/echo/g' libtool
}

LIBRARY_NAME="directfb"
LIBRARY_VERSION="1.4.2m"
PACKAGE_NAME="${LIBRARY_NAME}-${LIBRARY_VERSION}"
PACKAGE_PATH=../../

function package()
{
    if [ "$1" != "" ]; then
        PACKAGE_PATH=$1
    fi
    echo "PACKAGE_PATH=$PACKAGE_PATH"

    RETURN_PATH=`pwd`
    cd $PACKAGE_PATH
    mkdir -p $PACKAGE_NAME/bin
    mkdir -p $PACKAGE_NAME/include
    mkdir -p $PACKAGE_NAME/lib/pkgconfig
    mkdir -p $PACKAGE_NAME/share/man/man1
    mkdir -p $PACKAGE_NAME/share/man/man5
    cp -vrfP $MST_PREFIX/directfb_examples $PACKAGE_NAME
    cp -vrfP $MST_PREFIX/bin/dfb* $PACKAGE_NAME/bin
    cp -vrfP $MST_PREFIX/bin/direct* $PACKAGE_NAME/bin
    cp -vrfP $MST_PREFIX/bin/directfb* $PACKAGE_NAME/bin
    cp -vrfP $MST_PREFIX/bin/fusion* $PACKAGE_NAME/bin
    cp -vrfP $MST_PREFIX/bin/mkdfiff $PACKAGE_NAME/bin
    cp -vrfP $MST_PREFIX/bin/mkdgiff $PACKAGE_NAME/bin
    cp -vrfP $MST_PREFIX/include/directfb $PACKAGE_NAME/include
    cp -vrfP $MST_PREFIX/include/directfb-internal $PACKAGE_NAME/include
    cp -vrfP $MST_PREFIX/lib/directfb-1.4-0 $PACKAGE_NAME/lib
    cp -vrfP $MST_PREFIX/lib/libdirect* $PACKAGE_NAME/lib
    cp -vrfP $MST_PREFIX/lib/libfusion* $PACKAGE_NAME/lib
    cp -vrfP $MST_PREFIX/lib/pkgconfig/direct* $PACKAGE_NAME/lib/pkgconfig
    cp -vrfP $MST_PREFIX/lib/pkgconfig/fusion* $PACKAGE_NAME/lib/pkgconfig
    cp -vrfP $MST_PREFIX/share/directfb-* $PACKAGE_NAME/share
    cp -vrfP $MST_PREFIX/share/man/man1/dfb* $PACKAGE_NAME/share/man/man1
    cp -vrfP $MST_PREFIX/share/man/man1/directfb* $PACKAGE_NAME/share/man/man1
    cp -vrfP $MST_PREFIX/share/man/man5/directfb* $PACKAGE_NAME/share/man/man5
    tar -zvcf $PACKAGE_NAME.tar.gz $PACKAGE_NAME
    rm -rf $PACKAGE_NAME
    cd $RETURN_PATH
}

case $1 in
"package")
    echo "################  pacakage $LIBRARY_NAME"
    package $2
    ;;
*)
    echo "################  building $LIBRARY_NAME"
    build $1
    ;;
esac
