##
##  ispell.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_ispell     3.4.06
%define       V_igerman98  20161207
%define       V_hk_deutsch 20050408
%define       V_words      20050408

#   package information
Name:         ispell
Summary:      Interactive Spell Checker
URL:          https://www.cs.hmc.edu/~geoff/ispell.html
Vendor:       R.E. Gorin, Pace Willisson, Geoff Kuenning
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Writing
License:      BSD
Version:      %{V_ispell}
Release:      20231102

#   list of sources
Source0:      https://www.cs.hmc.edu/~geoff/tars/ispell-%{V_ispell}.tar.gz
Source1:      http://download.openpkg.org/components/versioned/ispell/hk-deutsch-%{V_hk_deutsch}.tar.gz
Source2:      http://www.j3e.de/ispell/igerman98/dict/igerman98-%{V_igerman98}.tar.bz2
Source3:      http://download.openpkg.org/components/versioned/ispell/words-%{V_words}.gz
Patch0:       ispell.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, bison, perl
PreReq:       OpenPKG, openpkg >= 20160101

%description
    Ispell is a fast screen-oriented spelling checker that shows you
    your errors in the context of the original file, and suggests
    possible corrections when it can figure them out. Compared to UNIX
    spell, it is faster and much easier to use. Ispell can also handle
    languages other than English.

%track
    prog ispell = {
        version   = %{version}
        url       = https://www.cs.hmc.edu/~geoff/ispell.html
        regex     = ispell-(__VER__)\.tar\.gz
    }
    prog ispell:igerman98 = {
        version   = %{V_igerman98}
        url       = http://www.j3e.de/ispell/igerman98/dict/
        regex     = igerman98-(__VER__)\.tar\.bz2
    }

%prep
    %setup -q
    %patch -p0

    #   unpack Unix dictionary
    %{l_gzip} -d -c %{SOURCE words-%{V_words}.gz} >$RPM_BUILD_DIR/words

    #   unpack German dictionaries
    mkdir hk-deutsch
    ( cd hk-deutsch
      %{l_gzip} -d -c %{SOURCE hk-deutsch-%{V_hk_deutsch}.tar.gz} | %{l_tar} xf -
    ) || exit $?
    %{l_bzip2} -d -c %{SOURCE igerman98-%{V_igerman98}.tar.bz2} | %{l_tar} xf -

%build
    #   configure package
    #   kludge: determine whether vendor termcap, termlib or curses library exists
    #   reason: some brain-dead platforms (like SuSE, etc) have no termcap library installed.
    termlib=""
    echo "int main(int argc, char *argv[]) { return 0; }" >dummy.c
    for lib in termcap termlib curses ncurses; do
        rc=0; %{l_cc} -o dummy dummy.c -l$lib >/dev/null 2>&1 || rc=1
        if [ $rc -eq 0 ]; then termlib="-l$lib"; break; fi
    done
    [ ".$termlib" = . ] && exit 1
    ( echo "#define MINIMENU"
      echo "#define MASKBITS 64"
      echo "#define CC         \"%{l_cc}\""
      echo "#define CFLAGS     \"%{l_cflags -O}\""
      echo "#define BINDIR     \"%{l_prefix}/bin\""
      echo "#define LIBDIR     \"%{l_prefix}/share/ispell\""
      echo "#define MAN1DIR    \"%{l_prefix}/man/man1\""
      echo "#define MAN45DIR   \"%{l_prefix}/man/man4\""
      echo "#define MAN45EXT   \".4\""
      echo "#define WORDS      \"$RPM_BUILD_DIR/words\""
      echo "#define YACC       \"%{l_prefix}/bin/bison -y\""
      echo "#define TERMLIB    \"$termlib\""
      case "%{l_platform -t}" in
          *-linux* | *-sunos* ) echo "#define USG" ;;
      esac
      #   echo "#define LANGUAGES \"{american,MASTERDICTS=american.med+,HASHFILES=americanmed+.hash,EXTRADICT=$RPM_BUILD_DIR/words}\""
    ) >local.h

    #   build package
    %{l_make} %{l_mflags}

    #   build additional German dictionaries
    ( cd hk-deutsch
      cat worte.txt verben.txt \
          adjektive.txt klein.txt geographie.txt vornamen.txt \
          abkuerz.txt imperat.txt latein.txt informatik.txt \
          infoabk.txt elektronik.txt orgabk.txt marken.txt \
          alphabeta.txt roemisch.txt zusammen.txt | \
          sort -u >all.words
      ../buildhash all.words deutsch.aff deutsch.hash
    ) || exit $?
    ( cd igerman98-%{V_igerman98}
      %{l_shtool} subst \
          -e 's;/usr/bin/perl;%{l_prefix}/bin/perl;g' \
          bin/*.pl
      %{l_shtool} subst \
          -e 's;munchlist;../munchlist;g' \
          Makefile
      %{l_make} %{l_mflags} \
          SHELL="%{l_bash}" \
          UNSQ=cat RM_OR_NOT=rm BUILDHASH=`pwd`/../buildhash \
          ispell/de_DE.aff ispell/de_DE.hash 2>/dev/null
    ) || exit $?

%install
    #   install package
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/man \
        $RPM_BUILD_ROOT%{l_prefix}/share/ispell
    ( echo "#define MINIMENU"
      echo "#define MASKBITS 64"
      echo "#define CC         \"%{l_cc}\""
      echo "#define CFLAGS     \"%{l_cflags -O}\""
      echo "#define BINDIR     \"$RPM_BUILD_ROOT%{l_prefix}/bin\""
      echo "#define LIBDIR     \"$RPM_BUILD_ROOT%{l_prefix}/share/ispell\""
      echo "#define MAN1DIR    \"$RPM_BUILD_ROOT%{l_prefix}/man/man1\""
      echo "#define MAN45DIR   \"$RPM_BUILD_ROOT%{l_prefix}/man/man4\""
      echo "#define MAN45EXT   \".4\""
      echo "#define WORDS      \"$RPM_BUILD_DIR/words\""
      #   echo "#define LANGUAGES \"{american,MASTERDICTS=american.med+,HASHFILES=americanmed+.hash,EXTRADICT=$RPM_BUILD_DIR/words}\""
    ) >local.h
    %{l_make} %{l_mflags} \
        config.sh install-basic install-deformatters install-dictbuild install-languages

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

    #   install German dictionaries
    ( cd hk-deutsch
      %{l_shtool} install -c -m 644 \
          deutsch.aff $RPM_BUILD_ROOT%{l_prefix}/share/ispell/deutsch-old.aff
      %{l_shtool} install -c -m 644 \
          deutsch.hash $RPM_BUILD_ROOT%{l_prefix}/share/ispell/deutsch-old.hash
    ) || exit $?
    ( cd igerman98-%{V_igerman98}
      %{l_shtool} install -c -m 644 \
          ispell/de_DE.aff $RPM_BUILD_ROOT%{l_prefix}/share/ispell/deutsch.aff
      %{l_shtool} install -c -m 644 \
          ispell/de_DE.hash $RPM_BUILD_ROOT%{l_prefix}/share/ispell/deutsch.hash
    ) || exit $?

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

