#!/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-2026  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

# Versions of bundled packages and names of source archives.
# To save on maintenance, and since we carry the same versions as x86 Slackware,
# let's pull details from the x86 trunk:
slack_setvarfromupstream VIMBRANCH

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

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

# The possible settings for this are yes/no/dynamic.
# Set it to "no" until Ruby fixes this (slated for Ruby 4.1, it seems) or Vim
# upstream finds a workaround.
#RUBYDYNAMIC=${RUBYDYNAMIC:-=dynamic}
# Doesn't work on AArch64:
RUBYDYNAMIC=no

# 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)
#VERSION="$(wget -q -O - https://github.com/vim/vim/tags | grep v${VIMBRANCH} | head -n 1 | sed "s/^.*v${VIMBRANCH}/v${VIMBRANCH}/" | cut -f 1 -d '<' | tr -d v)"
#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

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

config_vim() {
CFLAGS=-O2 \
./configure \
   $* \
   --prefix=/usr \
   --enable-luainterp=$LUAINTERP \
   --disable-canberra \
   --enable-multibyte \
   --enable-cscope \
   --enable-perlinterp=$PERLINTERP \
   --enable-pythoninterp=no \
   --enable-python3interp=dynamic \
   --enable-rubyinterp${RUBYDYNAMIC} \
   --enable-tclinterp=dynamic \
   --with-features=huge \
   --with-compiledby="<mozes@slackware.com>" \
   --with-wayland=no \
   --with-x=no \
   --build=${SLK_ARCH_BUILD}
}

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

#
# Apply VIM patches:
#

# 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

# Add an example vimrc (with everything commented out):
cat $CWD/vim.vimrc.diff | 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
zcat $CWD/doinst.sh.gz > $PKG/install/doinst.sh

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

# 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
