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

# vim-gvim.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter <mozes@slackware.com>
#
# 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.

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
# This reason this is ap/vim rather than xap/vim-gvim is because Slackware
# keeps its wares in source/ap/vim - xap/vim-gvim is a symlink.
# Handle building in the main tree and /patches:
slack_findpkgstore_is_stablerelease && {
   export CWD=$SLACKSOURCE/vim ;} || { export CWD=$SLACKSOURCE/ap/vim ;}

# Version of VIM is taken from the Slackware64 source tree
# rather than the arm/build script
#pushd $CWD
#VERSION=$(echo vim-${VIMBRANCH}*.tar.!(*sign|*asc|*sig) | rev | cut -f 3- -d . | cut -f 1 -d - | rev)
#popd

# 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

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

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

# We use the non-gui version of the vim package as a reference to
# avoid duplication of content.
# This means you need to have built the ap/vim package prior to the xap
# version.
# Using r2b this check is unnecessary and makes automation harder.
#if [ ! -f $PKGSTORE/ap/vim-${VERSION}*t?z ]; then
#   echo "** ERROR: Could not find non-X version of vim in the 'ap' series"
#   echo "          It may be the case if you build vim-gim first, as the"
#   echo "          versions won't match."
#   exit 1
#fi

config_vim() {
CFLAGS="$SLKCFLAGS" \
LDFLAGS="-L/usr/lib${LIBDIRSUFFIX}" \
./configure \
  $* \
  --prefix=/usr \
  --enable-luainterp=$LUAINTERP \
  --enable-multibyte \
  --enable-cscope \
  --enable-perlinterp=$PERLINTERP \
  --enable-python3interp=dynamic \
  --enable-pythoninterp=no \
  --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) || exit 1
slackhousekeeping
cd vim*/ || failextract

# Apply patches:
#


# Configure:
config_vim --with-x --enable-gui=gtk3 || failconfig

# Build:
make $NUMJOBS || make || exit 1

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

# 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 -f $PKG/usr/bin/{ex,vi}

mkdir -p $PKG/install
cat $CWD/slack-desc.vim-gvim > $PKG/install/slack-desc

# 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

# Unpack main vim package so we can use it as a reference:
mkdir $PKG/vim
( cd $PKG
  ( cd vim
    echo "Extracting non-gui slackware/ap/vim reference package :"
    slack_findpkgstore_is_stablerelease && {
       # We'll find it in the root of $PKGSTORE (patches/packages)
       explodepkg $PKGSTORE/vim-${VERSION}*t?z || exit 1 ;} || {
       # We'll find it in $PKGSTORE/ap (slackware/ap/)
       explodepkg $PKGSTORE/ap/vim-${VERSION}*t?z || exit 1 ;}

    sh install/doinst.sh
  ) || exit 1
) || exit 1

rm -rf $PKG/vim/install
( cd $PKG
  find vim | cut -b4- | while read sharedfile ; do
    if [ ! -d $sharedfile ]; then
      rm -fv $PKG/$sharedfile
    fi
  done
)

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

# Get rid of the terminal-based vim.desktop file:
rm -f $PKG/usr/share/applications/vim.desktop

# In Slackware's xap/vim-gvim package there are no man pages but there are the man
# page directories.  ARMedslack's acquires them so I'll just wipe them.
# The man pages are included in the ap/vim package which is a dependency requirement for
# using this xap/vim-gvim package:
find $PKG/usr/man -type f -print0 | xargs -0 rm -f

# Remove empty directories:
find $PKG -type d -exec rmdir -p {} \;

# Apply 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      # chown -R 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
