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

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

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

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

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

config_vim() {
CFLAGS=-O2 \
/configure \
 $* \
 --prefix=/usr \
 --enable-pythoninterp \
 --enable-perlinterp \
 --disable-tclinterp \
 --enable-multibyte \
 --enable-cscope \
 --with-features=huge \
 --with-compiledby="<[email protected]>" \
 $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
config_vim --with-x --enable-gui=gtk2
make
make install DESTDIR=$PKG

# 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/install
cat $CWD/slack-desc.vim-gvim > $PKG/install/slack-desc
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
)

# 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

# Perfect!
# 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 $TMP/vim-${VIMVER}*tgz 1> /dev/null 2> /dev/null ; then
 echo "FATAL: VIM package needed in $TMP"
 exit 1
fi
mkdir $PKG/vim
( cd $PKG
 ( cd vim
   echo "Extracting reference package $TMP/vim-${VIMVER}*tgz:"
   explodepkg $TMP/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
)

mkdir -p $PKG/usr/doc/vim-$VIMVER.$PATCHLEVEL
( 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 )

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

# 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

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