#!/bin/sh

# Please set Environment variable: ANDROID_NDK_ROOT

GCC_VERSION=4.4.0
PREBUILT=${ANDROID_NDK_ROOT}/build/prebuilt/windows/arm-eabi-${GCC_VERSION}
PLATFORM=android-3

configure_common()
{
	ARCH=$1
	FMPU=$2
	./configure \
		${ARCH} \
		--target-os=linux --enable-cross-compile --cross-prefix=${PREBUILT}/bin/arm-eabi- \
		--extra-cflags=" -fPIC -DANDROID -std=c99 ${FMPU} " \
		--enable-static --disable-asm --disable-yasm --disable-shared --disable-encoders --sysinclude=${ANDROID_NDK_ROOT}/build/platforms/${PLATFORM}/arch-arm/usr/include \
		--extra-ldflags="-Wl,-T,${PREBUILT}/arm-eabi/lib/ldscripts/armelf.x -Wl,-rpath-link=${ANDROID_NDK_ROOT}/build/platforms/${PLATFORM}/arch-arm/usr/lib -L${ANDROID_NDK_ROOT}/build/platforms/${PLATFORM}/arch-arm/usr/lib -nostdlib ${ANDROID_NDK_ROOT}/build/platforms/${PLATFORM}/arch-arm/usr/lib/crtbegin_static.o ${ANDROID_NDK_ROOT}/build/platforms/${PLATFORM}/arch-arm/usr/lib/crtend_android.o -lc -lm -ldl ${FMPU}" 
}

config_h_fix()
{
	echo "#undef restrict" >> config.h
	echo "#define restrict __restrict__" >> config.h
	echo "#undef HAVE_LRINT" >> config.h
	echo "#define HAVE_LRINT 1" >> config.h
	echo "#undef HAVE_LRINTF" >> config.h
	echo "#define HAVE_LRINTF 1" >> config.h
	echo "#undef HAVE_ROUND" >> config.h
	echo "#define HAVE_ROUND 1" >> config.h
	echo "#undef HAVE_ROUNDF" >> config.h
	echo "#define HAVE_ROUNDF 1" >> config.h
	echo "#undef HAVE_TRUNCF" >> config.h
	echo "#define HAVE_TRUNCF 1" >> config.h
}

cd jni
rm config.h
#configure_common --arch=armv5te
configure_common --arch=arm
config_h_fix

#echo "#undef ARCH_ARM" >> config.h
#echo "#define ARCH_ARM 1" >> config.h
#echo "#undef HAVE_ARMV5TE" >> config.h
#echo "#define HAVE_ARMV5TE 1" >> config.h

echo "ARCH=arm" > config-arm.mak
echo "ARCH_ARM=yes" >> config-arm.mak
#echo "HAVE_ARMV5TE=yes" >> config-arm.mak
echo "HAVE_ARMV5TE=no" >> config-arm.mak

cd ..
mkdir -p bin/ndk/local/armeabi
cp -f ${ANDROID_NDK_ROOT}/build/platforms/${PLATFORM}/arch-arm/usr/lib/libz.so bin/ndk/local/armeabi
${ANDROID_NDK_ROOT}/ndk-build NDK_APPLICATION_MK=Application_armeabi.mk

cd jni
rm config.h
#configure_common --arch=armvfp -fmpu=neon
configure_common --arch=armvfp
#configure_common --arch=arm
config_h_fix

#echo "#undef ARCH_ARM" >> config.h
#echo "#define ARCH_ARM 1" >> config.h
#echo "#undef HAVE_ARMV5TE" >> config.h
#echo "#define HAVE_ARMV5TE 1" >> config.h
#echo "#undef HAVE_ARMV6" >> config.h
#echo "#define HAVE_ARMV6 1" >> config.h
#echo "#undef HAVE_ARMV6T2" >> config.h
#echo "#define HAVE_ARMV6T2 1" >> config.h
echo "#undef HAVE_ARMVFP" >> config.h
echo "#define HAVE_ARMVFP 1" >> config.h
echo "#undef HAVE_NEON" >> config.h
echo "#define HAVE_NEON 1" >> config.h

echo "ARCH=arm" > config-arm.mak
echo "ARCH_ARM=yes" >> config-arm.mak
#echo "HAVE_ARMV5TE=yes" >> config-arm.mak
echo "HAVE_ARMV5TE=no" >> config-arm.mak
#echo "HAVE_ARMV6=yes" >> config-arm.mak
#echo "HAVE_ARMV6T2=yes" >> config-arm.mak
echo "HAVE_ARMVFP=yes" >> config-arm.mak
echo "HAVE_NEON=yes" >> config-arm.mak

cd ..
mkdir -p bin/ndk/local/armeabi-v7a
cp -f ${ANDROID_NDK_ROOT}/build/platforms/${PLATFORM}/arch-arm/usr/lib/libz.so bin/ndk/local/armeabi-v7a
${ANDROID_NDK_ROOT}/ndk-build NDK_APPLICATION_MK=Application_armeabi-v7a.mk


# --arch=armv41 \
# --cc=${PREBUILT}/bin/arm-eabi-gcc \
# --cc=${PREBUILT}/bin/arm-eabi-gcc \
# --nm=${PREBUILT}/bin/arm-eabi-nm \
# --ld=${PREBUILT}/bin/arm-eabi-ld \

# --extra-ldflags="-Wl,-T,${PREBUILT}/arm-eabi/lib/ldscripts/armelf.x -Wl,-rpath-link=${ANDROID_NDK_ROOT}/build/platforms/${PLATFORM}/arch-arm/usr/lib -L${ANDROID_NDK_ROOT}/build/platforms/${PLATFORM}/arch-arm/usr/lib -nostdlib ${PREBUILT}/lib/gcc/arm-eabi/${GCC_VERSION}/crtbegin.o ${PREBUILT}/lib/gcc/arm-eabi/${GCC_VERSION}/crtend.o -lc -lm -ldl"

# -flang-isoc9x
#-fstd=gnu89 

# --prefix=/home/myhome/work/ffmpeg-android-bin \
