#!/bin/bash

# vim-gvim.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:
# This reason this is ap/vim rather than xap/vim-gvim is because Slackware
# keeps its wares in source/ap/vim - xap/vim-gvim is a symlink.
export CWD=$SLACKSOURCE/ap/vim
export PORTCWD=$PWD

# Version of VIM is taken from the Slackware64 source tree
# rather than the arm/build script
pushd $CWD
VERSION=$(echo vim-${VIMBRANCH}*.tar.xz | rev | cut -f 3- -d . | cut -f 1 -d - | rev)
popd

VIMBRANCH=7.4
PYVER=$(python -V 2>&1 | cut -f 2 -d' ' | cut -f 1-2 -d.)

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

# We use the non-gui version of the vim package as a reference to
# avoid duplication of content.
# This means you need to have built the ap/vim package prior to the xap
# version.
if [ ! -f $PKGSTORE/ap/vim-${VERSION}*t?z ]; then
   echo "** ERROR: Could not find non-X version of vim in the 'ap' series"
   exit 1
fi

config_vim() {
CFLAGS="$SLKCFLAGS" \
LDFLAGS="-L/usr/lib${LIBDIRSUFFIX}" \
./configure \
  $* \
  --prefix=/usr \
  --enable-pythoninterp \
  --with-python-config-dir=/usr/lib${LIBDIRSUFFIX}/python$PYVER/config \
  --enable-perlinterp \
  --disable-tclinterp \
  --enable-multibyte \
  --enable-cscope \
  --with-features=huge \
  --with-compiledby="<mozes@slackware.com>" \
  --build=$ARCH-slackware-linux-gnueabi
}

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

# Configure:
config_vim --with-x --enable-gui=gtk2 || failconfig

# Build:
make $NUMJOBS || make || exit 1

# Install into package:
make install DESTDIR=$PKG || exit 1
rsync -lprvt $PKG/usr/share/man/ $PKG/usr/man/
rm -r $PKG/usr/share/man

# Legacy binary links:
( cd $PKG/usr/bin ; rm -rf ex )
( cd $PKG/usr/bin ; ln -sf vim ex )
( cd $PKG/usr/bin ; rm -rf rview )
( cd $PKG/usr/bin ; ln -sf vim rview )
( cd $PKG/usr/bin ; rm -rf rvim )
( cd $PKG/usr/bin ; ln -sf vim rvim )
( cd $PKG/usr/bin ; rm -rf view )
( cd $PKG/usr/bin ; ln -sf vim view )
( cd $PKG/usr/bin ; rm -rf eview )
( cd $PKG/usr/bin ; ln -sf vim eview )
( cd $PKG/usr/bin ; rm -rf evim )
( cd $PKG/usr/bin ; ln -sf vim evim )

mkdir -p $PKG/install
cat $CWD/slack-desc.vim-gvim > $PKG/install/slack-desc
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

# You'll have to run "gvim" to get the graphical version.
# Seems like this is the common way for console and gui versions
# of vim to co-exist.  If your "vi" symlink isn't pointed in a
# way that suits you, it is fully user serviceable.  :-)
( cd $PKG/usr/bin
  rm -f gvim
  mv vim gvim
  for programlink in $(find . -type l | cut -b3-) ; do
    rm $programlink
    ln -sf gvim $programlink
  done
)
rm -f vimtutor xxd

# Unpack main vim package so we can use it as a reference:
mkdir $PKG/vim
( cd $PKG
  ( cd vim
    echo "Extracting non-gui slackware/ap/vim reference package :"
    explodepkg $PKGSTORE/ap/vim-${VERSION}*t?z || exit 1
    sh install/doinst.sh
  ) || exit 1
) || exit 1

rm -rf $PKG/vim/install
( cd $PKG
  find vim | cut -b4- | while read sharedfile ; do
    if [ ! -d $sharedfile ]; then
      rm -fv $PKG/$sharedfile
    fi
  done
)

# Finally, reference purge:
rm -rf $PKG/vim

# Perhaps we would be better off without this - forcing folks to run kappfinder
# or something.  It seems better than starting with nothing, but might force
# people into defaults they don't want.  Another nifty catch-22...
mkdir -p $PKG/usr/share/applications
cp -a $CWD/gvim.desktop $PKG/usr/share/applications
mkdir -p $PKG/usr/share/pixmaps
cp -a $CWD/gvim.png $PKG/usr/share/pixmaps

# In Slackware's xap/vim-gvim package there are no man pages but there are the man
# page directories.  ARMedslack's acquires them so I'll just wipe them.
# The man pages are included in the ap/vim package which is a dependency requirement for
# using this xap/vim-gvim package:
find $PKG/usr/man -type f -print0 | xargs -0 rm -f

# Remove empty directories:
find $PKG -type d -exec rmdir -p {} \;

# 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

# Need to re-create the package name in a variable since it was set in
# arm/build with a place hoder version of 0.00.
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz
slackmp         # run makepkg -l y -c n

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