#!/bin/sh

# Slackware build script for mpv

# Copyright 2015 Hyperion


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

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i486 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

CWD=$(pwd)
TMP=${TMP:-/tmp}
PKG=$TMP/package-$NAME
OUTPUT=${OUTPUT:-/tmp}

# The SLKFLAGS aren't currently used. They do no harm and can
#   be left as a placeholder in case they are ever needed.
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi



rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $NAME-$VERSION
tar xvf $CWD/$NAME-$VERSION.tar.gz || tar xvf $CWD/v$VERSION.tar.gz
cp $CWD/waf-*.* $NAME-$VERSION/waf
chmod +x $NAME-$VERSION/waf
cd $NAME-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 \
  -o -perm 511 \) -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 640 -o -perm 600 -o -perm 444 \
  -o -perm 440 -o -perm 400 \) -exec chmod 644 {} \;

DOCS="Copyright LICENSE README.md RELEASE_NOTES"
MANUAL="DOCS/*.md DOCS/*.rst DOCS/*.txt"

# This leaves an opening for build options.
BUILD_OPTS=${BUILD_OPTS:-"--jobs=6 "}

# Setting CFLAGS and CXXFLAGS breaks configure.
./waf configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --confdir=/etc/$NAME \
  --mandir=/usr/man \
  --enable-libmpv-shared \
  --docdir=/usr/doc/$NAME-$VERSION \
  $BUILD_OPTS

./waf build
./waf --destdir=$PKG install

mkdir -p $PKG/etc/mpv
cat $CWD/mpv.conf > $PKG/etc/mpv/mpv.conf

#for f in $PKG/etc/mpv/*.conf ; do
  #mv $f{,.new}
#done

# single instance launcher
cat $CWD/gmpv > $PKG/usr/bin/gmpv
# cat $CWD/gclic > $PKG/usr/bin/gclic
chmod 755 $PKG/usr/bin/gmpv
# chmod 755 $PKG/usr/bin/gclic
sed -i 's/Exec=.*/Exec=gmpv %U/g' $PKG/usr/share/applications/mpv.desktop
sed -i 's/TryExec=.*/TryExec=gmpv/g' $PKG/usr/share/applications/mpv.desktop



# OSC tunnings
mkdir -p $PKG/etc/mpv/lua-settings
cat << "EOF" >> $PKG/etc/mpv/lua-settings/osc.conf
#valign=0.8
#halign=0
#vidscale=no
#boxalpha=128
#scaleforcedwindow=1
#scalewindowed=1
#scalefullscreen=1
EOF


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

if [ -d $PKG/usr/man ]; then
  find $PKG/usr/man -type f -exec gzip -9 {} \;
  for i in $( find $PKG/usr/man -type l ) ; do
    ln -s $( readlink $i ).gz $i.gz
    rm $i
  done
fi

mkdir -p $PKG/usr/doc/$NAME-$VERSION
cp -a $DOCS $PKG/usr/doc/$NAME-$VERSION
mkdir -p $PKG/usr/doc/$NAME-$VERSION/manual
for f in $MANUAL; do
  cp -a $f $PKG/usr/doc/$NAME-$VERSION/manual/
done
cat $CWD/$NAME.SlackBuild > $PKG/usr/doc/$NAME-$VERSION/$NAME.SlackBuild


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------------------------------------------------------|
mpv: mpv (a movie player based on MPlayer and mplayer2)
mpv:
mpv: mpv is a fork of mplayer2 and MPlayer. It shares some features with
mpv: the former projects while introducing many more. It supports a wide
mpv: variety of video file formats, audio and video codecs, and subtitle
mpv: types.  
mpv:
mpv:
mpv:
mpv: mpv homepage: http://mpv.io/
mpv:

EODESC

cat << "DOINST" > $PKG/install/doinst.sh
if [ -x /usr/bin/update-desktop-database ]; then
  /usr/bin/update-desktop-database -q usr/share/applications >/dev/null 2>&1
fi

if [ -e usr/share/icons/hicolor/icon-theme.cache ]; then
  if [ -x /usr/bin/gtk-update-icon-cache ]; then
    /usr/bin/gtk-update-icon-cache usr/share/icons/hicolor >/dev/null 2>&1
  fi
fi
DOINST

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$NAME-$VERSION-$ARCH-$BUILD$TAG.${PKGTYPE:-txz}

rm -rf PKG=$TMP/package-$NAME
