#!/bin/sh

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


PHOTOSPHERE_ROOT="$HOME/PERFORCE/DAILEO/Supernova"
test -z $PHOTOSPHERE_ROOT && echo "the PHOTOSPHERE_ROOT must be set!!" && exit 0

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="$THEALE_PATH/RedLion/3.0.20"

DFB_PATH="/vendor/include"
PREFIX_PATH="/vendor"
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"
CFLAGS_SETTING=" -DMSOS_PROCESS_SAFT_MUTEX -mlittle-endian  -march=armv7-a -mfpu=vfpv3  -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$PHOTOSPHERE_ROOT/projects/dfbinfo/inc -DMSOS_TYPE_LINUX"
CPPFLAGS_SETTING=" -DMSOS_PROCESS_SAFT_MUTEX -mlittle-endian  -march=armv7-a -mfpu=vfpv3  -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 "
DIRECTFB_CFLAGS_SETTING="-D_REENTRANT -I$DFB_PATH/include -I$DFB_PATH/directfb -I$DFB_PATH/directfb-internal"
DIRECTFB_LIBS_SETTING="-L$SHARED_LIB_PATH/lib -ldirect -lfusion -ldirectfb -lpthread "

# **********************************************
# 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 -ldirect"
fi
HW_DECODEJPEG_LIBS=" -lapiVDEC -ldrvAUDSP -lapiAUDIO -ldrvIPAUTH -lapiJPEG "
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 " \
                LIBS="$MSTAR_LIB $HW_DECODEJPEG_LIBS" \
                DIRECTFB_CFLAGS="$DIRECTFB_CFLAGS_SETTING"              \
                DIRECTFB_LIBS="$DIRECTFB_LIBS_SETTING"                  \
                --prefix=$PREFIX_PATH/directfb_examples                 \
                --host=arm-none-linux-gnueabi \
                --build=i386-linux \
                --enable-debug=no
./fix_prefix.sh
