#!/bin/sh

# menconder_codecs.sh
# 2016-02-08
# by Gernot Walzl

# Upgrades Slackware's default MPlayer package.
# Adds mp3, xvid and x264 support to mencoder.
# sbopkg has to be configured properly before running this script.
# http://www.mplayerhq.hu/DOCS/HTML/en/codec-installation.html

SLACKMIRROR="http://mirror.switch.ch/ftp/mirror/slackware/"
SLACKVER=$(awk '{print $2}' /etc/slackware-version)
#SLACKVER=current
PRGNAM=MPlayer

sbopkg -i "lame xvidcore x264" -k

set -e

mkdir -p $PRGNAM
cd $PRGNAM || exit 1

DIR="patches/source/${PRGNAM}"
wget -O - "${SLACKMIRROR}slackware-${SLACKVER}/patches/CHECKSUMS.md5" \
  | grep "source/${PRGNAM}/" | sed 's#\./.*/#\./#' > CHECKSUMS.md5
if [ "$(cat CHECKSUMS.md5 | wc -l)" = "0" ]; then
  DIR="source/xap/${PRGNAM}"
  wget -O - "${SLACKMIRROR}slackware-${SLACKVER}/source/CHECKSUMS.md5" \
    | grep "${PRGNAM}/" | sed 's#\./.*/#\./#' > CHECKSUMS.md5
fi

FILES=$(awk '{print $2}' CHECKSUMS.md5)
for FILE in $FILES; do
  if [ ! -f "$FILE" ]; then
    wget -O "$FILE" "${SLACKMIRROR}slackware-${SLACKVER}/${DIR}/${FILE}"
  fi
done

md5sum -c CHECKSUMS.md5 || exit 1

chmod +x $PRGNAM.SlackBuild
USE_PATENTS="YES" ./MPlayer.SlackBuild || exit 1

PKG=$(ls -v /tmp/$PRGNAM-*.t?z | tail -n 1)
upgradepkg --reinstall --install-new $PKG

#slackpkg blacklist $PRGNAM