#!/bin/sh
# Copyright 2008 Loris Vincenzi (http://www.slacky.eu)
# All rights reserved.
#
# Heavily based on the Slackware 13.0 SlackBuild
# Requirements: aften, opencore-amr, faac, faad2, lame, x264, xvidcore
# http://fixounet.free.fr/avidemux/
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# Exit on most errors
set -e

CWD=`pwd`
TMP=${TMP:-/tmp/txz}
NAME=avidemux
PKG=$TMP/package/$NAME
VERSION=2.5.2
ARCH=${ARCH:-i486}
BUILD=1sl

SOURCE=http://garr.dl.sourceforge.net/sourceforge/avidemux/$NAME'_'$VERSION.tar.gz

if [ ! -e $NAME'_'$VERSION.tar.gz ]; then
wget -c $SOURCE
fi

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"; SLKLDFLAGS=""; LIBDIRSUFFIX=""; CHOST=i486
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"; SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
fi

if [ ! -d $TMP ]; then
 mkdir -p $TMP
fi
if [ ! -d $PKG ]; then
 mkdir -p $PKG
fi

cd $TMP
tar xzvf $CWD/$NAME'_'$VERSION.tar.gz
cd $NAME'_'$VERSION
chmod -R u+w,go+r-w,a-s .
chown -R root:root .

# Compiling Avidemux 2.5.x
# The main difference is that the build is split in two parts 
# The avidemux main application(s)
# The plugins

# Building main application
# Compile the package
( mkdir build \
    && cd build \
      && cmake .. \
    -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DSYSCONF_INSTALL_DIR=/etc \
    -DMAN_INSTALL_DIR=/usr/man \
    -DINCLUDE_INSTALL_DIR=/usr/include \
    -DLIB_SUFFIX:STRING=${LIBDIRSUFFIX} \
    -DLIB_INSTALL_DIR=/usr/lib${LIBDIRSUFFIX} \
    -DBUILD_SHARED_LIBS:BOOL=ON \
    && make  \
  && make install \
&& make install DESTDIR=$PKG ) 


# Building plugins

( mkdir -p plugins/build \
     &&  cd plugins/build \
        &&   cmake .. \
            -DCMAKE_C_FLAGS:STRING="$SLKCFLAGS" \
            -DCMAKE_CXX_FLAGS:STRING="$SLKCFLAGS" \
            -DLIB_SUFFIX:STRING=${LIBDIRSUFFIX} \
	    -DLIB_INSTALL_DIR=/usr/lib${LIBDIRSUFFIX} \
	    -DAVIDEMUX_INSTALL_PREFIX=/usr \
            -DAVIDEMUX_LIB_DIR=/usr/lib \
            -DAVIDEMUX_SOURCE_DIR=${TMP}/avidemux'_'${VERSION} \
            -DAVIDEMUX_CORECONFIG_DIR=${TMP}/avidemux'_'${VERSION}/build/config \
            -DMAN_INSTALL_DIR=/usr/man \
            -DCMAKE_BUILD_TYPE=Release \
            && make \
       && make install DESTDIR=$PKG )

mv $PKG/usr/local/lib/ADM_plugins $PKG/usr/lib
rm -r $PKG/usr/local


mkdir -p $PKG/usr/doc/$NAME-cli-$VERSION/SlackBuild
cp -a AUTHORS  COPYING  README TODO $PKG/usr/doc/$NAME-cli-$VERSION

rm    $PKG/usr/bin/avidemux2_gtk
rm    $PKG/usr/bin/avidemux2_qt4
rm -r $PKG/usr/bin/i18n


( cd $PKG
find -iname '*gtk*'            -exec rm -f {} \;
find -iname '*qt4*'               -exec rm -f {} \;
find -iname '*Gtk*'            -exec rm -f {} \;
find -iname '*QT4*'               -exec rm -f {} \;
find -iname '*Qt*'               -exec rm -f {} \;
)



( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "current ar archive" | cut -f 1 -d : | xargs strip -g 2> /dev/null
)

mkdir -p $PKG/usr/man/man1
cat man/avidemux.1 > $PKG/usr/man/man1/avidemux.1

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-cli-$VERSION/SlackBuild/slack-desc
cat $CWD/$NAME-cli.SlackBuild > $PKG/usr/doc/$NAME-cli-$VERSION/SlackBuild/$NAME-cli.SlackBuild


gzip -9 $PKG/usr/man/*/*


cd $PKG
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/$NAME-cli-$VERSION-$ARCH-$BUILD.txz

if [ "$1" = "--cleanup" ]; then
 rm -rf $TMP
fi
