#!/bin/sh

# Slackware build script for MPlayer

# Copyright 2006-2008  Robby Workman, Northport, AL, USA
# All rights reserved.
#
# 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.

# Thanks to Niki Kovaks for some suggestions on this 
# Thanks to Eric Hameleers for suggestions (and some code examples) :)

PRGNAM=MPlayer
VERSION=1.0rc2
ARCH=${ARCH:-custom}
BUILD=${BUILD:-3}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

PKGVERSION=$VERSION	# This may change later in the script

# If you installed the codecs package according to the instructions at
# the MPlayer website, you'll probably want to change this variable
# to /usr/local/lib/codecs.  Otherwise, you'll want to leave it alone
# (if you installed the codecs using our SlackBuild script for them)
# unless you placed the codecs in a non-standard location.
CODECSDIR=${CODECSDIR:-/usr/lib/codecs}

# The default MPlayer theme is Blue
# http://www.mplayerhq.hu/MPlayer/skins/Blue-1.7.tar.bz2
# Others may work fine here, but the script has not been tested with them
# Even so, it's probably better to leave this alone and install additional
# themes to $HOME/.mplayer/skins/
THEME=Blue
THEME_VERSION=1.7

# The fonts listed below should exist on a stock Slackware 11.0 system.
# If you want to change the default font after installation, that is probably 
# best done by making a link to your preferred font in $HOME/.mplayer/ by:
#   cd $HOME/.mplayer
#   ln -s /path/to/preferred/font subfont.ttf 
OSDFONTS="Arialuni.ttf arial.ttf DejaVuSans.ttf Vera.ttf"

# This will build documentation for all languages - if you don't want some
# of the, either change this or pass what you do want on the command line
# when running the script:  LANGUAGES=en ./MPlayer.SlackBuild 
LANGUAGES=${LANGUAGES:-"en nl fr de es"}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.bz2
cd $PRGNAM-$VERSION
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

# Apply some *almost* official patches.  To clarify, the actual work done
# by the patch is identical to the official patches released by upstream.
# I just reworked them to apply from the toplevel source directory with -p1
#   --rworkman
# Fix buffer overflow in url.c
patch -p1 < $CWD/url_fix_20080120.diff
# Fix buffer overflow in stream_cddb.c
patch -p1 < $CWD/stream_cddb_fix_20080120.diff
# Fix stack overflow in demux_audio.c
patch -p1 < $CWD/demux_audio_fix_20080129.diff
# Fix buffer overflow in demux_mov.c
patch -p1 < $CWD/demux_mov_fix_20080129.diff 

# Patch to account for linux-2.6.24.x including ivtv, but MPlayer won't
# build against it.  See this link for more information and patch origin:
# http://archives.free.net.ph/message/20080201.201454.1047775e.en.html
patch -p1 < $CWD/ivtv_fix-2.6.24.x.diff 

# The MPlayer developers will not support and/or deal with bug reports
# if you don't allow it to automatically determine compiler flags,
# so modify this at your own risk
CFLAGS= \
./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --confdir=/etc/mplayer \
  --enable-color-console \
  --enable-gui \
  --enable-menu \
  --enable-runtime-cpudetection \
  --enable-largefiles \
  --codecsdir="$CODECSDIR" \
  --language="$LANGUAGES"

make
make install DESTDIR=$PKG

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

( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $( find . -type l ) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

mkdir -p $PKG/usr/share/mplayer/skins
if [ -r $CWD/$THEME-$THEME_VERSION.tar.bz2 ]; then
  cd $PKG/usr/share/mplayer/skins
  tar xf $CWD/$THEME-$THEME_VERSION.tar.bz2
  ln -s $THEME default
  cd -
else
  echo A usable theme tarball does not appear to exist in $CWD
  echo \(or you didn't edit the script to reflect it, or something
  echo else is broken\)
  echo The script will continue to build a package, but some manual
  echo setup may be required after installation.
  echo
  echo Type \'exit\' to quit or any other key to continue the script.
  read _continue
  if [ "$_continue" = "exit" ]; then
      exit 1
  fi
fi

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a AUTHORS Copyright LICENSE README DOCS $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
rm -rf $PKG/usr/doc/$PRGNAM-$VERSION/DOCS/man

# Don't clobber an existing config file
cat etc/example.conf > $PKG/etc/mplayer/mplayer.conf.new

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

# We'll link to one of the standard ttf fonts defined above.
# Thanks to Eric Hameleers for this code snippet and permission to use it.
cat << EOF >> $PKG/install/doinst.sh
# Symlink a default TrueType font for OSD:
if [ ! -f usr/share/mplayer/subfont.ttf ]; then
  for font in $OSDFONTS ; do
    if [ -f usr/share/fonts/TTF/\$font ]; then
      ( cd usr/share/mplayer 
        ln -fs /usr/share/fonts/TTF/\$font subfont.ttf 
      )
      break
    fi
  done
fi
EOF

cd $PKG
chown -R root:root .	# Some files in /usr/share/mplayer are 103:99
/sbin/makepkg -l y -c n -p $OUTPUT/$PRGNAM-$PKGVERSION-$ARCH-$BUILD$TAG.tgz
