#!/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

VIMVER=$VERSION
# This is the directory in the VIM source archive to cd into.
DIRVER=72

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

# 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

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

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t"
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac 
export SLKLDFLAGS="-L/usr/lib${LIBDIRSUFFIX}"

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

config_vim() {
CFLAGS="$SLKCFLAGS" LDFLAGS="$SLKLDFLAGS" \
./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-$VIMVER.tar.?z* || exit 1
tar xvvf $CWD/vim-$VIMVER-lang.tar.?z* || exit 1
slackhousekeeping
cd vim$DIRVER

# If there's no syntax update, create one:
if ! ls $CWD/vim-runtime-syntax-* 1> /dev/null 2> /dev/null ; then
  rm -rf runtime/syntax.orig
  cp -a runtime/syntax runtime/syntax.orig
  echo "Fetching vim syntax updates from ftp.nluug.nl..."
  rsync -avzcP ftp.nluug.nl::Vim/runtime/syntax/ runtime/syntax/
  diff -u -r --new-file runtime/syntax.orig runtime/syntax | gzip -9c > $CWD/vim-runtime-syntax-$(date +%Y%m%d).diff.gz
  rm -rf runtime/syntax
  mv runtime/syntax.orig runtime/syntax
fi

# Apply the syntax update:
zcat $CWD/vim-runtime-syntax-*.diff.gz | patch -p0 --verbose || exit 1

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

# 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 reference package $TMP/vim-${VIMVER}*tgz:"
    explodepkg $PKGSTORE/ap/vim-${VIMVER}*t?z
    sh install/doinst.sh
  )
)
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

# Supplement the VERSION with the patch level:
VERSION=$VERSION.$PATCHLEVEL
export SLACKPACKAGE=$PKGNAM-$VERSION-$ARCH-$BUILD.tgz
slackmp         # run makepkg -l y -c n

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