#!/bin/bash

# Mplayer.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for ARMedslack.
#
# Copyright 2006, 2007, 2008, 2009  Eric Hameleers, Eindhoven, NL
# Copyright 2009  Patrick J. Volkerding, Sebeka, MN, USA
# All rights reserved.
#
#   Permission to use, copy, modify, and distribute this software for
#   any purpose with or without fee is hereby granted, provided that
#   the above copyright notice and this permission notice appear in all
#   copies.
#
#   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
#   CONTRIBUTORS 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.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

DOCS="AUTHORS Changelog Copyright LICENSE README DOCS/HTML DOCS/tech"

DEFSKIN=${DEFSKIN:-"Blue"}  # Download more skins at the following url:
SKINVER=${SKINVER:-"1.7"}   # http://www.mplayerhq.hu/design7/dload.html

# Available languages: all 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
LANGUAGES="en nl fr de es"  # The default is to just add "en" documentation

CODECSDIR=/usr/lib${LIBDIRSUFFIX}/codecs   # Where the WIN32 codecs are expected for instance
USE_PATENTS=${USE_PATENTS:-"NO"}
# MPlayer will try to use one of the TrueType fonts present on the target
# system for it's On Screen Display (OSD) font.
# Slackware 11.0 ships with the Vera and DejaVu fonts, you may want to add
# more fonts to this list. The first font found will be used by creating a
# symbolic link "/usr/share/mplayer/subfont.ttf" to it.
# The use of bitmapped fonts is considered deprecated, but you can still use
# those if you want. Read http://www.mplayerhq.hu/DOCS/HTML/en/fonts-osd.html
# if you want to know more about OSD font configuration.
OSDFONTS="LiberationSans-Regular.ttf \
          Arialuni.ttf arial.ttf \
          DejaVuSans.ttf Vera.ttf"

# We will work with a stripped-down source tarball, not containing libdvdcss:
[ "$USE_PATENTS" != "YES" ] && EXTRA="_nolibdvdcss" || EXTRA=""

# Where do we look for sources?
SRCDIR=$CWD

SOURCE[0]="$SRCDIR/$PKGNAM${EXTRA}-${VERSION}.tar.xz"
SRCURL[0]=""

# The default skin to use (we need to add at least one)
SOURCE[1]="$SRCDIR/${DEFSKIN}-${SKINVER}.tar.bz2"
SRCURL[1]="http://www.mplayerhq.hu/MPlayer/skins/${DEFSKIN}-${SKINVER}.tar.bz2"

# Extract source:
echo "Extracting the source archive(s) for $PKGNAM..."
tar -xvf ${SOURCE[0]}
#tar -xvf $PORTCWD/sources/mplayer*xz
[ "$USE_PATENTS" != "YES" ] && rm -rf libdvdcss
chown -R root:root *
chmod -R u+w,go+r-w,a-s *
cd $PKGNAM*
#cd mplayer*

# Patch to only use pld instruction on CPUs that support it.
# ARMedslack patch by Jim Hawkins.
patch --verbose -p1 < $PORTCWD/sources/disable_arm_pld_instruction.diff || failpatch

# Determine what X we're running (the modular X returns the prefix
# in the next command, while older versions stay silent):
XPREF=$(pkg-config --variable=prefix x11) || true
[ "$XPREF" == "" ] && XPREF='/usr/X11R6'

# Remove support for patent encumbered and possibly illegal code:
if [ "$USE_PATENTS" != "YES" ]; then
  DO_PATENTED="--disable-libdvdcss-internal \
               --disable-mp3lame --disable-mp3lame-lavc \
               --disable-faac --disable-faac-lavc \
               --disable-libopencore_amrnb \
               --disable-libopencore_amrwb"

else
  DO_PATENTED=""
fi

# MPlayer wants to automatically determine compiler flags,
# so we don't provide CFLAGS:
#            --win32codecsdir=${CODECSDIR} \
#            --realcodecsdir=${CODECSDIR} \
./configure  --prefix=/usr \
            --mandir=/usr/man \
            --confdir=/etc/mplayer \
            --enable-gui \
            --enable-menu \
            --enable-largefiles \
            --disable-arts \
            --codecsdir=${CODECSDIR} \
            --language="${LANGUAGES}" \
            ${EXTRACONFIGUREOPTIONS} \
            ${DO_PATENTED} || exit 1
# So that MPlayer does not report "UNKNOWN" as it's version:
echo $VERSION > VERSION
make || exit 1
make DESTDIR=$PKG install || exit 1

# Build the html documentation (not all languages are available):
( cd DOCS/xml
  for i in $(echo $LANGUAGES | tr , ' ') ; do
    [ -d $i ] && make html-single-$i ;
  done
)

# Prepare the configfile:
mkdir -p $PKG/etc/mplayer
cp etc/example.conf $PKG/etc/mplayer/mplayer.conf.new

# Install our default skin:
cd $PKG/usr/share/mplayer/skins
tar -xvf ${SOURCE[1]}
chown -R root:root *
chmod -R u+w,go+r-w,a-s *
ln -s ${DEFSKIN} default
cd -

# Add this to the doinst.sh:
! [ -d $PKG/install ] && mkdir -p $PKG/install
cat <<EOINS >> $PKG/install/doinst.sh
# Handle the incoming configuration files:
config() {
  for infile in \$1; do
    NEW="\$infile"
    OLD="\`dirname \$NEW\`/\`basename \$NEW .new\`"
    # If there's no config file by that name, mv it over:
    if [ ! -r \$OLD ]; then
      mv \$NEW \$OLD
    elif [ "\`cat \$OLD | md5sum\`" = "\`cat \$NEW | md5sum\`" ]; then
      # toss the redundant copy
      rm \$NEW
    fi
    # Otherwise, we leave the .new copy for the admin to consider...
  done
}

# Installing a bitmap font is considered deprecated; use a TTF font instead.
# We try to link to an installed TTF font at install time.
# Configure a default TrueType font to use for the OSD :
if [ ! -f usr/share/mplayer/subfont.ttf ]; then
  for font in ${OSDFONTS}; do
    if [ -f .${XPREF}/lib${LIBDIRSUFFIX}/X11/fonts/TTF/\${font} ]; then
      ( cd usr/share/mplayer/
        ln -sf ${XPREF}/lib${LIBDIRSUFFIX}/X11/fonts/TTF/\${font} subfont.ttf
      )
      break
    fi
  done
fi
# Prepare the new configuration file
config etc/mplayer/mplayer.conf.new

if [ -x /usr/bin/update-desktop-database ]; then
  chroot . /usr/bin/update-desktop-database -q usr/share/applications
fi

if [ -e usr/share/icons/hicolor/icon-theme.cache ]; then
  if [ -x usr/bin/gtk-update-icon-cache ]; then
    chroot . /usr/bin/gtk-update-icon-cache usr/share/icons/hicolor >/dev/null 2>&1
  fi
fi

EOINS

# Add documentation:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a $DOCS $PKG/usr/doc/$PKGNAM-$VERSION || true
cp -a $SRCDIR/$(basename $0) $PKG/usr/doc/$PKGNAM-$VERSION/$PKGNAM.SlackBuild
#mv $PKG/usr/doc/$PKGNAM-$VERSION/HTML-single $PKG/usr/doc/$PKGNAM-$VERSION/html
# Save a sample of all configuration files:
for i in etc/*.conf ; do
  cp $i $PKG/usr/doc/$PKGNAM-$VERSION/$(basename $i)-sample
done
# Save a transcript of all configured options for this specific build:
if [ -n $OUTPUT/configure-$PKGNAM.log ]; then
  cat $OUTPUT/configure-$PKGNAM.log \
    | sed -n "/^Config files successfully generated/,/^'config.h' and 'config.mak' contain your configuration options./p" \
    > $PKG/usr/doc/$PKGNAM-$VERSION/$PKGNAM.configuration
fi

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
