#!/bin/sh

#Please modify your PHOTOSPHERE_ROOT and THEALE_PATH
#THEALE_PATH="/home/sherlock.chang/THEALE"
#PHOTOSPHERE_ROOT="/home/sherlock.chang/DAILEO/SN_Branch/New_Feature/Supernova_A7P_Lite"
#MST_PREFIX=/home/feng.lin/P4/THEALE/RedLion/Lunar/3rdLib/
MST_PREFIX=/vendor
export DESTDIR=`pwd`/output

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=amethyst.lite
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="$THEALE_PATH/RedLion/2.6.32.15/kernel"

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 "=================================="

if [ ! -d "$KERNEL_PATH/include" ]; then
    echo "Can not find kernel header files, please check the path!!!"
    exit 1
fi

LD_LIB_PATH="-L$SHARED_LIB_PATH -L$UTOPIA_LIB_PATH -lz -lm"
CFLAGS_SETTING="-DVISIBILITY_HIDDEN -EL -pthread -muclibc -mips32r2 -Os -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/mips/include -DMSOS_TYPE_LINUX"
CPPFLAGS_SETTING="-EL -pthread -muclibc -mips32r2 -Os"

echo $CFLAGS_SETTING | grep VISIBILITY_HIDDEN

if [ $? == 0 ];  then
    _CC_="mips-linux-gnu-gcc -EL -fvisibility=hidden"
else
    _CC_="mips-linux-gnu-gcc -EL"
fi


# **********************************************
# Reference Libs
# **********************************************
if [ "$CHIP" == "t2" ]; then
    MSTAR_LIB=" -lrt -lpthread -lapiGFX -lapiGOP -ldrvPQ -llinux -ldrvSERFLASH"
else
    MSTAR_LIB=" -lrt -lpthread -lapiGFX -lapiGOP -llinux  -ldrvVE -lapiXC -lapiPNL"
fi
HW_DECODEJPEG_LIBS=" "
HW_DECODEGIF_LIBS=" -lapiGPD"
echo "Running autoconf & automake"
autoreconf
autoconf
automake

./configure     CC="$_CC_" \
                LD="mips-linux-gnu-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" \
                --prefix=$MST_PREFIX \
                --sysconfdir=/config \
                --host=mips-linux-gnu \
                --build=i386-linux \
                --enable-jpeg \
                --enable-zlib \
                --enable-png \
                --enable-gif \
                --enable-gopc \
                --disable-vec \
                --enable-freetype \
                --disable-debug \
                --disable-profiling \
                --disable-unique \
                --with-gfxdrivers=mstargfx,mstargfx_g2 \
                --with-inputdrivers=mstarir,mstarkeypad \
                --enable-shared \
                --enable-static \
                --enable-multi \
                --with-tests=no \
                --disable-fbdev \
                --disable-x11 \
                --disable-vnc \
                --disable-sdl \
                --enable-devmem \
                --disable-pmem \
                --disable-hwdecodejpeg\
                --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=../../

OUTPUT_DIR="${DESTDIR}/${MST_PREFIX}"
echo $OUTPUT_DIR
echo $MST_PREFIX

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
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/libdirect-1.4.so.0.2.0
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/libdirectfb-1.4.so.0.2.0
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/libfusion-1.4.so.0.2.0
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/gfxdrivers/libdirectfb_mstar_g2.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/gfxdrivers/libdirectfb_mstar.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/inputdrivers/libdirectfb_mstar_linux_input.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/inputdrivers/libdirectfb_mstar_loopback_input.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/inputdrivers/libdirectfb_mstarir.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/inputdrivers/libdirectfb_mstarkeypad.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/wm/libdirectfbwm_default.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/systems/libdirectfb_devmem.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/systems/libdirectfb_pmem.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBFont/libidirectfbfont_default.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBFont/libidirectfbfont_dgiff.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBFont/libidirectfbfont_ft2.so      
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBFont/libidirectfbfont_mstarbmp.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBImageProvider/libidirectfbimageprovider_dfiff.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBImageProvider/libidirectfbimageprovider_gif.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBImageProvider/libidirectfbimageprovider_jpeg.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBImageProvider/libidirectfbimageprovider_gopc.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBImageProvider/libidirectfbimageprovider_mif.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBImageProvider/libidirectfbimageprovider_png.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBImageProvider/libidirectfbimageprovider_vec.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBVideoProvider/libidirectfbvideoprovider_gif.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBVideoProvider/libidirectfbvideoprovider_gopc.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBVideoProvider/libidirectfbvideoprovider_v4l.so
    mips-linux-gnu-objcopy --add-section .mmodule_version=$RETURN_PATH/version_info $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBVideoProvider/libidirectfbvideoprovider_vec.so
    rm -rf $OUTPUT_DIR/lib/*.a
    rm -rf $OUTPUT_DIR/lib/*.la
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/gfxdrivers/*.a
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/gfxdrivers/*.la
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/gfxdrivers/*.o
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/inputdrivers/*.a
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/inputdrivers/*.la
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/inputdrivers/*.o
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/systems/*.a
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/systems/*.la
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/systems/*.o
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/wm/*.a
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/wm/*.la
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/wm/*.o
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBFont/*.a
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBFont/*.la
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBFont/*.o
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBImageProvider/*.a
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBImageProvider/*.la
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBImageProvider/*.o
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBVideoProvider/*.a
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBVideoProvider/*.la
    rm -rf $OUTPUT_DIR/lib/directfb-1.4-0/interfaces/IDirectFBVideoProvider/*.o
    cp -vrfP $OUTPUT_DIR/directfb_examples $PACKAGE_NAME
    cp -vrfP $OUTPUT_DIR/bin/dfb* $PACKAGE_NAME/bin
    cp -vrfP $OUTPUT_DIR/bin/direct* $PACKAGE_NAME/bin
    cp -vrfP $OUTPUT_DIR/bin/directfb* $PACKAGE_NAME/bin
    cp -vrfP $OUTPUT_DIR/bin/fusion* $PACKAGE_NAME/bin
    cp -vrfP $OUTPUT_DIR/bin/mkdfiff $PACKAGE_NAME/bin
    cp -vrfP $OUTPUT_DIR/bin/mkdgiff $PACKAGE_NAME/bin
    cp -vrfP $OUTPUT_DIR/include/directfb $PACKAGE_NAME/include
    cp -vrfP $OUTPUT_DIR/include/directfb-internal $PACKAGE_NAME/include
    cp -vrfP $OUTPUT_DIR/lib/directfb-1.4-0 $PACKAGE_NAME/lib
    cp -vrfP $OUTPUT_DIR/lib/libdirect* $PACKAGE_NAME/lib
    cp -vrfP $OUTPUT_DIR/lib/libfusion* $PACKAGE_NAME/lib
    cp -vrfP $OUTPUT_DIR/lib/pkgconfig/direct* $PACKAGE_NAME/lib/pkgconfig
    cp -vrfP $OUTPUT_DIR/lib/pkgconfig/fusion* $PACKAGE_NAME/lib/pkgconfig
    cp -vrfP $OUTPUT_DIR/share/directfb-* $PACKAGE_NAME/share
    cp -vrfP $OUTPUT_DIR/share/man/man1/dfb* $PACKAGE_NAME/share/man/man1
    cp -vrfP $OUTPUT_DIR/share/man/man1/directfb* $PACKAGE_NAME/share/man/man1
    cp -vrfP $OUTPUT_DIR/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
