#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# elvis.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 31-May-2004
#
# Copyright 2005-2010  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# 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.!(*sign|*asc|*sig)
cd $PKGNAM* || exit 1
slackhousekeeping

# Fix redefined getline() with glibc > 2.9 by changing the
# local function to elv_getline():
zcat $CWD/elvis.ref.c.getline.diff.gz | patch -p1 --verbose || exit 1

# Package framework:
mkdir -p $PKG/usr/{bin,man/man1,share/$PKGNAM-$VERSION}

# Build so that the output gets into $PKG:
./configure \
  --without-x \
  --without-gnome \
  --bindir=$PKG/usr/bin \
  --datadir=$PKG/usr/share/elvis-$VERSION \
  --docdir=$PKG/usr/share/elvis-$VERSION/doc \
  --verbose || failconfig
make $NUMJOBS || make || failmake
make install || exit 1

# Now we make the real binaries:
make clean
./configure \
  --without-x \
  --without-gnome \
  --bindir=/usr/bin \
  --datadir=/usr/share/elvis-$VERSION \
  --docdir=/usr/share/elvis-$VERSION/doc \
  --verbose || failconfig
make $NUMJOBS || make || failmake

# And install them:
install -m755 elvtags ref elvis $PKG/usr/bin

# Turn off color highlighting by default.  I can't imagine editing files
# like that all day...  (this coming from the inventor of /etc/(DIR|LS)_COLORS ;-)
cp -a $CWD/elvis.clr* $PKG/usr/share/elvis-$VERSION
chown root:root $PKG/usr/share/elvis-$VERSION/elvis.clr*

# Install the man pages:
( cd doc
  install -m644 ctags.man  $PKG/usr/man/man1/elvtags.1
  install -m644 elvfmt.man $PKG/usr/man/man1/elvfmt.1
  install -m644 elvis.man  $PKG/usr/man/man1/elvis.1
  install -m644 ref.man    $PKG/usr/man/man1/ref.1 )

# Install docs:
cp -a BUGS COPYING INSTALL README.html \
      $PKG/usr/share/$PKGNAM-$VERSION

# If this is actually useful for anything, fill me in on what it's for:
rm -f $PKG/usr/share/elvis-$VERSION/tags
rm -f $PKG/usr/share/elvis-$VERSION/doc/*.man
rm -f $PKG/usr/share/elvis-$VERSION/doc/printdoc.bat
cd lib
mkdir $PKG/usr/doc
( cd $PKG/usr/doc ; ln -sf /usr/share/elvis-$VERSION . )

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
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
