#!/bin/sh
# $Id: mplayer-codecs.SlackBuild,v 1.5 2010/09/26 11:39:08 root Exp root $
# Copyright 2006,2007,2010 Eric Hameleers, Eindhoven, NL
# -----------------------------------------------------------------------------
#
# Slackware SlackBuild script 
# ===========================
# By:         Eric Hameleers <alien@slackware.com>
# For:        mplayer-codecs
# Descr:      Windows codecs (32-bit and 64-bit) for MPlayer - and xine.
# URL:        http://www.mplayerhq.hu/
# Needs:     
# Changelog:
# 20061022-1: 05/Nov/2006 by Eric Hameleers <alien@slackware.com>
#             * Initial build.
# 20071007-1: 15/Oct/2007 by Eric Hameleers <alien@slackware.com>
#             * Update.
# 20100303-1: 26/sep/2010 by Eric Hameleers <alien@slackware.com>
#             * New 32-bit codec pack. Also build a codec package for x86_64
#               now (it's limited though), and default to "all" for 32-bit.
# 
# Run 'sh mplayer-codecs.SlackBuild' to build a Slackware package.
# The package (.txz) plus descriptive .txt file are created in /tmp .
# Install using 'installpkg'. 
#
# -----------------------------------------------------------------------------


PRGNAM=mplayer-codecs
BUILD=${BUILD:-1}
TAG=${TAG:-alien}

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

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

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

case "$ARCH" in
  i?86)
    TYPE=all
    VERSION=20100303
    LIBDIRSUFFIX=""
    ;;
  x86_64)
    # Note, for x86_64 only a small "essential" pack is available, so we wil
    # expand a bit with an additional (old) RPM:
    TYPE=essential
    VERSION=20071007
    RPMVER=20061022
    LIBDIRSUFFIX="64"
    ;;
  *)
    echo "Only x86 is a supported platform for Windows codec packs!"
    exit 1
    ;;
esac

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

# Save old umask and set to 0022:
_UMASK_=$(umask)
umask 0022

SOURCE[0]="$SRCDIR/ChangeLog"
SRCURL[0]="http://www1.mplayerhq.hu/MPlayer/releases/codecs/ChangeLog"

SOURCE[1]="$SRCDIR/MD5SUMS"
SRCURL[1]="http://www1.mplayerhq.hu/MPlayer/releases/codecs/MD5SUMS"

if [ "$ARCH" = "x86_64" ]; then
  SOURCE[2]="$SRCDIR/${TYPE}-amd64-${VERSION}.tar.bz2"
  SRCURL[2]="http://www1.mplayerhq.hu/MPlayer/releases/codecs/${TYPE}-amd64-${VERSION}.tar.bz2"
  SOURCE[3]="$SRCDIR/mplayer-codecs-extra-${RPMVER}-1.x86_64.rpm"
  SRCURL[3]="http://www.mplayerhq.hu/MPlayer/releases/codecs/mplayer-codecs-extra-${RPMVER}-1.x86_64.rpm"
else
  SOURCE[2]="$SRCDIR/${TYPE}-${VERSION}.tar.bz2"
  SRCURL[2]="http://www1.mplayerhq.hu/MPlayer/releases/codecs/${TYPE}-${VERSION}.tar.bz2"
fi

# DOCS="ChangeLog MD5SUMS"
DOCS="${SOURCE[0]} ${SOURCE[1]}"

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

# Create working directories:
mkdir -p $OUTPUT          # place for the package to be saved
mkdir -p $TMP/tmp-$PRGNAM # location to build the source
mkdir -p $PKG             # place for the package to be built
rm -rf $PKG/*             # always erase old package's contents
rm -rf $TMP/tmp-$PRGNAM/* # remove the remnants of previous build
rm -rf $OUTPUT/{configure,make,install,error,makepkg}-$PRGNAM.log
                          # remove old log files

# Source file availability:
for (( i = 0; i < ${#SOURCE[*]}; i++ )) ; do
  if ! [ -f ${SOURCE[$i]} ]; then
    if ! [ "x${SRCURL[$i]}" == "x" ]; then
      # Check if the $SRCDIR is writable at all - if not, download to $OUTPUT
      [ -w "$SRCDIR" ] || SOURCE[$i]="$OUTPUT/$(basename ${SOURCE[$i]})"
      echo "Source '$(basename ${SOURCE[$i]})' not available yet..."
      echo "Will download file to $(dirname $SOURCE[$i])"
      wget -nv -T 20 -O "${SOURCE[$i]}" "${SRCURL[$i]}" || true
      if [ $? -ne 0 -o ! -s "${SOURCE[$i]}" ]; then
        echo "Downloading '$(basename ${SOURCE[$i]})' failed... aborting the build."
        mv -f "${SOURCE[$i]}" "${SOURCE[$i]}".FAIL
        exit 1
      fi
    else
      echo "File '$(basename ${SOURCE[$i]})' not available... aborting the build."
      exit 1
    fi
  fi
done

if [ "$P1" == "--download" ]; then
  echo "Download complete."
  exit 0
fi

# --- PACKAGE BUILDING ---

echo "++"
echo "|| $PRGNAM-$VERSION"
echo "++"

cd $TMP/tmp-$PRGNAM

echo "Extracting the source archive(s) for $PRGNAM..."
# Install the codecs into the proper dir
mkdir -p $PKG/usr/lib${LIBDIRSUFFIX}/codecs
tar xvf ${SOURCE[2]}
chown -R root:root *
chmod -R u+w,go+r-w,a+X-s *
install -m0755 ${TYPE}*-${VERSION}/* \
  $PKG/usr/lib${LIBDIRSUFFIX}/codecs

# Create compatibility link:
cd $PKG/usr/lib${LIBDIRSUFFIX}
  if [ "$ARCH" = "x86_64" ]; then
    ln -s codecs win64
  else
    ln -s codecs win32
  fi
cd -

# Add some extra 64-bit codecs or else it will be very minimalistic:
if [ "$ARCH" = "x86_64" ]; then
  cd $PKG
    rpm2cpio < ${SOURCE[3]} | cpio -imdv
  cd -
fi

# Add documentation:
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a $DOCS $PKG/usr/doc/$PRGNAM-$VERSION || true
cat $SRCDIR/$(basename $0) > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild
chown -R root:root $PKG/usr/doc/$PRGNAM-$VERSION
find $PKG/usr/doc -type f -exec chmod 644 {} \;

# Add a package description:
mkdir -p $PKG/install
cat $SRCDIR/slack-desc > $PKG/install/slack-desc

# Build the package:
cd $PKG
makepkg --linkadd y --chown n $OUTPUT/${PRGNAM}-${TYPE}_${VERSION}-${ARCH}-${BUILD}${TAG}.txz \
  2>&1 | tee $OUTPUT/makepkg-${PRGNAM}.log
cd $OUTPUT
md5sum ${PRGNAM}-${TYPE}_${VERSION}-${ARCH}-${BUILD}${TAG}.txz > ${PRGNAM}-${TYPE}_${VERSION}-${ARCH}-${BUILD}${TAG}.txz.md5
cat $PKG/install/slack-desc | grep "^${PRGNAM}" > $OUTPUT/${PRGNAM}-${TYPE}_${VERSION}-${ARCH}-${BUILD}${TAG}.txt

# Restore the original umask:
umask ${_UMASK_}

