#!/bin/bash -e

######################################################
# FFmpeg builds script for iOS
# Property of Labgency
######################################################

cd src

DO_CONFIGURE=true
DO_CLEAN=true
DO_INSTALL=false  # todo: remove install code once everything is set up

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
{
    # --disable-doc : required because of strange bug of toolchain.

    export PATH=$PATH:.
    export CURRENTPATH=$(pwd)

    export MAX_VERSION="8.4" 
    export DEVROOT=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
    export SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${MAX_VERSION}.sdk
    export CC=$DEVROOT/usr/bin/clang
    export LD=$DEVROOT/usr/bin/ld
    export CPP="$DEVROOT/usr/bin/clang -E"
    export CXX=$DEVROOT/usr/bin/clang++
    export AR=$DEVROOT/usr/bin/ar
    export AS=$DEVROOT/usr/bin/as 
    export LIBTOOL=$DEVROOT/usr/bin/libtool
    export NM=$DEVROOT/usr/bin/nm
    export CXXCPP="$DEVROOT/usr/bin/clang++ -E"
    export RANLIB=$DEVROOT/usr/bin/ranlib
    export ARCH=arm

    COMMONFLAGS="-isysroot ${SDKROOT} $CPUFLAGS -fasm"
    export CPPFLAGS="-arch $CPU"
    export LDFLAGS="${COMMONFLAGS} -L$CURRENTPATH/libs_iphone/lib"
    export CFLAGS="${COMMONFLAGS} -I$CURRENTPATH/libs_iphone/include"
    export CXXFLAGS="${COMMONFLAGS} "
    export PATH=$DEVROOT/usr/bin:$PATH

    if $DO_CONFIGURE ; then


        

#to delete symbols :
#-s -g0" \
        ./configure \
        --extra-cflags="-arch $CPU -miphoneos-version-min=${MIN_VERSION}" \
        --extra-ldflags="-arch $CPU -miphoneos-version-min=${MIN_VERSION}" \
        --enable-cross-compile \
        --arch=${FFMPEG_ARCH} \
        --target-os=darwin \
        --cc=${CC} \
        --as="gas-preprocessor.pl $CC" \
        --sysroot=${SDKROOT} \
        --enable-optimizations \
        --prefix=$PREFIX \
        --disable-doc \
        --enable-parsers \
        --disable-encoders  \
        --enable-decoders \
        --disable-muxers \
        --enable-demuxers \
        --enable-swscale  \
        --disable-ffplay \
        --disable-ffprobe \
        --disable-ffserver \
        --disable-iconv \
        --enable-network \
        --disable-bsfs \
        --enable-pic \
        --enable-filters \
        --enable-protocols  \
        $YASMPARAM \
        --disable-debug \
        --enable-asm \
        --enable-sram \
        --disable-everything \
        --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=mp3 \
        --enable-decoder=mp3float \
        --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=tls \
        --enable-protocol=https \
        --enable-protocol=crypto \
        --enable-openssl \
        --enable-nonfree \
        $ADDITIONAL_CONFIGURE_FLAG

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

        DO_CLEAN=true

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

}

function build_one_simulator
{
    # --disable-doc : required because of strange bug of toolchain.

    export PATH=$PATH:.
    export CURRENTPATH=$(pwd)

    export MAX_VERSION="8.4" #6.1"
    #export DEVROOT=/Applications/Xcode.app/Contents/Developer/Platforms/${PLATFORM}.platform/Developer
    #export SDKROOT=$DEVROOT/SDKs/${PLATFORM}${MAX_VERSION}.sdk
    export DEVROOT=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain
    export SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/${PLATFORM}.platform/Developer/SDKs/${PLATFORM}${MAX_VERSION}.sdk
    export CC=$DEVROOT/usr/bin/clang
    export LD=$DEVROOT/usr/bin/ld
    export CPP="$DEVROOT/usr/bin/clang -E"
    export CXX=$DEVROOT/usr/bin/clang++
    export AR=$DEVROOT/usr/bin/ar
    export AS=$DEVROOT/usr/bin/as 
    export LIBTOOL=$DEVROOT/usr/bin/libtool
    export NM=$DEVROOT/usr/bin/nm
    export CXXCPP="$DEVROOT/usr/bin/clang++ -E"
    export RANLIB=$DEVROOT/usr/bin/ranlib
    export ARCH=arm

    COMMONFLAGS="-isysroot ${SDKROOT} $CPUFLAGS -fasm"
    export CPPFLAGS="-arch $CPU"
    export LDFLAGS="${COMMONFLAGS} -L$CURRENTPATH/libs_iphone/lib"
    export CFLAGS="${COMMONFLAGS} -I$CURRENTPATH/libs_iphone/include"
    export CXXFLAGS="${COMMONFLAGS} "
    export PATH=$DEVROOT/usr/bin:$PATH
        
    $CC $CFLAGS -arch $CPU -dM -E -x c /dev/null


    if $DO_CONFIGURE ; then

#to delete symbols :
#-s -g0" \
        ./configure \
        --extra-cflags="-arch $CPU -miphoneos-version-min=${MIN_VERSION}" \
        --extra-ldflags="-arch $CPU -miphoneos-version-min=${MIN_VERSION}" \
        --enable-cross-compile \
        --arch=$CPU \
        --target-os=darwin \
        --cc=${CC} \
        --sysroot=${SDKROOT} \
        --enable-optimizations \
        --prefix=$PREFIX \
        --disable-doc \
        --enable-parsers \
        --disable-encoders  \
        --enable-decoders \
        --disable-muxers \
        --enable-demuxers \
        --enable-swscale  \
        --disable-ffplay \
        --disable-ffprobe \
        --disable-ffserver \
        --disable-iconv \
        --enable-network \
        --disable-bsfs \
        --enable-pic \
        --enable-filters \
        --enable-protocols  \
        $YASMPARAM \
        --disable-debug \
        --enable-asm \
        --enable-sram \
        --disable-everything \
        --enable-decoder=zlib \
        --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=mp3 \
        --enable-decoder=mp3float \
        --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=tls \
        --enable-protocol=https \
        --enable-protocol=crypto \
        --enable-openssl \
        --enable-nonfree \
        $ADDITIONAL_CONFIGURE_FLAG

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

        DO_CLEAN=true

    fi
    if $DO_CLEAN ; then
        make clean
    fi
    make -j8; 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 ..


#arm v7n
CPU=armv7
CPUFLAGS=-marm
YASMPARAM=--disable-yasm
MIN_VERSION="6.0"
PLATFORM="iPhoneOS"
FFMPEG_ARCH=arm
export GASPP_FIX_XCODE5=0
PREFIX=../builds/ios/armv7
build_one

CPU=armv7s
CPUFLAGS=-marm
YASMPARAM=--disable-yasm
MIN_VERSION="6.0"
PLATFORM="iPhoneOS"
FFMPEG_ARCH=arm
export GASPP_FIX_XCODE5=0
PREFIX=../builds/ios/armv7s
#build_one

CPU=arm64
CPUFLAGS=-marm
YASMPARAM=--disable-yasm
MIN_VERSION="7.0"
PLATFORM="iPhoneOS"
export GASPP_FIX_XCODE5=1
FFMPEG_ARCH=arm64
PREFIX=../builds/ios/arm64
build_one

CPU=i386
CPUFLAGS=
ASPARAM=
YASMPARAM=
PLATFORM="iPhoneSimulator"
MIN_VERSION="6.0"
export GASPP_FIX_XCODE5=0
PREFIX=../builds/ios/i386
build_one_simulator

CPU=x86_64
CPUFLAGS=
ASPARAM=
YASMPARAM=
PLATFORM="iPhoneSimulator"
MIN_VERSION="7.0"
export GASPP_FIX_XCODE5=0
PREFIX=../builds/ios/x86_64
build_one_simulator

cd ../builds/ios
mkdir -p universal/lib universal/include
cp -r armv7/include/* universal/include/
cd armv7/lib
for file in *.a
do
cd ../..
lipo armv7/lib/$file arm64/lib/$file i386/lib/$file x86_64/lib/$file -create -output universal/lib/$file 
echo "Universal $file created."
cd -
done

