#!/bin/sh
# Copyright 2008 Loris Vincenzi (http://www.slacky.eu)
# All rights reserved.
#
# Heavily based on the Slackware 13.0 SlackBuild
# Requirements: amrnb, amrwb, dirac, enca, faac, lame, ladspa (compilation), libs2b, libdca, libdv, libdvdnav, libmpcdec, libdvdcss
# libnut (Statically linked), live555(Statically linked),  lirc, lirccd (0.8.4), schroendinger, speex, twolame
# x264, xvidcore, yasm (compilation)
#
# http://www.MPlayerHQ.hu
#
# 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.

CWD=`pwd`
TMP=${TMP:-/tmp/txz}
PKG=$TMP/package/mplayer
NAME=mplayer
VERSION=r29390
ARCH=${ARCH:-i486}
BUILD=1sl
SKIN=Blue
SKINV=1.7


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

cd $TMP

# We are using Alien source, because a problem with current SVN (GUI crash). 
tar xJvf $CWD/MPlayer_nolibdvdcss-$VERSION.tar.xz
tar xjvf $CWD/$SKIN-$SKINV.tar.bz2
cd MPlayer-$VERSION
chmod -R u+w,go+r-w,a-s .
chown -R root:root .
./configure --prefix=/usr \
--confdir=/etc/mplayer \
--codecsdir=/usr/lib/codecs \
--win32codecsdir=/usr/lib/codecs \
--xanimcodecsdir=/usr/lib/codecs \
--realcodecsdir=/usr/lib/codecs \
--mandir=/usr/man \
--disable-ivtv \
--disable-dvdread-internal \
--enable-joystick \
--enable-largefiles \
--enable-xvmc \
--enable-xmms \
--enable-gui \
--enable-menu \
--enable-radio \
--language=all \
--enable-runtime-cpudetection \
$EXTRASWITCH

read

make || exit 1
make install DESTDIR=$PKG

# Prepare the configfile:
mkdir -p $PKG/etc/mplayer
cp etc/example.conf $PKG/etc/mplayer/mplayer.conf
cp etc/{codecs.conf,dvb-menu.conf,input.conf,menu.conf}  $PKG/etc/mplayer
( cd $PKG/etc/mplayer
for i in *.conf; do mv "$i" "`basename $i .conf`.conf.new"; done )
 


mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a AUTHORS Changelog DOCS LICENSE README $PKG/usr/doc/$NAME-$VERSION

mkdir -p $PKG/usr/share/{applications,pixmaps}
cp $CWD/mplayer.desktop $PKG/usr/share/applications
cp etc/mplayer.xpm $PKG/usr/share/pixmaps/mplayer.xpm
ln -s /usr/share/fonts/TTF/DejaVuSans.ttf $PKG/usr/share/mplayer/subfont.ttf

mkdir -p $PKG/usr/share/mplayer/skins/default
mv -f $TMP/$SKIN/* $PKG/usr/share/mplayer/skins/default

( 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/install
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/slack-desc > $PKG/usr/doc/$NAME-$VERSION/slack-desc
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild


	echo "if [ -x /usr/bin/update-mime-database ]; then
  /usr/bin/update-mime-database /usr/share/mime &> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh
	echo "if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database &> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh
	echo "if [ -x /usr/bin/gtk-update-icon-cache ]; then
  /usr/bin/gtk-update-icon-cache  -f -q /usr/share/icons/hicolor &> /dev/null
fi" >> $PKG/install/doinst.sh
	echo "" >> $PKG/install/doinst.sh

find $PKG/usr/man -type f -exec gzip -9 {} \;

cd $PKG
ADD=$(ls /var/log/packages/ | sed -ne 's/\(libdvdcss\)-\([^-]\+-[^-]\+-[^-]\+\)/\1 >= \2/p') \
requiredbuilder -y -v -s $CWD $PKG
makepkg -l y -c n $CWD/MPlayer-$VERSION-$ARCH-$BUILD.txz

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