#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi

VIMVER=7.1
CTAGSVER=5.7
# This is the directory in the VIM source archive to cd into.
DIRVER=71
ARCH=${ARCH:-i486}
BUILD=1

PKG=$TMP/package-vim
rm -rf $PKG
mkdir -p $PKG

# 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

# ctags was once a part of vim,
# but now we have to bundle it in
cd $TMP
rm -rf ctags-$CTAGSVER
tar xzvf $CWD/ctags-$CTAGSVER.tar.gz || exit 1
cd ctags-$CTAGSVER
chown -R root:root .
CFLAGS=-O2 \
./configure \
  --prefix=/usr \
  --build=$ARCH-slackware-linux
make -j2 || exit 1
mkdir -p $PKG/usr/bin
cat ctags > $PKG/usr/bin/ctags
chmod 755 $PKG/usr/bin/ctags
mkdir -p $PKG/usr/man/man1
cat ctags.1 | gzip -9c > $PKG/usr/man/man1/ctags.1.gz
mkdir -p $PKG/usr/doc/ctags-$CTAGSVER
cp -a \
  COPYING EXTENDING.html FAQ INSTALL INSTALL.oth NEWS README \
  $PKG/usr/doc/ctags-$CTAGSVER
chmod 644 $PKG/usr/doc/ctags-$CTAGSVER/*

cd $TMP
rm -rf vim$DIRVER
tar xjvf $CWD/vim-$VIMVER.tar.bz2 || exit 1
tar xjvf $CWD/vim-$VIMVER-lang.tar.bz2 || exit 1

config_vim() {
CFLAGS=-O2 \
./configure \
  $* \
  --prefix=/usr \
  --enable-pythoninterp \
  --enable-perlinterp \
  --disable-tclinterp \
  --enable-multibyte \
  --enable-cscope \
  --with-features=huge \
  --with-compiledby="<volkerdi@slackware.com>" \
  --build=$ARCH-slackware-linux

  # I had been adding this, but got 100% complaints and 0% kudos:
  #  --enable-rubyinterp
}

cd $TMP/vim$DIRVER
chown -R root:root .
for file in $CWD/patches/* ; do
  ( zcat $file 2> /dev/null | patch -p0 --verbose --batch )
done
zcat $CWD/sh.vim.diff.gz | patch -p0 --verbose || exit 1

config_vim --without-x --disable-gui
make -j2 || exit 1
make install DESTDIR=$PKG

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

# Fix manpage symlinks:
if [ -d $PKG/usr/man ]; then
  ( cd $PKG/usr/man
    for manpagedir in $(find . -type d -name "man*") ; do
      ( cd $manpagedir
        for eachpage in $( find . -type l -maxdepth 1) ; do
          ln -s $( readlink $eachpage ).gz $eachpage.gz
          rm $eachpage
        done
        gzip -9 *.?
      )
    done
  )
fi

# 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/usr/doc/vim-$VIMVER.$PATCHLEVEL
cp -a README.txt $PKG/usr/doc/vim-$VIMVER.$PATCHLEVEL
find $PKG/usr/doc/vim-$VIMVER.$PATCHLEVEL -type f | xargs chmod 644
( cd $PKG/usr/doc/vim-$VIMVER.$PATCHLEVEL ; rm -rf doc )
( cd $PKG/usr/doc/vim-$VIMVER.$PATCHLEVEL ; ln -sf /usr/share/vim/vim$DIRVER doc )

mkdir -p $PKG/install
cat $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

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

cd $PKG
makepkg -l y -c n $TMP/vim-$VIMVER.$PATCHLEVEL-$ARCH-$BUILD.tgz