#!/bin/sh

#Please modify your PHOTOSPHERE_ROOT and THEALE_PATH
THEALE_PATH="/users/luke.hsieh/swork/PERFORCE/THEALE"
PHOTOSPHERE_ROOT="/users/luke.hsieh/swork/Sentor_167727"
MST_PREFIX=/directfb-1.4.2m
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=nugget


SHARED_LIB_PATH="$PHOTOSPHERE_ROOT/aurora_trunk_dtv/mb97/environment/release/vendor/lib"
SHARED_LIB_PATH_DFB=`pwd`/dfb_need/lib
DFB_NEEDED_PATH=`pwd`/dfb_need/include
UTOPIA_LIB_PATH="$PHOTOSPHERE_ROOT/ice_product_mstar_arm/mstar/bsp/D7L/so_lib/mips_hard_el"
UTOPIA_INCLUDE_PATH="$PHOTOSPHERE_ROOT/ice_product_mstar_arm/mstar/bsp/D7L/include"
KERNEL_PATH="$THEALE_PATH/RedLion/3.1.10"

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$SHARED_LIB_PATH_DFB -L$UTOPIA_LIB_PATH -lz -lm"
CFLAGS_SETTING="-EL -pthread -mhard-float -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 -DMSOS_TYPE_LINUX  -DPAGESIZE_LOG=12"
CPPFLAGS_SETTING="-EL -pthread -mhard-float"

echo $CFLAGS_SETTING 

if [ $? == 0 ];  then
    _CC_="mips-linux-gnu-gcc -EL"
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 -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="$_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 \
                --enable-vec \
                --enable-freetype \
                --disable-debug \
                --disable-profiling \
                --disable-unique \
                --with-gfxdrivers=mstargfx_g2 \
                --with-inputdrivers=mstarlinuxinput,mstarir,mstarkeypad,mstar_networkir,mstarloopbackinput \
                --enable-shared \
                --enable-static \
                --enable-multi \
                --with-tests=yes \
                --disable-fbdev \
                --disable-x11 \
                --disable-vnc \
                --disable-sdl \
                --enable-devmem \
                --disable-pmem \
                --enable-hwdecodejpeg \
                --disable-hwdecodepng \
                --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
    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
}

function package_for_vestel()
{
chmod -R 777 $OUTPUT_DIR
cd $OUTPUT_DIR
find . -iname *.la -exec rm "{}" \;
find . -iname *.a -exec rm "{}" \;
find . -iname *.o -exec rm "{}" \;
cd ../
tar cvfz directfb-1.4.2m_D7L.tar.gz directfb-1.4.2m
if [ $? -ne 0 ]
then
  echo "tar error..."
  cd -
  exit -1
fi
chmod 777 $OUTPUT_DIR/directfb-1.4.2m_D7L.tar.gz
echo "directfb-1.4.2m_D7L.tar.gz is ready at " $OUTPUT_DIR
ls -lah $OUTPUT_DIR
cd -
}


case $1 in
"package")
        package_for_vestel
	exit
	;;
esac


echo "################  building $LIBRARY_NAME"
build $1
if [ $? -ne 0 ]
then
  echo "configure error!!!"
  exit -1
fi
   
sed -i 's/$echo/$ECHO/g' libtool
make clean
make all
if [ $? -ne 0 ] 
then
  echo "build error!!!"
  exit -1
fi
 
echo "Deleting everything under " $OUTPUT_DIR " in 10sec"
sleep 10
rm -rf $OUTPUT_DIR/*
mkdir -p $OUTPUT_DIR/bin
mkdir -p $OUTPUT_DIR/include
mkdir -p $OUTPUT_DIR/lib
mkdir -p $OUTPUT_DIR/share
chmod -R 777 $OUTPUT_DIR/*
make install
if [ $? -ne 0 ] 
then
  echo "install error!!!"
  exit -1
fi
    
package_for_vestel

