#!/bin/bash

# vim.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 20-Jun-2004

# Update these with each re-build/upgrade:
VIMVER=7.2
CTAGSVER=5.7
# This is the directory in the VIM source archive to cd into.
DIRVER=72

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
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=armv4 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   alpha)   export SLKCFLAGS="-O2" ;;
   x86_64)  export SLKCFLAGS="-O2" ;;
   i486)    export SLKCFLAGS="-O2" ;;
   mips)    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

echo "Building patch level: $PATCHLEVEL"

###### Build ctags###################################################
cd $TMPBUILD
tar xvvf $CWD/ctags-$CTAGSVER.tar.gz
cd ctags-$CTAGSVER
slackhousekeeping

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

# Build:
make $NUMJOBS || failmake

# Install binary:
mkdir -p $PKG/usr/bin
install -m755 ctags $PKG/usr/bin

# Install man pages:
mkdir -p $PKG/usr/man/man1
install -m644 ctags.1 $PKG/usr/man/man1

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

##### Build vim without X support######################################

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*

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

# Configure:
config_vim \
   --without-x \
   --disable-gui || 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

cp -a runtime/vimrc_example.vim runtime/vimrc.new
# This had been reported to be helpful in using vim with crontab -e,
# but I've since heard that it works fine without this extra option
# and I'd rather not change things unless there is a reason.
#zcat $CWD/vim.vimrc.diff.gz | patch -p1 --verbose || exit 1
cat runtime/vimrc.new > $PKG/usr/share/vim/vimrc.new

# 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 )

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VIMVER.$PATCHLEVEL
cp -a README.txt $PKG/usr/doc/$PKGNAM-$VIMVER.$PATCHLEVEL
( cd $PKG/usr/doc/$PKGNAM-$VIMVER.$PATCHLEVEL ; rm -rf doc )
( cd $PKG/usr/doc/$PKGNAM-$VIMVER.$PATCHLEVEL ; ln -sf /usr/share/vim/vim$DIRVER doc )

# Package description & install script:
mkdir -pm755 $PKG/install
install -m644 $CWD/slack-desc.vim $PKG/install/slack-desc
cat << EOF > $PKG/install/doinst.sh
#!/bin/sh
config() {
  NEW="\$1"
  OLD="\$(dirname \$NEW)/\$(basename \$NEW .new)"
  # If there's no config file by that name, mv it over:
  if [ ! -r \$OLD ]; then
    mv \$NEW \$OLD
  elif [ "\$(cat \$OLD | md5sum)" = "\$(cat \$NEW | md5sum)" ]; then # toss the redundant copy
    rm \$NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}
config usr/share/vim/vimrc.new
EOF
zcat $CWD/doinst.sh.gz >> $PKG/install/doinst.sh

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

# Start fake root server:
start_fakeroot  # start the fakeroot server so we can set ownerships

# Set 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      # set all files to 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
