#!/bin/sh

trap exit ERR

FFMPEG_DIR=ffmpeg-armv7
SCRIPT_DIR=$( (cd -P $(dirname $0) && pwd) )

echo "Pulling r22403..."
svn co -r22403 svn://svn.ffmpeg.org/ffmpeg/trunk $FFMPEG_DIR

cd $FFMPEG_DIR
mkdir -p dist

# Default configure options
CONFIGURE_OPTIONS="--enable-gpl --enable-postproc --enable-swscale --enable-avfilter"

# Add x264 if exists
X264_DIST="$SCRIPT_DIR/x264-armv7/dist"
if [ -d "$X264_DIST" ]; then
  CONFIGURE_OPTIONS="$CONFIGURE_OPTIONS --enable-libx264 --extra-ldflags=-L$X264_DIST/lib --extra-cflags=-I$X264_DIST/include"
fi

# Add xvid if exists
XVID_DIST="$SCRIPT_DIR/xvid-armv7"
if [ -d "$XVID_DIST" ]; then
  CONFIGURE_OPTIONS="$CONFIGURE_OPTIONS --enable-libxvid --extra-ldflags=-L$XVID_DIST/lib --extra-cflags=-I$XVID_DIST/include"
fi

echo "Configure options: $CONFIGURE_OPTIONS"

./configure --cc=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2 --as="$SCRIPT_DIR/gas-preprocessor.pl /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/gcc-4.2" --sysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk --prefix="dist" --enable-cross-compile --target-os=darwin --arch=arm --cpu=cortex-a8 --extra-cflags="-arch armv7" --extra-ldflags="-arch armv7" --enable-pic $CONFIGURE_OPTIONS

make && make install

echo "Installed: $FFMPEG_DIR/dist"
