%define _prefix /usr
%define _sysconfdir /etc

%define __brp_mangle_shebangs /bin/true
%define __brp_remove_la_files /bin/true
%define source_date_epoch_from_changelog 0

%define enable_mod_ffi 1
%define enable_mod_memc 0
%define enable_mod_mysql 1
%define enable_mod_uci 0

Summary: Hawk Interpreter
Name: @PACKAGE_NAME@
Version: @PACKAGE_VERSION@
Release: 1%{?dist}
License: Simplified BSD License
Vendor: Chung, Hyung-Hwan 
URL: @PACKAGE_URL@
Group: System Environment/Libraries
Source0: %{name}-%{version}.tar.gz
Requires: %{name}-libs%{?_isa}

%if %{enable_mod_ffi}
## prep_cif_var() available since 3.0.11
BuildRequires: libffi-devel%{?_isa} >= 3.0.11
%endif
%if %{enable_mod_memc}
BuildRequires: libmemcached-devel%{?_isa} >= 1.0.18
%endif
%if %{enable_mod_mysql}
##BuildRequires: mariadb-connector-c-devel%{?_isa}
BuildRequires: mysql-devel%{?_isa}
%endif
%if %{enable_mod_uci}
BuildRequires: libuci-devel%{?_isa}
%endif

BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)

%description
Hawk Interpreter

%package libs
Summary: Hawk runtime shared objects
Group: System Environment/Libraries

%description libs
This package contains runtime share object files for Hawk.

%if %{enable_mod_ffi}
%package ffi
Summary: Hawk ffi module
Group: System Environment/Libraries
Requires: %{name}-libs%{?_isa} = %{version}
Requires: libffi%{?_isa} >= 3.0.11

%description ffi
This package contains the ffi module file for Hawk.
%endif

%if %{enable_mod_memc}
%package memc
Summary: Hawk memc module
Group: System Environment/Libraries
Requires: %{name}-libs%{?_isa} = %{version}
Requires: libmemcached-libs%{?_isa} >= 1.0.18

%description memc
This package contains the memc module file for Hawk.
%endif

%if %{enable_mod_mysql}
%package mysql
Summary: Hawk mysql module
Group: System Environment/Libraries
Requires: %{name}-libs%{?_isa} = %{version}
## tricky to specify the right mysql/mariadb client library.
##Requires: mariadb-connector-c%{?_isa}

%description mysql
This package contains the mysql module file for Hawk.
%endif

%if %{enable_mod_uci}
%package uci
Summary: Hawk uci module
Group: System Environment/Libraries
Requires: %{name}-libs%{?_isa} = %{version}
Requires: libuci%{?_isa}

%description uci
This package contains the uci module file for Hawk.
%endif

%package devel
Summary: Development files for Hawk
Group: System Environment/Libraries
Requires: %{name}-libs%{?_isa} = %{version}

%description devel
The header files and libraries for developing applications with Hawk.

%package static
Summary: Static library files for Hawk
Group: System Environment/Libraries
Requires: %{name}-devel%{?_isa} = %{version}

%description static
The static library files for developing applications with Hawk.

%prep
%setup -q

%build
RPM_OPT_FLAGS="$(echo $RPM_OPT_FLAGS | %{__sed} 's/-O2/-O3/')"

##CFLAGS="${RPM_OPT_FLAGS} -fPIC" CXXFLAGS="${RPM_OPT_FLAGS} -fPIC -std=c++11" ./configure  \
CFLAGS="${RPM_OPT_FLAGS} -fPIC" CXXFLAGS="${RPM_OPT_FLAGS} -fPIC" ./configure  \
	--prefix=%{_prefix} \
	--libdir=%{_libdir} \
	--sysconfdir=%{_sysconfdir} \
	--enable-static=yes \
	--enable-shared=yes \
	--enable-static-module=yes \
	--enable-wide-char="yes:2w" \
	--enable-intmax=no \
	--enable-fltmax=no \
	--enable-cxx=yes \
	--enable-pthread-flags=yes \
	--enable-libltdl=no \
	--enable-libunwind=no \
%if %{enable_mod_ffi}
	--enable-mod-ffi=yes \
%else
	--enable-mod-ffi=no \
%endif
%if %{enable_mod_memc}
	--enable-mod-memcached=yes \
%else
	--enable-mod-memcached=no \
%endif
%if %{enable_mod_mysql}
	--with-mysql=yes \
	--enable-mod-mysql=yes \
%else
	--with-mysql=no \
	--enable-mod-mysql=no \
%endif
	--enable-mod-sed=yes:static \
%if %{enable_mod_uci}
	--enable-mod-uci=yes
%else
	--enable-mod-uci=no
%endif

make

%install
make install DESTDIR=%{buildroot}
rm -rf %{buildroot}%{_libdir}/pkgconfig
rm -f %{buildroot}%{_bindir}/uni-case
rm -f %{buildroot}%{_bindir}/uni-prop
rm -f %{buildroot}%{_bindir}/hawk[0-9][0-9]  ## delete binaries from the samples directory
rm -f %{buildroot}%{_libdir}/libhawk-*.la %{buildroot}%{_libdir}/libhawk-*.a ## delete static libraries of dynamic modules

%check
make check

%clean
rm -rf "%{buildroot}"

%post
/sbin/ldconfig

%postun
/sbin/ldconfig

%files
%defattr(-,root,root)
%{_bindir}/hawk

%files libs
%defattr(-,root,root)
%{_libdir}/libhawk.so.*

%if %{enable_mod_ffi}
%files ffi
%{_libdir}/libhawk-ffi.so*
%endif

%if %{enable_mod_memc}
%files memc
%{_libdir}/libhawk-memc.so*
%endif

%if %{enable_mod_mysql}
%files mysql
%{_libdir}/libhawk-mysql.so*
%endif

%if %{enable_mod_uci}
%files uci
%{_libdir}/libhawk-uci.so*
%endif

%files devel
%defattr(-,root,root)
%{_includedir}/*
%{_libdir}/libhawk.so

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