# No tests yet for 5.3.5
%global test_version 5.4.3
# If you are incrementing major_version, enable bootstrapping and adjust accordingly.
# Version should be the latest prior build. If you don't do this, RPM will break and
# everything will grind to a halt.
%global bootstrap 1
%global bootstrap_major_version 5.3
%global bootstrap_version %{bootstrap_major_version}.6

# Place rpm-macros into proper location.
%global macrosdir %(d=%{_rpmconfigdir}/macros.d; [ -d $d ] || d=%{_sysconfdir}/rpm; echo $d)

Summary:        Lua is a powerful, light-weight programming language designed for extending applications.
Summary(ja):    アプリケーション拡張向けに設計された強力かつ軽量なプログラミング言語
Name:           lua
Version:        5.4.3
Release:        1%{?_dist_release}
Group:          programming
Vendor:         Project Vine
Distribution:   Vine Linux

License:        MIT
URL:            https://www.lua.org/
Source0:        https://www.lua.org/ftp/%{name}-%{version}.tar.gz
# copied from doc/readme.html on 2014-07-18
Source1:        mit.txt
%if 0%{?bootstrap}
Source2:        http://www.lua.org/ftp/lua-%{bootstrap_version}.tar.gz
%endif
Source3:        http://www.lua.org/tests/lua-%{test_version}-tests.tar.gz
# multilib
Source4:        luaconf.h
# rpm-macro
Source1000:     macros.lua
# rpm-generator
Source1001:     lua.attr
Patch0:         %{name}-5.4.0-beta-autotoolize.patch
Patch1:         %{name}-5.3.0-idsize.patch
Patch3:         %{name}-5.2.2-configure-linux.patch
Patch4:         %{name}-5.3.0-configure-compat-module.patch
%if 0%{?bootstrap}
Patch5:         %{name}-5.3.0-autotoolize.patch
Patch6:		%{name}-5.3.5-luac-shared-link-fix.patch
%endif
# https://www.lua.org/bugs.html
Patch19:	%{name}-5.4.3-bug3.patch

BuildRoot:      %{_tmppath}/%{name}-%{version}-root
BuildRequires:  readline-devel
BuildRequires:  ncurses-devel

Requires:       lua-libs = %{version}-%{release}
# Normally, this is the same as version, but... not always.
%global         major_version %(echo %{version} | cut -d . -f1,2)

%description
Lua is an extension programming language designed to support 
general procedural programming with data description facilities. 
It also offers good support for object-oriented programming, 
functional programming, and data-driven programming. 
Lua is intended to be used as a powerful, light-weight configuration language 
for any program that needs one. 
Lua is implemented as a library, written in clean C 
(that is, in the common subset of ANSI C and C++).

%description -l ja
Lua は、データ記述機能を備え、汎用の手続き型プログラミングをサポート
するようデザインされた拡張プログラミング言語です。
オブジェクト指向プログラミング、関数型プログラミング、データ駆動型
プログラミングもサポートしています。
Lua は、コンフィギュレーションが必要なあらゆるプログラムのための、
パワフルかつ軽いコンフィギュレーション言語としての使用を意図しています。
Lua は、クリーンな C (つまり、ANSI C と C++ の共通のサブセット) で
書かれたライブラリとして実装されています。


%package libs
Summary:        Libraries for %{name}
Summary(ja):    Lua の共有ライブラリ
Provides:       lua(abi) = %{major_version}

%description libs
This package contains the shared libraries for %{name}.


%package        devel
Summary:        Libraries and include files for Lua.
Summary(ja):    Lua の開発用ファイル
Group:          programming
Requires:       %{name} = %{version}-%{release}
Requires:       ncurses-devel
Requires:       pkgconfig

%description devel
Libraries and include files for Lua.


%package        static
Summary:        Static library for Lua
Summary(ja):    Lua の静的ライブラリ
Group:          programming
Requires:       %{name}-devel = %{version}-%{release}

%description static
This package contains the static version of liblua for %{name}.


%prep
%if 0%{?bootstrap}
%setup -q -a 2 -a 3 -n %{name}-%{version}
%else
%setup -q -a 3
%endif
cp %{SOURCE1} .
mv src/luaconf.h src/luaconf.h.template.in
%patch0 -p1 -E -z .autoxxx
%patch1 -p1 -z .idsize
#%% patch2 -p1 -z .luac-shared
%patch3 -p1 -z .configure-linux
%patch4 -p1 -z .configure-compat-all

# Put proper version in configure.ac, patch0 hardcodes 5.3.0
sed -i 's|5.3.0|%{version}|g' configure.ac
%patch19 -p1 -b .bug3
autoreconf -ifv

%if 0%{?bootstrap}
cd lua-%{bootstrap_version}/
mv src/luaconf.h src/luaconf.h.template.in
%patch5 -p1 -b .autoxxx
%patch1 -p1 -b .idsize
%patch3 -p1 -z .configure-linux
%patch4 -p1 -z .configure-compat-all
%patch6 -p1 -b .luac-shared-link-fix
autoreconf -i
cd ..
%endif


%build
%configure --with-readline --with-compat-module
sed -i 's|^hardcode_libdir_flag_spec=.*|hardcode_libdir_flag_spec=""|g' libtool
sed -i 's|^runpath_var=LD_RUN_PATH|runpath_var=DIE_RPATH_DIE|g' libtool
# Autotools give me a headache sometimes.
sed -i 's|@pkgdatadir@|%{_datadir}|g' src/luaconf.h.template

# hack so that only /usr/bin/lua gets linked with readline as it is the
# only one which needs this and otherwise we get License troubles
make %{?_smp_mflags} LIBS="-lm -ldl"
# only /usr/bin/lua links with readline now #luac_LDADD="liblua.la -lm -ldl"

%if 0%{?bootstrap}
pushd lua-%{bootstrap_version}
%configure --with-readline --with-compat-module
sed -i 's|^hardcode_libdir_flag_spec=.*|hardcode_libdir_flag_spec=""|g' libtool
sed -i 's|^runpath_var=LD_RUN_PATH|runpath_var=DIE_RPATH_DIE|g' libtool
# Autotools give me a headache sometimes.
sed -i 's|@pkgdatadir@|%{_datadir}|g' src/luaconf.h.template

# hack so that only /usr/bin/lua gets linked with readline as it is the
# only one which needs this and otherwise we get License troubles
make %{?_smp_mflags} LIBS="-lm -ldl" luac_LDADD="liblua.la -lm -ldl"
popd
%endif


%install
rm -rf $RPM_BUILD_ROOT
make install DESTDIR=$RPM_BUILD_ROOT
rm $RPM_BUILD_ROOT%{_libdir}/*.la
mkdir -p $RPM_BUILD_ROOT%{_libdir}/lua/%{major_version}
mkdir -p $RPM_BUILD_ROOT%{_datadir}/lua/%{major_version}

# Rename luaconf.h to luaconf-<arch>.h to avoid file conflicts on
# multilib systems and install luaconf.h wrapper
mv %{buildroot}%{_includedir}/luaconf.h %{buildroot}%{_includedir}/luaconf-%{_arch}.h
install -p -m 644 %{SOURCE4} %{buildroot}%{_includedir}/luaconf.h

%if 0%{?bootstrap}
pushd lua-%{bootstrap_version}
mkdir $RPM_BUILD_ROOT/installdir
make install DESTDIR=$RPM_BUILD_ROOT/installdir
cp -a $RPM_BUILD_ROOT/installdir/%{_libdir}/liblua-%{bootstrap_major_version}.so $RPM_BUILD_ROOT%{_libdir}/
mkdir -p $RPM_BUILD_ROOT%{_libdir}/lua/%{bootstrap_major_version}
mkdir -p $RPM_BUILD_ROOT%{_datadir}/lua/%{bootstrap_major_version}
rm -rf $RPM_BUILD_ROOT/installdir
popd
%endif

# Install rpm-macro and requires generator
install -Dpm 0644 %{SOURCE1000} $RPM_BUILD_ROOT/%{macrosdir}/macros.lua
install -Dpm 0644 %{SOURCE1001} $RPM_BUILD_ROOT/%{_fileattrsdir}/lua.attr


%check
cd ./lua-%{test_version}-tests/

# Dont skip the fully portable or ram-hungry tests:
# sed -i.orig -e '
#     /attrib.lua/d;
#     /files.lua/d;
#     /db.lua/d;
#     /errors.lua/d;
#     ' all.lua
# LD_LIBRARY_PATH=$RPM_BUILD_ROOT/%{_libdir} $RPM_BUILD_ROOT/%{_bindir}/lua all.lua
# Removing tests that fail under mock/koji
sed -i.orig -e '
    /db.lua/d;
    /errors.lua/d;
    ' all.lua
LD_LIBRARY_PATH=$RPM_BUILD_ROOT/%{_libdir} $RPM_BUILD_ROOT/%{_bindir}/lua -e"_U=true" all.lua


%clean
rm -rf $RPM_BUILD_ROOT


%files
%defattr(-,root,root,-)
%{!?_licensedir:%global license %%doc}
%license mit.txt
%doc README doc/*.html doc/*.css doc/*.gif doc/*.png
%{_bindir}/lua
%{_bindir}/luac
%{_mandir}/man1/lua*.1*

%files libs
%dir %{_libdir}/lua
%dir %{_libdir}/lua/%{major_version}
%{_libdir}/liblua-%{major_version}.so
%dir %{_datadir}/lua
%dir %{_datadir}/lua/%{major_version}

%if 0%{?bootstrap}
%dir %{_libdir}/lua/%{bootstrap_major_version}
%{_libdir}/liblua-%{bootstrap_major_version}.so
%dir %{_datadir}/lua/%{bootstrap_major_version}
%endif

%files devel
%defattr(-,root,root,-)
%{_includedir}/l*.h
%{_includedir}/l*.hpp
%{_libdir}/liblua.so
%{_libdir}/pkgconfig/*.pc
%dir %{macrosdir}
%{macrosdir}/macros.lua
%{_fileattrsdir}/lua.attr

%files static
%defattr(-,root,root,-)
%{_libdir}/*.a


%changelog
* Sun May 09 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.4.3-1
- new upstream release.
- imported Patch19 from upstream.
- dropped ldconfig scriptlets.

* Sat Dec 26 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.4.2-1
- new upstream release.
- dropped Patch8-15 and 1000: fixed in upstream.

* Fri Aug 21 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 5.4.0-1
- new upstream release.

* Sat Aug 31 2019 Toshiaki Ara <ara_t@384.jp> 5.1.4-8
- rebuild with readline-8.0 and ncurses-6.1

* Sun Mar 22 2015 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 5.1.4-7
- rebuilt with readline 6.3

* Mon Dec 30 2013 IWAI, Masaharu <iwaim.sub@gmail.com> 5.1.4-6
- added Japanese summary
- moved devel and static subpackages to Development/Libraries Group 

* Mon Dec 30 2013 IWAI, Masaharu <iwaim.sub@gmail.com> 5.1.4-5
- build with current VineSeed

* Sat Sep 25 2010 Yoji TOYODA <bsyamato@sea.plala.or.jp> 5.1.4-4
- rebuild with rpm-4.8.1 for pkg-config file

* Fri Feb 12 2010 MATSUBAYASHI Kohji <shaolin@vinelinux.org> - 5.1.4-3
- rebuilt with gcc-4.4.3-3 on ppc

* Fri Feb  5 2010 MATSUBAYASHI Kohji <shaolin@vinelinux.org> - 5.1.4-2
- rebuilt with rpm-4.8.0-3 (on ppc)

* Wed Feb 03 2010 Daisuke SUZUKI <daisuke@linux.or.jp> 5.1.4-1
- new upstream release
- add lua-static subpackage

* Sat Jul 26 2008 Shu KONNO <owa@bg.wakwak.com> 5.1.2-1vl5
- applied new versioning policy and spec in utf-8

* Sun Aug 5 2007 Munehiro Yamamoto <myamamoto@g.math.s.chiba-u.ac.jp> 5.1.2-0vl2
- rebuild for VineSeed

* Sun Aug 5 2007 Munehiro Yamamoto <myamamoto@g.math.s.chiba-u.ac.jp> 5.1.2-0vl1
- initial build for Vine Linux 4.1
 - source update

* Sat Aug 4 2007 Munehiro Yamamoto <myamamoto@g.math.s.chiba-u.ac.jp> 5.1.1-0vlmp1
- rebuild for Vine Linux 4.1

* Fri Jan 19 2007 Hans de Goede <j.w.r.degoede@hhs.nl> 5.1.1-3
- Remove "-lreadline -lncurses" from lua.pc (bz 213895)

* Sun Oct 15 2006 Hans de Goede <j.w.r.degoede@hhs.nl> 5.1.1-2
- Only link /usr/bin/lua with readline / do not link %%{_libdir}/liblua-5.1.so
  with readline so that we don't cause any License troubles for packages
  linking against liblua-5.1.so, otherwise lua could drag the GPL only readline
  lib into the linking of non GPL apps.

* Sat Oct 14 2006 Hans de Goede <j.w.r.degoede@hhs.nl> 5.1.1-1
- New upstream release 5.1.1
- Fix detection of readline during compile (iow add readline support back)

* Sat Jul 23 2005 Munehiro Yamamoto <myamamoto@g.math.s.chiba-u.ac.jp> 5.0.2vlmp1
- initial build for Vine Linux 3.2