#!/bin/sh
#
######################################################
# Build script                                       #
#                                                    #
# See .info for details                              #
######################################################

######################################################
# Configure extension creation parameters            #
######################################################

SRCNAM=ffmpeg-3.1.3.tar.bz2
WRKDIR=ffmpeg-3.1.3
EXTNAM=ffmpeg
TMPDIR=/tmp/ffmpeg

######################################################
# Prepare extension creation                         #
######################################################

# Remove dirs and files left from previous creation

rm -r -f $WRKDIR

rm -r -f $TMPDIR
rm -r -f $TMPDIR-doc
rm -r -f $TMPDIR-locale

# Crete temporary directory

mkdir -p $TMPDIR

######################################################
# Compile extension                                  #
######################################################

# Export variables needed for compilation

export CFLAGS="-O2 -pipe -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"
export CXXFLAGS="-O2 -pipe -fno-exceptions -fno-rtti -march=armv6zk -mtune=arm1176jzf-s -mfpu=vfp"

# Unpack source in current directory

tar -xf $SRCNAM

# Configure it

cd $WRKDIR

rm -f configure 
cp ../configure .

./configure \
--prefix=/usr/local \
--enable-gpl \
--enable-version3 \
--enable-nonfree \
--disable-static \
--enable-shared \
--enable-libass \
--enable-libcelt \
--enable-libfaac \
--enable-libfontconfig \
--enable-libfreetype \
--enable-libfribidi \
--enable-libgsm \
--enable-libmp3lame \
--enable-libopencore-amrnb \
--enable-libopencore-amrwb \
--enable-libopenh264 \
--enable-libopenjpeg \
--enable-libopus \
--enable-libspeex \
--enable-libtwolame \
--enable-libvorbis \
--enable-libvpx \
--enable-libwavpack \
--enable-libwebp \
--enable-libx264 \
--enable-libx265 \
--enable-libxvid \
--enable-opengl \
--enable-openssl \
--disable-neon



# Compile

make -j4

exit


# Install in base temp dir

make install DESTDIR=$TMPDIR

# Delete compilation work directory

cd ..
rm -r -f $WRKDIR

# Adjust directory access rigths

find $TMPDIR/ -type d | xargs chmod -v 755;

# Strip executables

find $TMPDIR | xargs file | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded

# Move files to doc extension

mkdir -p $TMPDIR-doc/usr/local/share
mv $TMPDIR/usr/local/share/man $TMPDIR-doc/usr/local/share
mv $TMPDIR/usr/local/share/info $TMPDIR-doc/usr/local/share

# Move files to locale extension

mkdir -p $TMPDIR-locale/usr/local
mv $TMPDIR/usr/local/share $TMPDIR-locale/usr/local

###################################################
# Create base extension in temp dir               #
###################################################

cd $TMPDIR
cd ..
mksquashfs $TMPDIR $EXTNAM.tcz
cd $TMPDIR
find usr -not -type d > $EXTNAM.tcz.list
mv ../$EXTNAM.tcz .

# Create md5 file

md5sum $EXTNAM.tcz > $EXTNAM.tcz.md5.txt

# Cleanup temp directory

rm -r -f usr

###################################################
# Create doc extension in temp dir                #
###################################################

cd $TMPDIR-doc
cd ..
mksquashfs $TMPDIR-doc $EXTNAM-doc.tcz
cd $TMPDIR-doc
find usr -not -type d > $EXTNAM-doc.tcz.list
mv ../$EXTNAM-doc.tcz .

# Create md5 file

md5sum $EXTNAM-doc.tcz > $EXTNAM-doc.tcz.md5.txt

# Cleanup temp directory

rm -r -f usr

###################################################
# Create locale extension in temp dir             #
###################################################

cd $TMPDIR-locale
cd ..
mksquashfs $TMPDIR-locale $EXTNAM-locale.tcz
cd $TMPDIR-locale
find usr -not -type d > $EXTNAM-locale.tcz.list
mv ../$EXTNAM-locale.tcz .

# Create md5 file

md5sum $EXTNAM-locale.tcz > $EXTNAM-locale.tcz.md5.txt

# Cleanup temp directory

rm -r -f usr

