#!/bin/bash

# elvis.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 31-May-2004

# 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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# 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.bz2
cd $PKGNAM*
slackhousekeeping

# 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 || failmake
make install 

# 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 || 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
# Add /usr/bin/ex and /usr/bin/vi symlinks.
( cd $PKG/usr/bin ; ln -sf elvis ex ; ln -sf elvis vi )
mkdir $PKG/usr/doc
( cd $PKG/usr/doc ; ln -sf /usr/share/elvis-$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
