#!/bin/bash

# vim-gvim.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <stuart@armedslack.org>

# 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

# 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 $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# 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=-O2 \
./configure \
  $* \
  --prefix=/usr \
  --enable-pythoninterp \
  --enable-perlinterp \
  --disable-tclinterp \
  --enable-multibyte \
  --enable-cscope \
  --with-features=huge \
  --with-compiledby="<mozes@slackware.com>" \
  --build=$ARCH-slackware-linux
}

# Extract source:
cd $TMPBUILD
tar xvvf $CWD/vim-$VIMVER.tar.bz2
tar xvvf $CWD/vim-$VIMVER-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:
config_vim \
   --with-x \
   --enable-gui=gtk2 || failconfig

# Build:
make $NUMJOBS || failmake

# Install into package:
make install DESTDIR=$PKG

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 )

# Install package description & install script:
mkdir -pm755 $PKG/install
install -m644 $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

# Now we get rid of everything that's not in the other vim package.
# Let's assume (dangerous ;-) that we just built that in $TMP
# and use it as a reference:
if ! /bin/ls $PKGSTORE/ap/vim-${VIMVER}*tgz 1> /dev/null 2> /dev/null ; then
  echo "FATAL: VIM package needed in $PKGSTORE/ap/"
  exit 1
fi
mkdir -p $PKG/vim
( cd $PKG
  ( cd vim
    echo "Extracting reference package $PKGSTORE/ap/vim-${VIMVER}*tgz:"
    explodepkg $PKGSTORE/ap/vim-${VIMVER}*tgz 1> /dev/null
    sh install/doinst.sh  )
)
rm -r $PKG/vim/install
( cd $PKG
  find vim | cut -b4- | while read sharedfile ; do
    if [ ! -d $sharedfile ]; then
       rm --verbose $PKG/$sharedfile
    fi
  done )

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

# 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

# Perhaps we would be better off without this and 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

# Remove empty directories:
find $PKG -type d -exec rmdir -p {} \; 2> /dev/null

# 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
slackmp         # run makepkg -l y -c n

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