##
##  flex.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_new        2.6.0
%define       V_old        2.5.4a
%define       V_old_maj    2.5.4
%define       V_reflex     20230206

#   package information
Name:         flex
Summary:      Fast Lexical Analyzer Generator
URL:          http://www.gnu.org/software/flex/
Vendor:       Free Software Foundation
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        CORE
Group:        CompilerCompiler
License:      BSD
Version:      %{V_new}
Release:      20230207

#   package options
%option       with_old     no

#   list of sources
Source0:      http://download.sourceforge.net/flex/flex-%{V_new}.tar.gz
Source1:      http://download.sourceforge.net/flex/flex-%{V_old}.tar.gz
Source2:      ftp://ftp.invisible-island.net/reflex/reflex-%{V_reflex}.tgz
Patch0:       flex.patch

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

%description
    Flex is a tool for generating scanners: programs which recognize
    lexical patterns in text. Flex reads the given input files for a
    description of a scanner to generate. The description is in the
    form of pairs of regular expressions and C code, called rules.
    Flex generates as output a C source file, lex.yy.c, which defines
    a C function yylex(). This file is compiled and linked with the
    -lfl library to produce an executable. When the executable is run,
    it analyzes its input for occurrences of the regular expressions.
    Whenever it finds one, it executes the corresponding C code.

%track
    prog flex:new = {
        version   = %{V_new}
        url       = http://sourceforge.net/projects/flex/files/
        regex     = flex-(\d+\.\d+\.\d+)\.tar\.gz
    }
    prog flex:old = {
        disabled
        comment   = "cs: 'old' actually refers to the completely discontinued version of flex"
        version   = %{V_old}
        url       = http://flex.sourceforge.net/
        regex     = flex-(__VER__)\.tar\.gz
    }
    prog flex:reflex = {
        version   = %{V_reflex}
        url       = ftp://ftp.invisible-island.net/reflex/
        regex     = reflex-(__VER__)\.tgz
    }

%prep
    %setup -q -c
    %setup -q -T -D -a 1
    %setup -q -T -D -a 2
    %patch -p0 -d flex-%{V_new}
    sleep 1; touch flex-%{V_new}/scan.c
    %{l_shtool} subst \
        -e '1,/^@copying/!{ /^@end copying/,/^@copying/!H }' \
        -e '/^@insertcopying/g' \
        -e '/^@copying/,/^@end copying/d' \
        flex-%{V_new}/doc/flex.texi

%build
    #   configure and build Flex (new version)
    ( cd flex-%{V_new}
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      M4="%{l_prefix}/bin/m4" \
      MAKEINFO=`which true` \
      HELP2MAN=`which true` \
      ./configure \
          --prefix=%{l_prefix} \
          --mandir=%{l_prefix}/man \
          --infodir=%{l_prefix}/info \
          --disable-shared \
          --disable-nls
      cp -p doc/flex.1 doc/flex.1.saved
      %{l_make} %{l_mflags}
    ) || exit $?

%if "%{with_old}" == "yes"
    #   configure and build Flex (old version)
    ( cd flex-%{V_old_maj}
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      ./configure \
          --prefix=$RPM_BUILD_ROOT%{l_prefix}
      %{l_make} %{l_mflags}
    ) || exit $?
    #   configure and build Reflex (alternative old version)
    ( cd reflex-%{V_reflex}
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      ./configure \
          --prefix=%{l_prefix} \
          --mandir=%{l_prefix}/man
      %{l_make} %{l_mflags}
    ) || exit $?
%endif

%install

%if "%{with_old}" == "yes"
    #   install Flex (old version)
    ( cd flex-%{V_old_maj}
      %{l_make} %{l_mflags} install
      rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/flex++
      mv  $RPM_BUILD_ROOT%{l_prefix}/bin/flex \
          $RPM_BUILD_ROOT%{l_prefix}/bin/flex-old
      mv  $RPM_BUILD_ROOT%{l_prefix}/include/FlexLexer.h \
          $RPM_BUILD_ROOT%{l_prefix}/include/FlexLexer-old.h
      mv  $RPM_BUILD_ROOT%{l_prefix}/lib/libfl.a \
          $RPM_BUILD_ROOT%{l_prefix}/lib/libfl-old.a
      mv  $RPM_BUILD_ROOT%{l_prefix}/man/man1/flex.1 \
          $RPM_BUILD_ROOT%{l_prefix}/man/man1/flex-old.1
    ) || exit $?
    #   install Reflex (alternative old version)
    ( cd reflex-%{V_reflex}
      %{l_make} %{l_mflags} install \
          prefix=$RPM_BUILD_ROOT%{l_prefix} \
          exec_prefix=$RPM_BUILD_ROOT%{l_prefix}
    ) || exit $?
%endif

    #   install Flex (new version)
    ( cd flex-%{V_new}
      %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"
      %{l_shtool} install -c -m 644 \
          doc/flex.1.saved $RPM_BUILD_ROOT%{l_prefix}/man/man1/flex.1
    ) || exit $?

    #   strip down installation
    rm -f $RPM_BUILD_ROOT%{l_prefix}/info/dir
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/doc
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true

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

%files -f files

%clean

