##
##  vim.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED 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 AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS 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.
##

#   package versions
%define       V_vl  9.1
%define       V_vs  91
%define       V_pl  1857

#   package information
Name:         vim
Summary:      The Vi (Improved) Text Editor
URL:          http://www.vim.org/
Vendor:       Bram Moolenaar et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        CORE
Group:        Editor
License:      Charityware
Version:      %{V_vl}.%{V_pl}
Release:      20251015

#   package options
%option       with_features   normal
%option       with_multibyte  yes
%option       with_perl       no
%option       with_python     no
%option       with_tcl        no
%option       with_ruby       no
%option       with_terminal   no

#   list of sources
Source0:      https://github.com/vim/vim/archive/refs/tags/v%{version}.tar.gz
Source2:      rc.vim
Source3:      vimrc
Patch0:       vim.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, ncurses
PreReq:       OpenPKG, openpkg >= 20160101, ncurses
%if "%{with_perl}" == "yes"
BuildPreReq:  perl
PreReq:       perl
%endif
%if "%{with_python}" == "yes"
BuildPreReq:  python
PreReq:       python
%endif
%if "%{with_tcl}" == "yes"
BuildPreReq:  tcl
PreReq:       tcl
%endif
%if "%{with_ruby}" == "yes"
BuildPreReq:  ruby
PreReq:       ruby
%endif

%description
    Vim (Vi Improved) is a text editor that is upwards compatible with
    the classical Unix Vi. It can be used to edit any text files. It
    is especially useful for editing programs. Vim offers a lot of
    enhancements not available in the traditional Vi. For example,
    Vim has multi level undo, multi windows and buffers, syntax
    highlighting, command line editing, filename completion, on-line
    help, and visual selection.

%track
    prog vim = {
        version   = %{version}
        url       = https://github.com/vim/vim/tags
        regex     = tag/v(\d+.\d+\.\d+)
    }

%prep
    #   unpack base distribution parts
    %setup -q
    %patch -p0

    #   adjust introduction text for OpenPKG branding
    %{l_shtool} subst \
        -e 's;Modified by;Packaged by;' \
        src/version.c

    #   adjust paths to global configuration files
    %{l_shtool} subst \
        -e 's;^/\/\/ \(#define SYS_VIMRC_FILE\).*$;\1 "%{l_prefix}/etc/vim/vimrc";' \
        -e 's;^/\/\/ \(#define SYS_GVIMRC_FILE\).*$;\1 "%{l_prefix}/etc/vim/gvimrc";' \
        src/feature.h

%if "%{with_python}" == "yes"
    #   fix Python includes
    %{l_shtool} subst \
        -e 's;PYTHON_CFLAGS=.*$;PYTHON_CFLAGS="`python-config --includes`";' \
        src/auto/configure
%endif

%build
    #   configure package
    cd src
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags}" \
    GREP="grep" \
    ./configure \
        --prefix=%{l_prefix} \
        --mandir=%{l_prefix}/man \
        --with-local-dir=%{l_prefix} \
        --with-global-runtime=%{l_prefix}/lib/vim \
        --with-modified-by='%{l_openpkg_release}' \
        --with-tlib=ncurses \
        --without-x \
        --disable-gui \
%if "%{with_features}" == "huge"
        --with-features=huge \
%else
%if "%{with_features}" == "big"
        --with-features=big \
%else
%if "%{with_features}" == "small"
        --with-features=small \
%else
        --with-features=normal \
%endif
%endif
%endif
%if "%{with_multibyte}" == "yes"
        --enable-multibyte \
%endif
%if "%{with_perl}" == "yes"
        --enable-perlinterp \
%else
        --disable-perlinterp \
%endif
%if "%{with_python}" == "yes"
        --enable-pythoninterp \
        --with-python-config-dir=%{l_prefix}/lib/python/config \
%else
        --disable-pythoninterp \
%endif
%if "%{with_tcl}" == "yes"
        --enable-tclinterp \
%else
        --disable-tclinterp \
%endif
%if "%{with_ruby}" == "yes"
        --enable-rubyinterp \
%else
        --disable-rubyinterp \
%endif
%if "%{with_terminal}" == "yes"
        --enable-terminal \
%else
        --disable-terminal \
%endif
        --disable-nls
    %{l_shtool} subst \
        -e 's;^\(VIMRTDIR[^=]*=\).*;\1;' \
        Makefile

    #   build package
    %{l_make} %{l_mflags -O}

%install
    #   perform package installation procedure
    ( cd src
      %{l_make} %{l_mflags} install \
          prefix=$RPM_BUILD_ROOT%{l_prefix} \
          MANDIR=$RPM_BUILD_ROOT%{l_prefix}/man
    ) || exit $?

    #   strip down installation hierarchy
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/vim >/dev/null 2>&1 || true
    rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/xxd
    rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man1/xxd.1
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/vim/tools
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/vim/macros

    #   add links for program runtime variants
    for link in vi ex rview rvim view vimdiff; do
        rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/$link
        ln $RPM_BUILD_ROOT%{l_prefix}/bin/vim \
           $RPM_BUILD_ROOT%{l_prefix}/bin/$link
        rm -f $RPM_BUILD_ROOT%{l_prefix}/man/man1/$link.1
        ln $RPM_BUILD_ROOT%{l_prefix}/man/man1/vim.1 \
           $RPM_BUILD_ROOT%{l_prefix}/man/man1/$link.1
    done

    #   create installation area for addon packages
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/lib/vim

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.vim} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   install default configuration
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/vim
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE vimrc} $RPM_BUILD_ROOT%{l_prefix}/etc/vim/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/vim/vimrc'

%files -f files

%clean

%preun
    if [ $1 -eq 0 ]; then
        #   remove a (usually empty) "helptags" file of addon packages
        rm -f $RPM_INSTALL_PREFIX/lib/vim/doc/tags >/dev/null 2>&1 || true
    fi

