%global ver 4.0.4
%global rev 3010

Name:           firebird
Version:        %{ver}.%{rev}
Release:        3

Summary:        SQL relational database management system
License:        Interbase
URL:            http://www.firebirdsql.org/

Source0:        https://github.com/FirebirdSQL/firebird/releases/download/v%{ver}/Firebird-%{ver}.%{rev}-0.tar.xz
Source1:        firebird-logrotate
Source2:        firebird.service
Source3:        fb_config

Patch0000:      add-pkgconfig-files.patch
Patch0001:      no-copy-from-icu.patch
Patch0002:      cloop-honour-build-flags.patch
# from upstream
Patch0003:      c++17.patch
Patch0004:      noexcept.patch
Patch0005:      autoconf.patch
Patch0006:      btyacc-honour-build-flags.patch
Patch0007:      firebird-configure-c99.patch
#Patch0008:      0001-Port-to-RISC-V-64-bit-riscv64.patch
Patch0009:     0001-add-loongarch64-support-for-Firebird.patch
#Patch0010:     0002-Modify-PROD_FLAGS-and-DEV_FLAGS-parameters-for-loongarch64.patch
Patch0011:      0001-add-sw_64-support.patch

BuildRequires: autoconf
BuildRequires: automake
BuildRequires: libtommath-devel
BuildRequires: libtool
BuildRequires: ncurses-devel
BuildRequires: libicu-devel
BuildRequires: libedit-devel
BuildRequires: gcc-c++
BuildRequires: libstdc++-static
BuildRequires: systemd-units
BuildRequires: chrpath
BuildRequires: zlib-devel
BuildRequires: procmail
BuildRequires: make
BuildRequires: libtomcrypt-devel
BuildRequires: unzip
BuildRequires: sed

Requires(postun): /usr/sbin/userdel
Requires(postun): /usr/sbin/groupdel
Requires(pre):    /usr/sbin/groupadd
Requires(pre):    /usr/sbin/useradd
Recommends:     logrotate
Requires:       libfbclient2 = %{version}-%{release}
Requires:       libib-util = %{version}-%{release}
Requires:       %{name}-utils = %{version}-%{release}

Obsoletes:      firebird-arch < 4.0
Obsoletes:      firebird-filesystem < 4.0
Obsoletes:      firebird-classic-common < 4.0
Obsoletes:      firebird-classic < 4.0
Obsoletes:      firebird-superclassic < 4.0
Obsoletes:      firebird-superserver < 4.0
Conflicts:      firebird-arch < 4.0
Conflicts:      firebird-filesystem < 4.0
Conflicts:      firebird-classic-common < 4.0
Conflicts:      firebird-classic < 4.0
Conflicts:      firebird-superclassic < 4.0
Conflicts:      firebird-superserver < 4.0


%description
Firebird is a relational database offering many ANSI SQL standard features
that runs on Linux, Windows, MacOS and a variety of Unix platforms.
Firebird offers excellent concurrency, high performance, and powerful
language support for stored procedures and triggers. It has been used
in production systems, under a variety of names, since 1981.


%package devel
Requires:       %{name} = %{version}-%{release}
Requires:       libfbclient2-devel = %{version}-%{release}
Summary:        UDF support library for Firebird SQL server

%description devel
This package is needed for development of client applications and user
defined functions (UDF) for Firebird SQL server.

Firebird is a relational database offering many ANSI SQL standard features
that runs on Linux, Windows, MacOS and a variety of Unix platforms.
Firebird offers excellent concurrency, high performance, and powerful
language support for stored procedures and triggers. It has been used
in production systems, under a variety of names, since 1981.


%package -n libib-util
Summary:        Firebird SQL UDF support library

%description -n libib-util
libib_util contains utility functions used by
User-Defined Functions (UDF) for memory management etc.

Firebird is a relational database offering many ANSI SQL standard features
that runs on Linux, Windows, MacOS and a variety of Unix platforms.
Firebird offers excellent concurrency, high performance, and powerful
language support for stored procedures and triggers. It has been used
in production systems, under a variety of names, since 1981.


%package -n libfbclient2
Summary:        Firebird SQL server client library
Obsoletes:      firebird-libfbclient < 4.0
Conflicts:      firebird-libfbclient < 4.0
Obsoletes:      firebird-libfbembed < 4.0

%description -n libfbclient2
Shared client library for Firebird SQL server.

Firebird is a relational database offering many ANSI SQL standard features
that runs on Linux, Windows, MacOS and a variety of Unix platforms.
Firebird offers excellent concurrency, high performance, and powerful
language support for stored procedures and triggers. It has been used
in production systems, under a variety of names, since 1981.


%package -n libfbclient2-devel
Summary:        Development libraries and headers for Firebird SQL server
Requires:       %{name}-devel = %{version}-%{release}
Requires:       libfbclient2 = %{version}-%{release}
Requires:       pkgconfig

%description -n libfbclient2-devel
Development files for Firebird SQL server client library.

Firebird is a relational database offering many ANSI SQL standard features
that runs on Linux, Windows, MacOS and a variety of Unix platforms.
Firebird offers excellent concurrency, high performance, and powerful
language support for stored procedures and triggers. It has been used
in production systems, under a variety of names, since 1981.


%package help
Requires:       %{name} = %{version}-%{release}
Summary:        Documentation for Firebird SQL server
BuildArch:      noarch

%description help
Documentation for Firebird SQL server.

Firebird is a relational database offering many ANSI SQL standard features
that runs on Linux, Windows, MacOS and a variety of Unix platforms.
Firebird offers excellent concurrency, high performance, and powerful
language support for stored procedures and triggers. It has been used
in production systems, under a variety of names, since 1981.


%package utils
Requires:       libfbclient2 = %{version}-%{release}
Summary:        Firebird SQL user utilities

%description utils
Firebird SQL user utilities.

Firebird is a relational database offering many ANSI SQL standard features
that runs on Linux, Windows, MacOS and a variety of Unix platforms.
Firebird offers excellent concurrency, high performance, and powerful
language support for stored procedures and triggers. It has been used
in production systems, under a variety of names, since 1981.

%prep
%autosetup -n Firebird-%{ver}.%{rev}-0 -p1

%build
export CFLAGS="%{optflags} -fno-strict-aliasing"
export CXXFLAGS="${CFLAGS} -fno-delete-null-pointer-checks"
NOCONFIGURE=1 ./autogen.sh
%configure --disable-rpath --prefix=%{_prefix} \
  --with-system-editline \
  --with-fbbin=%{_bindir} --with-fbsbin=%{_sbindir} \
  --with-fbconf=%{_sysconfdir}/%{name} \
  --with-fblib=%{_libdir} --with-fbinclude=%{_includedir} \
  --with-fbdoc=%{_defaultdocdir}/%{name} \
  --with-fbsample=%{_defaultdocdir}/%{name}/sample \
  --with-fbsample-db=%{_localstatedir}/lib/%{name}/data \
  --with-fbhelp=%{_localstatedir}/lib/%{name}/system \
  --with-fbintl=%{_libdir}/%{name}/intl \
  --with-fbmisc=%{_datadir}/%{name}/misc \
  --with-fbsecure-db=%{_localstatedir}/lib/%{name}/secdb \
  --with-fbmsg=%{_localstatedir}/lib/%{name}/system \
  --with-fblog=%{_localstatedir}/log/%{name} \
  --with-fbglock=%{_rundir}/%{name} \
  --with-fbplugins=%{_libdir}/%{name}/plugins \
  --with-fbtzdata=%{_localstatedir}/lib/%{name}/tzdata 

make %{?_smp_mflags}
cd gen
sed -i '/linkFiles "/d' ./install/makeInstallImage.sh
./install/makeInstallImage.sh
chmod -R u+w buildroot%{_docdir}/%{name}

%install
chmod u+rw,a+rx gen/buildroot/%{_includedir}/firebird/impl
cp -r gen/buildroot/* ${RPM_BUILD_ROOT}/
mkdir -p ${RPM_BUILD_ROOT}%{_libdir}/pkgconfig
cp -v gen/install/misc/*.pc ${RPM_BUILD_ROOT}%{_libdir}/pkgconfig/

cd ${RPM_BUILD_ROOT}
rm -vf .%{_sbindir}/*.sh
mv -v .%{_sbindir}/fb_config .%{_libdir}/
install -p -m 0755 %{SOURCE3} %{buildroot}%{_sbindir}/fb_config
rm -vf .%{_includedir}/perf.h
rm -vf .%{_libdir}/libicu*.so
chmod -R u+w .%{_docdir}/%{name}
mv -v .%{_datadir}/%{name}/misc/upgrade/udf/* .%{_docdir}/%{name}/
rm -rvf .%{_datadir}/%{name}/misc
mv -v .%{_sysconfdir}/%{name}/README.md .%{_sysconfdir}/%{name}/CHANGELOG.md \
  .%{_docdir}/%{name}/
mv -v .%{_sysconfdir}/%{name}/IDPLicense.txt .%{_docdir}/%{name}/
mv -v .%{_sysconfdir}/%{name}/IPLicense.txt .%{_docdir}/%{name}/
mv -v .%{_bindir}/gstat .%{_bindir}/gstat-fb
mv -v .%{_bindir}/isql .%{_bindir}/isql-fb
rm -rvf .%{_defaultdocdir}/%{name}/sample/prebuilt

mkdir -p .%{_localstatedir}/log/%{name}
mkdir -p .%{_sysconfdir}/logrotate.d
echo 1 > .%{_localstatedir}/log/%{name}/%{name}.log
sed "s@%{name}.log@%{_localstatedir}/log/%{name}/%{name}.log@g" %{SOURCE1} > .%{_sysconfdir}/logrotate.d/%{name}

mkdir -p .%{_unitdir}
cp -f %{SOURCE2} .%{_unitdir}/%{name}.service


%pre 
# Create the firebird group if it doesn't exist
getent group %{name} > /dev/null || /usr/sbin/groupadd -r %{name} 
getent passwd %{name} >/dev/null || /usr/sbin/useradd -d / -g %{name} -s /sbin/nologin -r %{name} 

# Add gds_db to /etc/services if needed
FileName=/etc/services
newLine="gds_db 3050/tcp  # Firebird SQL Database Remote Protocol"
oldLine=`grep "^gds_db" $FileName`
if [ -z "$oldLine" ]; then
 echo $newLine >> $FileName
fi


%post 
%systemd_post firebird.service


%postun 
%systemd_postun_with_restart firebird.service


%preun 
%systemd_preun firebird.service


%files
%{_docdir}/%{name}/IDPLicense.txt
%{_docdir}/%{name}/IPLicense.txt
%{_bindir}/fbtracemgr
%{_sbindir}/firebird
%{_sbindir}/fbguard
%{_sbindir}/fb_lock_print
%dir %{_sysconfdir}/%{name}
%config(noreplace) %{_sysconfdir}/%{name}/databases.conf
%config(noreplace) %{_sysconfdir}/%{name}/fbtrace.conf
%config(noreplace) %{_sysconfdir}/%{name}/firebird.conf
%config(noreplace) %{_sysconfdir}/%{name}/plugins.conf
%config(noreplace) %{_sysconfdir}/%{name}/replication.conf
%dir %{_libdir}/%{name}
%dir %{_datadir}/%{name}
%{_libdir}/%{name}/intl
%{_libdir}/%{name}/plugins

%dir %{_localstatedir}/lib/%{name}
%dir %attr(0700,%{name},%{name}) %{_localstatedir}/lib/%{name}/secdb
%dir %attr(0700,%{name},%{name}) %{_localstatedir}/lib/%{name}/data
%dir %attr(0755,%{name},%{name}) %{_localstatedir}/lib/%{name}/system
%dir %attr(0755,%{name},%{name}) %{_localstatedir}/lib/%{name}/tzdata
%attr(0600,firebird,firebird) %config(noreplace) %{_localstatedir}/lib/%{name}/secdb/security4.fdb
%attr(0644,firebird,firebird) %{_localstatedir}/lib/%{name}/system/help.fdb
%attr(0644,firebird,firebird) %{_localstatedir}/lib/%{name}/system/firebird.msg
%attr(0644,firebird,firebird) %{_localstatedir}/lib/%{name}/tzdata/*.res
%ghost %dir %attr(0775,%{name},%{name}) /run/%{name}
%ghost %attr(0644,%{name},%{name}) /run/%{name}/fb_guard
%dir %{_localstatedir}/log/%{name}
%config(noreplace) %attr(0664,%{name},%{name})  %{_localstatedir}/log/%{name}/%{name}.log
%config(noreplace) %attr(0644,root,root) %{_sysconfdir}/logrotate.d/%{name}

%attr(0644,root,root) %{_unitdir}/%{name}.service


%files devel
%{_includedir}/*.h
%{_includedir}/%{name}
%{_libdir}/fb_config
%{_sbindir}/fb_config


%files -n libfbclient2
%{_libdir}/libfbclient.so.*


%files -n libfbclient2-devel
%{_libdir}/libfbclient.so
%{_libdir}/pkgconfig/fbclient.pc


%files -n libib-util
%{_libdir}/libib_util.so


%files help
%{_docdir}/%{name}
%exclude %{_docdir}/%{name}/IDPLicense.txt
%exclude %{_docdir}/%{name}/IPLicense.txt
%attr(0600,firebird,firebird) %{_localstatedir}/lib/%{name}/data/employee.fdb


%files utils
%{_bindir}/gstat-fb
%{_bindir}/fbsvcmgr
%{_bindir}/gbak
%{_bindir}/gfix
%{_bindir}/gpre
%{_bindir}/gsec
%{_bindir}/isql-fb
%{_bindir}/nbackup
%{_bindir}/qli
%{_bindir}/gsplit

%changelog
* Tue Mar 04 2025 maqi <maqi@uniontech.com> - 4.0.4.3010-3
- add sw_64 support

* Mon May 20 2024 Wenlong Zhang <zhangwenlong@loongson.cn> - 4.0.4.3010-2
- add loongarch64 support for firebird

* Fri Apr 19 2024 xiejing <xiejing@kylinos.cn> - 4.0.4.3010-1
- Update to 4.0.4.3010

* Thu Aug 10 2023 dillon chen <dillon.chen@gmail.com> - 4.0.3.2975-1
- Update to 4.0.3.2975

* Thu May 25 2023 Wenlong Zhang <zhangwenlong@loongson.cn> - 3.0.10.33601-4
- Modify PROD_FLAGS and DEV_FLAGS parameters for loongarch64 

* Mon May  8 2023 Wenlong Zhang <zhangwenlong@loongson.cn> - 3.0.10.33601-3
- add loongarch64 support

* Thu Mar 2 2023 dillon chen <dillon.chen@gmail.com> - 3.0.10.33601-2
- add -fPIE in honour-buildflags.patch

* Thu Sep 1 2022 Funda Wang <fundawang@yeah.net> - 3.0.10.33601-1
- New version 3.0.10

* Tue Aug 30 2022 dillon chen<dillon.chen@gmail.com> - 3.0.3.32900-10
- put correct source as /usr/sbin/fb_config

* Mon Mar 7 2022 yaoxin <yaoxin30@huawei.com> - 3.0.3.32900-9
- Fix failed to parse pid from pid file

* Fri Sep 10 2021 bzhaoop <bzhaojyathousandy@gmail.com> - 3.0.3.32900-8
- Del rpath in some binaries for firebird.

* Thu Feb 25 2021 huanghaitao <huanghaitao8@huawei.com> - 3.0.3.32900-7
- Fix login shell to /sbin/nologin

* Mon Jan 13 2020 openEuler Buildteam <buildteam@openeuler.org> - 3.0.3.32900-6
- Type:bugfix
- Id:NA
- SUG:NA
- DESC:remove the libtermcap-devel in buildrequires

* Mon Dec 2 2019 lihao <lihao129@huawei.com> - 3.0.3.32900-5
- Package Init


