#!/bin/sh

SUPPORT_ARMV6="0"
SDKMAXVER="6.1"
SDKMINVER="3.0"
PLATFORM="iPhoneOS"
XCODE_PATH=`xcode-select --print-path`
DEVROOT=${XCODE_PATH}/Platforms/${PLATFORM}.platform/Developer
SDKROOT=${DEVROOT}/SDKs/${PLATFORM}${SDKMAXVER}.sdk
CC=${DEVROOT}/usr/bin/gcc
AR=${DEVROOT}/usr/bin/ar
LIPO=${DEVROOT}/usr/bin/lipo
AS=/usr/local/bin/gas-preprocessor.pl

FFMPEG_COMMON_ARGS="--disable-avfilter \
--disable-avdevice \
--disable-swscale \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffserver \
--disable-ffprobe \
--disable-doc \
--disable-debug \
--disable-encoders \
--disable-decoders \
--disable-muxers \
--disable-demuxers \
--disable-filters \
--disable-indevs \
--disable-outdevs \
--disable-protocols \
--disable-bsfs \
--disable-parsers \
--disable-hwaccels \
--enable-decoder=aac \
--enable-decoder=aac_latm \
--enable-decoder=atrac3 \
--enable-decoder=cook \
--enable-decoder=ra_144 \
--enable-decoder=ra_288 \
--enable-decoder=sipr \
--enable-decoder=vorbis \
--enable-decoder=wmalossless \
--enable-decoder=wmapro \
--enable-decoder=wmav1 \
--enable-decoder=wmav2 \
--enable-decoder=wmavoice \
--enable-decoder=mp1 \
--enable-decoder=mp1float \
--enable-decoder=mp2 \
--enable-decoder=mp2float \
--enable-decoder=mp3 \
--enable-decoder=mp3adu \
--enable-decoder=mp3adufloat \
--enable-decoder=mp3float \
--enable-decoder=mp3on4 \
--enable-decoder=mp3on4float \
--enable-demuxer=aac \
--enable-demuxer=asf \
--enable-demuxer=mp3 \
--enable-demuxer=ogg \
--enable-demuxer=xwma \
--enable-demuxer=rm \
--enable-demuxer=mov \
--enable-demuxer=mpegts \
--enable-demuxer=mpegtsraw \
--enable-demuxer=mpegps \
--enable-demuxer=flv \
--enable-parser=aac \
--enable-parser=aac_latm \
--enable-parser=mpegaudio \
--enable-bsf=aac_adtstoasc \
--enable-bsf=mp3_header_decompress \
--enable-shared --disable-static"

FFMPEG_LIBS="libavcodec libavformat libavutil"

# Settings for ARMv7
echo "Building ARMv7"

./configure \
--cc=$CC \
--as="${AS} ${CC}" \
--sysroot=$SDKROOT \
--target-os=darwin \
--arch=arm \
--cpu=cortex-a9 \
--extra-cflags='-arch armv7 -mfpu=neon' \
--extra-ldflags="-arch armv7 -isysroot ${SDKROOT} -miphoneos-version-min=${SDKMINVER}" \
--prefix=compiled/armv7 \
--enable-cross-compile \
--disable-armv5te \
--disable-asm \
--enable-pic \
$FFMPEG_COMMON_ARGS

make clean
make && make install

# Settings for ARMv7s
echo "Building ARMv7s"

./configure \
--cc=$CC \
--as="${AS} ${CC}" \
--sysroot=$SDKROOT \
--target-os=darwin \
--arch=arm \
--cpu=cortex-a9 \
--extra-cflags='-arch armv7s -mfpu=neon' \
--extra-ldflags="-arch armv7s -isysroot ${SDKROOT} -miphoneos-version-min=${SDKMINVER}" \
--prefix=compiled/armv7s \
--enable-cross-compile \
--disable-asm \
--enable-pic \
$FFMPEG_COMMON_ARGS

make clean
make && make install

# Settings for ARMv6
if [ $SUPPORT_ARMV6 = "1" ]
then
echo "Building ARMv6"

./configure \
--cc=$CC \
--as="${AS} ${CC}" \
--sysroot=$SDKROOT \
--target-os=darwin \
--arch=arm \
--cpu=arm1176jzf-s \
--extra-cflags='-arch armv6' \
--extra-ldflags="-arch armv6 -isysroot ${SDKROOT} -miphoneos-version-min=${SDKMINVER}" \
--prefix=compiled/armv6 \
--enable-cross-compile \
--disable-armv5te \
--disable-asm \
--enable-pic \
$FFMPEG_COMMON_ARGS

make clean
make && make install

fi

# Settings for x86
echo "Building x86"
PLATFORM="iPhoneSimulator"
DEVROOT=${XCODE_PATH}/Platforms/${PLATFORM}.platform/Developer
SDKROOT=${DEVROOT}/SDKs/${PLATFORM}${SDKMAXVER}.sdk
CC=${DEVROOT}/usr/bin/gcc

./configure \
--cc=$CC \
--as="${AS} ${CC}" \
--sysroot=$SDKROOT \
--target-os=darwin \
--arch=i386 \
--cpu=i386 \
--extra-cflags='-arch i386' \
--extra-ldflags="-arch i386 -isysroot ${SDKROOT} -miphoneos-version-min=${SDKMINVER}" \
--prefix=compiled/i386 \
--enable-cross-compile \
--disable-asm \
--disable-pic \
--disable-optimizations \
$FFMPEG_COMMON_ARGS

#--enable-debug=3 \
#--disable-optimizations \
#--disable-stripping

make clean
make && make install

# Package universal libs
mkdir -p ./compiled/fat/lib

ARCHITECTURES="i386 armv7 armv7s"

if [ $SUPPORT_ARMV6 = "1" ]; then
	ARCHITECTURES="$ARCHITECTURES armv6"
fi

for ARCH in $ARCHITECTURES
do
	for LIBNAME in $FFMPEG_LIBS
	do
		install_name_tool -id @executable_path/$LIBNAME.dylib ./$OUTPUT_PREFIX/$ARCH/lib/$LIBNAME.dylib
		install_name_tool -change $OUTPUT_PREFIX/$ARCH/lib/libavutil.dylib @executable_path/libavutil.dylib ./$OUTPUT_PREFIX/$ARCH/lib/$LIBNAME.dylib
		install_name_tool -change $OUTPUT_PREFIX/$ARCH/lib/libavcodec.dylib @executable_path/libavcodec.dylib ./$OUTPUT_PREFIX/$ARCH/lib/$LIBNAME.dylib
	done
done

for LIBNAME in $FFMPEG_LIBS
do
	LIPO_ARGS="" 
	for ARCH in $ARCHITECTURES
	do
		LIPO_ARGS="${LIPO_ARGS} -arch ${ARCH} ./compiled/${ARCH}/lib/${LIBNAME}.dylib"
	done
	
	$LIPO -output ./compiled/fat/lib/$LIBNAME.dylib  -create $LIPO_ARGS
done
