#!/bin/sh

# Slackware build script for MPlayer

# Copyright 2006-2008  Robby Workman, Northport, AL, USA
# Copyright 2007-2008  Eric Hameleers <alien@slackware.com>
# 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 Heinz Wiesinger and Niki Kovacs for suggestions and code

PRGNAM=mplayer
VERSION=${VERSION:-svn_r27865}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-2}
TAG=${TAG:-_SBo}

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

# 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}

# This will activate runtime cpu-detection. This is a good choice,
# if you build a package you want to use on different machines.
# However, this does have a performance issue, so if that affects
# you, you can turn it off here
RUNTIME_CPU=${RUNTIME_CPU:-yes}

if [ "$RUNTIME_CPU" = "yes" ]; then
    do_cpu="en"
else
    do_cpu="dis"
fi

# This will configure MPlayer with correct support for DVD Menus.
# MPlayer will normally find libdvdnav automatically if the correct version 
# is installed. However, only with this option set to "yes" (and the external
# libdvdnav installed) will you be able to navigate through the menus.
DVDNAV=${DVDNAV:-no}

if [ "$DVDNAV" = "yes" ]; then
    do_dvdnav="--disable-dvdread-internal"
else
    do_dvdnav=""
fi

# 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 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="LiberationSans-Regular.ttf DejaVuSans.ttf Arialuni.ttf arial.ttf Vera.ttf"

# Change this with something like:  LANGUAGES=cs ./MPlayer.SlackBuild 
# Choices are: bg cs de dk el en es fr hu it ja ko mk nb
#              nl pl ro ru sk sv tr uk pt_BR zh_CN zh_TW
# If you need more than one, they should be comma-separated -- e.g. "en,es,pt_BR"
LANGUAGES=${LANGUAGES:-"en"}

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 {} \;

# The MPlayer developers will not support and/or deal with bug reports
# using custom CFLAGS, and the intent here is not to build a redistributable
# package anyway, we won't :-)
./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --confdir=/etc/mplayer \
  --disable-arts \
  --enable-gui \
  --enable-menu \
  --${do_cpu}able-runtime-cpudetection \
  --codecsdir="$CODECSDIR" \
  --language="$LANGUAGES" \
  $do_dvdnav

make
make install DESTDIR=$PKG

# Now let's build the html docs
( cd DOCS/xml
  for i in $(echo $LANGUAGES | tr , ' ') ; do 
    make html-single-$i ;
  done
)

( find $PKG | xargs file | grep -e "executable" -e "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 did not 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 Changelog Copyright LICENSE README DOCS/tech \
  $PKG/usr/doc/$PRGNAM-$VERSION
# This isn't exactly ideal, but it's better than having the entire
# script fail simply because the docs wouldn't build
( cp -a DOCS/HTML-single $PKG/usr/doc/$PRGNAM-$VERSION/html 2>/dev/null )
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

# 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 
        rm -f subfont.ttf
        ln -fs /usr/share/fonts/TTF/\$font subfont.ttf 
      )
      break
    fi
  done
fi
EOF

cd $PKG
chown -R root:root .	# Just in case
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
