#!/bin/bash

# mercurial.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.?z*
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Install into package:
python setup.py build install --root=$PKG || failinstall

# Ordinarily we will not add stuff that's not installed through the normal
# default installation method, but we've had a lot of requests, and it appears
# that installing at least the hgk extension is the ad-hoc standard.
mkdir -p $PKG/usr/libexec/mercurial
cp -a contrib/hgk $PKG/usr/libexec/mercurial
chmod 0755 $PKG/usr/libexec/mercurial/hgk

mkdir -p $PKG/etc/mercurial/hgrc.d
cat << EOF > $PKG/etc/mercurial/hgrc.d/hgk.rc.new
[extensions]
# enable hgk extension ('hg help' shows 'view' as a command)
hgk =
[hgk]

path=/usr/libexec/mercurial/hgk
EOF

# Bash and zsh completion:
mkdir -p $PKG/etc/bash_completion.d
cp -a contrib/bash_completion $PKG/etc/bash_completion.d/mercurial.sh
chmod 644 $PKG/etc/bash_completion.d/mercurial.sh
mkdir -p $PKG/usr/share/zsh/site-functions
cp -a contrib/zsh_completion $PKG/usr/share/zsh/site-functions/_mercurial
chmod 644 $PKG/usr/share/zsh/site-functions/_mercurial

# Emacs Lisp extensions:
mkdir -p $PKG/usr/share/emacs/site-lisp
cp -a contrib/mercurial.el contrib/mq.el $PKG/usr/share/emacs/site-lisp
chmod 644 $PKG/usr/share/emacs/site-lisp/*

( cd doc
  for dir in 1 5 ; do
    mkdir -p $PKG/usr/man/man${dir}
    for file in *.${dir} ; do
      install -m644 $file $PKG/usr/man/man${dir}
    done
  done )

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav CONTRIBUTORS COPYING* PKG-INFO README* \
        $PKG/usr/doc/$PKGNAM-$VERSION

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
