#!/bin/bash
#
# source: https://sites.google.com/site/kdekorte2/gnomemplayer
#
# maintainer: me
#

NAME=gnome-mplayer
VERSION=${VERSION:-$(echo $NAME-*.tar.?z* | rev | cut -f 3- -d . | cut -f 1 -d - | rev)}
BUILD=${BUILD:-3jp}

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


CWD=$(pwd)
PKG=/tmp/${NAME}-$VERSION-$ARCH-$BUILD

case "$ARCH" in
  i486)      SLKCFLAGS="-O2 -march=i486 -mtune=i686"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  x86_64)    SLKCFLAGS="-O2 -fPIC"
             SLKLDFLAGS="-L/usr/lib64"; LIBDIRSUFFIX="64"
             ;;
  armv7hl)   SLKCFLAGS="-O2 -march=armv7-a -mfpu=vfpv3-d16"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  armv6hl)   SLKCFLAGS="-O2 -march=armv6 -mfpu=vfp -mfloat-abi=hard"
             SLKLDFLAGS=""; LIBDIRSUFFIX=""
             ;;
  *)         SLKCFLAGS=${SLKCFLAGS:-"O2"}
             SLKLDFLAGS=${SLKLDFLAGS:-""}; LIBDIRSUFFIX=${LIBDIRSUFFIX:-""}
             ;;
esac


rm -rf $PKG
mkdir -p $PKG
rm -rf $CWD/$NAME-$VERSION
tar xvf $CWD/$NAME-$VERSION.tar.?z* || exit 1
cd $CWD/$NAME-$VERSION || exit 1
#cat $CWD/gxxxxxxxxxxxx.patch | patch -p1 --verbose

chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;
LDFLAGS="$SLKLDFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
CFLAGS="$SLKCFLAGS" \

./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --mandir=/usr/man \
  --docdir=/usr/doc/$PRGNAM-$VERSION \
  --localstatedir=/var \
  --sysconfdir=/etc \
  --disable-gtk3 \
  --disable-nautilus \
  --disable-schemas-install \
  --with-alsa \
  --with-pulseaudio \
  --with-libnotify \
  --program-prefix= \
  --program-suffix= \
  --build=$ARCH-slackware-linux
  # Adding '--disable-gtk3' causes error:
  # Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported
  # ... unless gmtk also is built without gtk3 support.
  # Without gtk3 we also do not need the symbolic icon fallback patch anymore.

make -j6 || make || exit 1
make install DESTDIR=$PKG || exit 1

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


# Compress and link manpages, if any:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a \
  AUTHORS COPYING INSTALL NEWS README TODO \
  $PKG/usr/doc/$NAME-$VERSION

# Description file
mkdir -p $PKG/install
cat << EODESC > $PKG/install/slack-desc
# HOW TO EDIT THIS FILE:
# The "handy ruler" below makes it easier to edit a package description.  Line
# up the first '|' above the ':' following the base package name, and the '|'
# on the right side marks the last column you can put a character in.  You must
# make exactly 11 lines for the formatting to be correct.  It's also
# customary to leave one space after the ':'.

                  |-----handy-ruler---------------------------------------------------|
gnome-mplayer: gnome-mplayer (GTK2 interface to MPlayer)
gnome-mplayer:
gnome-mplayer: GNOME MPlayer is a GTK GUI for MPlayer based on the Gnome HIG.
gnome-mplayer: The code is not dependent on any Gnome library but expects some
gnome-mplayer: form of GConf backend for storing settings.
gnome-mplayer: Slackware before 14.0 did not include GConf, for those older
gnome-mplayer: releases you should install the 'dconf' package so that your
gnome-mplayer: application preferences can still be stored.
gnome-mplayer:
gnome-mplayer: gnome-mplayer home: http://kdekorte.googlepages.com/gnomemplayer
gnome-mplayer:
EODESC

cat <<EOT > $PKG/install/doinst.sh
# Compile glib schema files:
if [ -x usr/bin/glib-compile-schemas ]; then
  chroot . /usr/bin/glib-compile-schemas usr/share/glib-2.0/schemas > /dev/null 2>&1
fi

# Update the desktop database:
if [ -x usr/bin/update-desktop-database ]; then
  chroot . /usr/bin/update-desktop-database usr/share/applications > /dev/null 2>&1
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

# Update hicolor theme cache:
if [ -d usr/share/icons/hicolor ]; then
  if [ -x /usr/bin/gtk-update-icon-cache ]; then
    chroot . /usr/bin/gtk-update-icon-cache -f -t usr/share/icons/hicolor 1> /dev/null 2> /dev/null
  fi
fi

EOT


mkdir -p $PKG/usr/src/$NAME-$VERSION
cat $CWD/$NAME.SlackBuild > $PKG/usr/src/$NAME-$VERSION/$NAME.SlackBuild

cd $PKG
/sbin/makepkg -l y -c n $PKG.txz
rm -rf $PKG
rm -rf $CWD/$NAME-$VERSION


