#!/bin/bash -e

cd src

DO_CONFIGURE=true
DO_CLEAN=true

for arg; do
    case "$arg" in
        "--no-configure") DO_CONFIGURE=false
           ;;
        "--no-clean") DO_CLEAN=false
           ;;
        "--enable-logs")
            sed -i "" -e 's/static int av_log_level.*/static int av_log_level = AV_LOG_DEBUG;/g' libavutil/log.c
            ;;
        "--disable-logs")
            sed -i "" -e 's/static int av_log_level.*/static int av_log_level = AV_LOG_FATAL;/g' libavutil/log.c
            ;;
    esac
done


function build_one
{

    if $DO_CONFIGURE ; then


        EXTRA_CFLAGS="$EXTRA_CFLAGS"
        EXTRA_CXXFLAGS="$EXTRA_CXXFLAGS -Wno-multichar -fno-exceptions -fno-rtti"

        ./configure --target-os=android \
            --prefix=$PREFIX \
            --enable-cross-compile \
            --extra-libs="" \
            --arch=$ARCH \
            --cc=$CC \
            --cxx=$CXX \
            --nm=$NM \
            --strip=$STRIP \
            --extra-cflags="-fpic -DANDROID  -fasm  -fno-short-enums $EXTRA_CFLAGS $OPTIMIZE_CFLAGS" \
            --extra-ldflags="$EXTRA_LDFLAGS -lc -lm -llog -ldl" \
            --extra-cxxflags="$EXTRA_CXXFLAGS" \
            --disable-static \
            --enable-shared \
            --disable-symver \
            --enable-parsers \
            --disable-encoders  \
            --enable-decoders \
            --disable-muxers \
            --enable-demuxers \
            --enable-optimizations \
            --enable-swscale  \
            --disable-ffplay \
            --disable-ffmpeg \
            --disable-ffprobe \
            --disable-ffserver \
            --enable-network \
            --disable-bsfs \
            --disable-doc \
            --enable-filters \
            --enable-protocols  \
            --enable-asm \
            --enable-sram \
            --disable-everything \
            --disable-debug \
            --enable-decoder=aac \
            --enable-decoder=ac3 \
            --enable-decoder=eac3 \
            --enable-decoder=ass \
            --enable-decoder=movtext \
            --enable-decoder=srt \
            --enable-decoder=h264 \
            --enable-decoder=vc1 \
            --enable-decoder=wmav1 \
            --enable-decoder=wmav2 \
            --enable-decoder=wmapro \
            --enable-decoder=wmv3 \
            --enable-decoder=hevc \
            --enable-decoder=webvtt \
            --enable-demuxer=webvtt \
            --enable-demuxer=mpegts \
            --enable-demuxer=mov \
            --enable-demuxer=aac \
            --enable-demuxer=ass \
            --enable-demuxer=srt \
            --enable-demuxer=asf \
            --enable-parser=h264 \
            --enable-parser=aac \
            --enable-protocol=file \
            --enable-protocol=tcp \
            --enable-protocol=http \
            --enable-protocol=crypto \
            --enable-protocol=tls \
            --enable-protocol=https \
            --enable-openssl \
            $ADDITIONAL_CONFIGURE_FLAG

        perl -pi -e 's/HAVE_SSE2_INLINE 1/HAVE_SSE2_INLINE 0/' config.h


        DO_CLEAN=true
    fi
    rm -rf $PREFIX

    if $DO_CLEAN ; then
        make clean
    fi
    make -j8 V=1; make install
}

#cd expat-2.0.1
#export CC=$WHATINEED/bin/arm-linux-androideabi-gcc
#export CXX=$WHATINEED/bin/arm-linux-androideabi-g++
#export NM=$WHATINEED/bin/arm-linux-androideabi-nm
#./configure --prefix=`pwd`/build \
#	    --host=arm \
#	    --enable-shared=false
#make clean;
#make -j4;make install;cd ..


export CURRENTPATH=$(pwd)
export EXPAT=$CURRENTPATH/../../expat/builds/android/arm
PLATFORM=$ANDROID_NDK_ROOT/platforms/android-14/arch-arm/
ANDROID_TOOLCHAIN=$ANDROID_TOOLCHAIN_ROOT/android-arm-14
GCCPREFIX=arm-linux-androideabi
export CC=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-gcc
export LD=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-ld
export CPP=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-cpp
export CXX=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-g++
export AR=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-ar
export AS=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-as
export NM=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-nm
export CXXCPP=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-cpp
export RANLIB=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-ranlib
export STRIP=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-strip
export ARCH=arm
export EXTRA_CFLAGS="-marm"
export PATH=$ANDROID_TOOLCHAIN/$GCCPREFIX/bin:$PATH

#arm v6
CPU=armv6
OPTIMIZE_CFLAGS="-march=$CPU"   # -D__ANDROID_DEBUG__
PREFIX=../builds/android/$CPU 
ADDITIONAL_CONFIGURE_FLAG=
EXTRA_CFLAGS=-I$(pwd)/libs_android/armv6/include
EXTRA_LDFLAGS=-L$(pwd)/libs_android/armv6/lib
build_one

#arm v7 fpv3-d16
CPU=armv7-a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -march=$CPU -mtune=cortex-a8"
PREFIX=../builds/android/$CPU 
ADDITIONAL_CONFIGURE_FLAG=--enable-armv6
EXTRA_CFLAGS=-I$(pwd)/libs_android/armv7/include
EXTRA_LDFLAGS=-L$(pwd)/libs_android/armv7/lib
build_one

export CURRENTPATH=$(pwd)
export EXPAT=$CURRENTPATH/../../expat/builds/android/x86
PLATFORM=$ANDROID_NDK_ROOT/platforms/android-14/arch-x86/
ANDROID_TOOLCHAIN=$ANDROID_TOOLCHAIN_ROOT/android-x86-14
GCCPREFIX=i686-android-linux
if [ ! -f $ANDROID_TOOLCHAIN/bin/$GCCPREFIX-gcc ]; then
    GCCPREFIX=i686-linux-android
fi
export CC=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-gcc
export LD=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-ld
export CPP=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-cpp
export CXX=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-g++
export AR=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-ar
export AS=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-as
export NM=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-nm
export CXXCPP=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-cpp
export RANLIB=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-ranlib
export STRIP=$ANDROID_TOOLCHAIN/bin/$GCCPREFIX-strip
export ARCH=x86
export EXTRA_CFLAGS=""
export PATH=$ANDROID_TOOLCHAIN/$GCCPREFIX/bin:$PATH

#x86 intel
CPU=x86
OPTIMIZE_CFLAGS="-msse3"
PREFIX=../builds/android/$CPU
ADDITIONAL_CONFIGURE_FLAG=
EXTRA_CFLAGS=-I$(pwd)/libs_android/x86/include
EXTRA_LDFLAGS=-L$(pwd)/libs_android/x86/lib
build_one
