#!/bin/sh
# -----------------------------------------------------------------------------
# based on Slackware's build script

# Set initial variables:

PRGNAM=MPlayer
VERSION=${VERSION:-20120701}
BRANCH=${BRANCH:-1.1}        # leave empty if you want to build MPlayer trunk
FFMPEG=${FFMPEG:-20120514}
BUILD=${BUILD:-2}
TAG=${TAG:-}

# Show the branch version in the package name if we build from a branch:
[ -n "$BRANCH" ] && PKGVERSION=${BRANCH}_${VERSION} || PKGVERSION=${VERSION}

LANGUAGES="all"

# Automatically determine the architecture we're building on:
MARCH=$( uname -m )
if [ -z "$ARCH" ]; then
  case "$MARCH" in
    i?86)    export ARCH=i486 ;;
    armv7hl) export ARCH=$MARCH ;;
    arm*)    export ARCH=arm ;;
    # Unless $ARCH is already set, use uname -m for all other archs:
    *)       export ARCH=$MARCH ;;
  esac
fi

if [ "$ARCH" = "x86_64" ]; then
  LIBDIRSUFFIX="64"
  # --enable-runtime-cpudetection is supported only for x86, x86_64, and PPC
  EXTRACONFIGUREOPTIONS="--enable-runtime-cpudetection"
elif [ "$ARCH" = "i486" -o \
       "$ARCH" = "i586" -o \
       "$ARCH" = "i686" ]; then
  LIBDIRSUFFIX=""
  EXTRACONFIGUREOPTIONS="--enable-runtime-cpudetection"
else
  LIBDIRSUFFIX=""
  EXTRACONFIGUREOPTIONS=""
fi

# Where the WIN32 codecs are expectedfor instance
CODECSDIR=/usr/lib${LIBDIRSUFFIX}/codecs

USE_PATENTS=YES

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

# Where do we look for sources?
SRCDIR=$(cd $(dirname $0); pwd)

SOURCE[0]="$SRCDIR/${PRGNAM}${EXTRA}-${VERSION}.tar.xz"
SOURCE[1]="$SRCDIR/ffmpeg-${FFMPEG}.tar.xz"

# Place to build (TMP) package (PKG) and output (OUTPUT) the program:
TMP=${TMP:-/tmp/build}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

##
## --- with a little luck, you won't have to edit below this point --- ##
##

# Exit the script on errors:
set -e
trap 'echo "$0 FAILED at line $LINENO!" | tee $OUTPUT/error-${PRGNAM}.log' ERR
# Catch unitialized variables:
set -u
P1=${1:-1}

# Create working directories:
mkdir -p $TMP/tmp-$PRGNAM # location to build the source
rm -rf $TMP/tmp-$PRGNAM/* # remove the remnants of previous build
mkdir -p $PKG             # place for the package to be built
rm -rf $PKG/*             # erase old package's contents
mkdir -p $OUTPUT          # place for the package to be saved

# --- PACKAGE BUILDING ---

# smplayer
tar -C $TMP -xf $SRCDIR/smplayer-*
cd $TMP/smplayer-*
make -j8 PREFIX=/usr
make PREFIX=/usr install DESTDIR=$PKG
exit
cd $TMP/tmp-$PRGNAM
echo "Extracting the source archive(s) for $PRGNAM..."
tar -xvf ${SOURCE[0]}
# Extract the ffmpeg source inside the MPlayer directory:
( cd ${PRGNAM}-${VERSION} && tar -xvf ${SOURCE[1]} )

chown -R root:root *
chmod -R u+w,go+r-w,a+X-s *


cd ${PRGNAM}-${VERSION}

# Prevent a compilation error:
# "libm.h:54:74: error: expected identifier or '(' before 'sizeof'"
# See also:
# http://lists.mplayerhq.hu/pipermail/mplayer-users/2012-June/084871.html
cat $SRCDIR/MPlayer_ffmpeg.patch | patch -p0 --verbose \
  2>&1 | tee $OUTPUT/patch-${PRGNAM}.log

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

echo Building ...
# MPlayer wants to automatically determine compiler flags,
# so we don't provide CFLAGS.
./configure \
  --prefix=/usr \
  --mandir=/usr/man \
  --confdir=/etc/mplayer \
  --enable-gui \
  --enable-menu \
  --disable-arts \
  --disable-bitmap-font \
  --codecsdir=${CODECSDIR} \
  --language="${LANGUAGES}" \
  ${EXTRACONFIGUREOPTIONS} \
  2>&1 | tee $OUTPUT/configure-${PRGNAM}.log
# So that MPlayer does not report "UNKNOWN" as it's version:
if [ ! -f VERSION ]; then
  echo $VERSION > VERSION
fi
make -j 8 2>&1 | tee $OUTPUT/make-${PRGNAM}.log
make DESTDIR=$PKG install 2>&1 |tee $OUTPUT/install-${PRGNAM}.log

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

# Add this to the doinst.sh:
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

# Update the desktop database:
if [ -x usr/bin/update-desktop-database ]; then
  chroot . /usr/bin/update-desktop-database usr/share/applications 1> /dev/null 2> /dev/null
fi

# Update hicolor theme cache:
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

# Update the mime database:
if [ -x usr/bin/update-mime-database ]; then
  chroot . /usr/bin/update-mime-database usr/share/mime >/dev/null 2>&1
fi

EOINS

# Strip binaries:
( find $PKG | xargs file | grep -e "executable" -e "shared object" \
  | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null )

# Add a package description:
mkdir -p $PKG/install
cat $SRCDIR/slack-desc > $PKG/install/slack-desc
if [ -f $SRCDIR/doinst.sh ]; then
  cat $SRCDIR/doinst.sh >> $PKG/install/doinst.sh
fi

# Build the package:
cd $PKG
makepkg --linkadd y --chown n $OUTPUT/${PRGNAM}-${PKGVERSION}-${ARCH}-${BUILD}${TAG}.txz 2>&1 | tee $OUTPUT/makepkg-${PRGNAM}.log
