#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# vim.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 20-Jun-2004
#
# Copyright 2008, 2009, 2010, 2013, 2016  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

# Update these with each re-build/upgrade:
XSB=$CWD/$PKGNAM.SlackBuild
[ ! -x $XSB ] && { echo "ERROR: Cannot find x86 SlackBuild for ${PKGNAM}!" ; exit 1; }
for upstreamvar in \
  VIMBRANCH \
  CTAGSVER \
  ; do
  eval $( egrep "^${upstreamvar}=" $XSB  )
  echo "Upstream variable: ${upstreamvar} version $( eval $( echo "echo \$${upstreamvar}" ) )"
done

# The possible settings for this are yes/no/dynamic.
PERLINTERP=${PERLINTERP:-dynamic}

# Version of VIM is taken from the Slackware64 source tree
# rather than the arm/build script
# The Slackware script also downloads if it's missing but we don't need to do that.
pushd $CWD
VERSION=$(echo vim-${VIMBRANCH}*.tar.!(*sign|*asc|*sig) | rev | cut -f 3- -d . | cut -f 1 -d - | rev)
popd
echo "VIM version: $VERSION"
echo "VIM branch : $VIMBRANCH"

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

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

# If dynamic support for Ruby works with this $ARCH, then allow it:
# needs checking for A64
if [ "$ARCH" = "x86_64" ]; then
  RUBYDYNAMIC="=dynamic"
fi

###### Build ctags###################################################
cd $TMPBUILD
tar xvvf $CWD/ctags-$CTAGSVER.tar.!(*sign|*asc|*sig)
cd ctags-$CTAGSVER || exit 1
slackhousekeeping

# Configure:
CFLAGS="$SLKCFLAGS" \
LDFLAGS="-L/usr/lib${LIBDIRSUFFIX}" \
./configure \
   --prefix=/usr \
   --build=${SLK_ARCH_BUILD} || failconfig

# Build:
make $NUMJOBS || make || 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 -fav \
   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 \
   --with-python-config-dir=/usr/lib${LIBDIRSUFFIX}/python$PYVER/config \
   --with-python3-config-dir=$(python3-config --configdir) \
   --disable-canberra \
   --enable-multibyte \
   --enable-cscope \
   --enable-perlinterp=$PERLINTERP \
   --enable-pythoninterp=dynamic \
   --enable-python3interp=dynamic \
   --enable-rubyinterp${RUBYDYNAMIC} \
   --enable-tclinterp=dynamic \
   --with-features=huge \
   --with-compiledby="<mozes@slackware.com>" \
   --build=${SLK_ARCH_BUILD}
}

# Extract source:
cd $TMPBUILD
tar xvvf $CWD/vim-${VERSION}.tar.!(*sign|*asc|*sig)
slackhousekeeping
cd vim* || exit 1

# Configure:
config_vim \
   --without-x \
   --disable-gui || failconfig

# Build:
make $NUMJOBS || make || failmake

# Install into package:
make install DESTDIR=$PKG
rsync -lprvt $PKG/usr/share/man/ $PKG/usr/man/
rm -r $PKG/usr/share/man

# Don't make backups in /var/spool/cron/*, which fixes "crontab -e":
zcat $CWD/vim.vimrc.diff.gz | patch -p1 --verbose || exit 1

cp -a runtime/vimrc_example.vim runtime/vimrc.new

# Add patched vimrc to the package:
cat runtime/vimrc.new > $PKG/usr/share/vim/vimrc.new

# Don't package gvim.desktop - it will be included in the vim-gvim package:
rm -f $PKG/usr/share/applications/gvim.desktop

# Remove empty directory:
rmdir $PKG/usr/share/applications

# Legacy binary links:
( 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 )

# These should not be shipped:
rm -fv $PKG/usr/bin/{ex,vi}

# Copy docs:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav README.txt \
      $PKG/usr/doc/$PKGNAM-$VERSION
( cd $PKG/usr/doc/$PKGNAM-$VERSION ; rm -rf doc )
( cd $PKG/usr/doc/$PKGNAM-$VERSION ; ln -sf ../../share/vim/vim$(echo $VIMBRANCH | tr -d .) doc )

# Move the vim.desktop into the docs directory. There's really not much use
# for this .desktop file. It's easy enough to just open a terminal and run
# vim in it without this.
mv -fv $PKG/usr/share/applications/vim.desktop $PKG/usr/doc/$PKGNAM--$VERSION

# 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
ulimit -s unlimited
shopt -s extglob
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

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

# Set generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # set all files to root.root, chmod -R og-w, slackchown, slack644docs

# Need to re-create the package name in a variable since it was set in
# arm/build with a place hoder version of 0.00.
export SLACKPACKAGE=$PKGNAM-$VERSION-$PKGARCH-$BUILD.txz
slackmp         # run makepkg -l y -c n

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