#!/bin/bash

# xvim.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>
# 1-March-2005

# This is the directory in the VIM source archive to cd into (X/GTK version):
DIRVER=70

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

# Paths to skeleton port's source & real Slackware source tree:
# Slackware's xvim package build script lives in source/ap/vim
export CWD=$SLACKSOURCE/ap/vim
export PORTCWD=$PWD

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

# Determine the CFLAGS for the known architectures:
case $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Determine VIM patchlevel:
PATCHLEVEL=$(/bin/ls $CWD/patches/* 2> /dev/null | tail -1 | cut -f 3 -d . )
if [ "$PATCHLEVEL" = "" ]; then
   PATCHLEVEL=000
fi

############# Build ctags #########################

# Update this when using a newer version of ctags:
CTAGSVER=5.5.4

printf "\t\t[*] Building ctags [*]"

# Extract source:
tar zxvvf $CWD/ctags-*.tar.gz
cd ctags-*
slackhousekeeping

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   $ARCH-slackware-linux || failconfig

# Build:
make || failmake

# Create some package framework:
mkdir -p $PKG/usr/{bin,man/man1,doc/ctags-$CTAGSVER}

# Install:
install -m755 ctags $PKG/usr/bin
install -m644 ctags.1 $PKG/usr/man/man1

# Copy docs:
cp -a COPYING EXTENDING.html FAQ INSTALL INSTALL.oth NEWS README \
     $PKG/usr/doc/ctags-$CTAGSVER

############# Build vim with X support #########################

printf "\t\t[*] Building xvim [*]"

# Extract source:
cd $TMPBUILD
tar jxvvf $CWD/vim-$VERSION.tar.bz2
tar jxvvf $CWD/vim-$VERSION-lang.tar.bz2
slackhousekeeping

cd vim$DIRVER
# Apply patches:
for file in $CWD/patches/* ; do
   ( zcat $file 2> /dev/null | patch -p0 --verbose --batch )
done

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --with-x \
   --enable-gui=gtk2 \
   --with-features=big \
   $ARCH-slackware-linux || failconfig

# Build:
make || failmake

# Install into package:
make install DESTDIR=$PKG

# Delete some cruft:
#rm -rf $PKG/usr/man/man1/etags.1

##################################################################

# 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

# Supplement the VERSION with the patch level:
VERSION=$VERSION.$PATCHLEVEL

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

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