#!/bin/sh

# Script to download, package and install MPlayer binary codecs.
# Last modified: 2005-04-02

TMP=/tmp/mplayer-codecs

rm -rf "$TMP"
umask 0022

ok() {
  echo " OK"
}

fail() {
  echo " Failed"
  [ -d "$TMP" ] && echo "Note: Temporary directory \"$TMP\" is not deleted."
  echo
  exit 1
}

if ! which lynx > /dev/null 2> /dev/null; then
  echo "This script requires lynx."
  exit 1
elif ! which wget > /dev/null 2> /dev/null; then
  echo "This script requires wget."
  exit 1
fi

cat << EOF

This script downloads, packages and (if run by root) installs
MPlayer binary codec pack "all". These codecs are optional but
they will enable MPlayer to play broader range of formats.

Quote from http://www1.mplayerhq.hu/homepage/design7/codecs.html:

    "Copyrights of all DLLs remain with their respective
    owner(s). You should be aware that some of these DLLs have
    licenses that restrict them to use with certain programs.
    We only distribute them, the rest is your responsibility."

EOF

echo -n "Continue? [Y/n] "
read foo
[ "$foo" = "n" ] && exit 0

echo
echo -n "Looking for codec package URL..."
CODECPACKURL=$(lynx -dump http://www1.mplayerhq.hu/MPlayer/releases/codecs/ 2> /dev/null \
  | grep -Eo 'http://www1.mplayerhq.hu/MPlayer/releases/codecs/all-[0-9]{8}.tar.bz2$' \
  | sort | tail -n1)
[ "$CODECPACKURL" != "" ] && ok || fail

echo -n "Parsing the URL..."
FILENAME=$(basename "$CODECPACKURL")
VERSION=$(basename "$FILENAME" .tar.bz2 | grep -Eo '[0-9]{8}')
[ "$VERSION" != "" ] && ok || fail

if [ -f "/var/log/packages/mplayer-codecs-$VERSION-i386-1unknown" ]; then
  echo "Found codec pack version $VERSION which is already installed."
  echo -n "Reinstall? [y/N] "
  read foo
  [ "$foo" != "y" ] && exit 0
  echo
fi

echo -n "Creating temporary directory $TMP..."
mkdir -m 0700 -p "$TMP"
[ $? = 0 ] && ok || fail

echo "Downloading codec pack version $VERSION:"
echo
echo "wget -O \"$FILENAME\" \"$CODECPACKURL\""
cd "$TMP"
wget -O "$FILENAME" "$CODECPACKURL"
[ $? != 0 ] && exit 1

echo -n "Decompressing..."
tar xf "$FILENAME" 2> /dev/null
[ $? = 0 ] && ok || fail

echo -n "Creating the package..."
mkdir -p package/usr/lib/codecs package/install
mv "all-$VERSION"/* package/usr/lib/codecs
rmdir "all-$VERSION"
cd package
cat << EOF > install/slack-desc
mplayer-codecs: mplayer-codecs (Binary codecs for MPlayer)
mplayer-codecs:
mplayer-codecs: These codecs are optional but they will enable MPlayer
mplayer-codecs: to play broader range of formats.
mplayer-codecs:
mplayer-codecs: From http://www1.mplayerhq.hu/homepage/design7/codecs.html:
mplayer-codecs: "Copyrights of all DLLs remain with their respective
mplayer-codecs: owner(s). You should be aware that some of these DLLs have
mplayer-codecs: licenses that restrict them to use with certain programs.
mplayer-codecs: We only distribute them, the rest is your responsibility."
mplayer-codecs:
EOF

makepkg -l y -c y "../mplayer-codecs-$VERSION-i386-1unknown.tar" > /dev/null 2> /dev/null && ok || fail

cd ..
if [ "$(id -u)" = "0" ]; then
  echo -n "Installing or upgrading the package..."
  upgradepkg --install-new --reinstall "mplayer-codecs-$VERSION-i386-1unknown.tar" > /dev/null && ok || fail
fi

echo -n "Save the created package file for further use? [y/N] "
read foo
if [ "$foo" = "y" ]; then
  echo -n "Compressing the package..."
  lzma e "mplayer-codecs-$VERSION-i386-1unknown.tar" "mplayer-codecs-$VERSION-i386-1unknown.tlz" 2> /dev/null && ok || fail
  echo "Package saved as \"$TMP/mplayer-codecs-$VERSION-i386-1unknown.tlz\"."
fi

echo -n "Delete temporary files? [Y/n] "
read foo
if [ "$foo" != "n" ]; then
  echo -n "Deleting temporary files..."
  rm -rf \
    package \
    "$FILENAME" \
    "mplayer-codecs-$VERSION-i386-1unknown.tar" \
    2> /dev/null && ok || fail
  cd ..
  rmdir "$TMP" 2> /dev/null
fi

echo
exit 0
