%global uid 133
%global username bacula

Name:               bacula
Version:            13.0.1
Release:            2
Summary:            Cross platform network backup for Linux, Unix, Mac and Windows
License:            AGPLv3 with exceptions
URL:                http://www.bacula.org
Source0:            https://sourceforge.net/projects/bacula/files/bacula/%{version}/%{name}-%{version}.tar.gz
Source6:            bacula.logrotate
Source10:           bacula-fd.service
Source11:           bacula-dir.service
Source12:           bacula-sd.service
Source15:           bacula-fd.sysconfig
Source16:           bacula-dir.sysconfig
Source17:           bacula-sd.sysconfig
Patch0001:          bacula-sqlite-priv.patch
Patch0002:          bacula-seg-fault.patch
Patch0003:          bacula-autoconf.patch
Patch0004:          bacula-stop-the-service-and-the-process-exits-normally.patch
BuildRequires:      desktop-file-utils perl-generators sed autoconf automake lzo-devel zlib-devel
BuildRequires:      gcc gcc-c++ glibc-devel ImageMagick libacl-devel libstdc++-devel libxml2-devel
BuildRequires:      libcap-devel ncurses-devel openssl-devel readline-devel sqlite-devel 
BuildRequires:      qt5-qtbase-devel systemd mariadb-connector-c-devel perl-interpreter libpq-devel

%description
Bacula is a set of programs that allow you to manage the backup, recovery, and
verification of computer data across a network of different computers. It is
based on a client/server architecture and is efficient and relatively easy to
use, while offering many advanced storage management features that make it easy
to find and recover lost or damaged files.

%package_help

%package common
Summary:            Common Bacula files
Provides:           bacula-libs = %{version}-%{release} bacula-libs-sql = %{version}-%{release} bacula-libs-sqlite = %{version}-%{release} 
Obsoletes:          bacula-libs < %{version}-%{release} bacula-sysconfdir <= 2.4 bacula-libs-sql < %{version}-%{release}
Provides:           bacula-libs-mysql = %{version}-%{release} bacula-libs-postgresql = %{version}-%{release}
Obsoletes:          bacula-libs-mysql <= 5.0.3 bacula-libs-sqlite <= 5.0.3 bacula-libs-postgresql <= 5.0.3
Provides:           group(%username) = %uid user(%username) = %uid bacula-director = %{version}-%{release}
Obsoletes:          bacula-sysconfdir <= 2.4 bacula-director < %{version}-%{release} 
Provides:           bacula-director-common = %{version}-%{release} bacula-director-mysql = %{version}-%{release}
Obsoletes:          bacula-director-common < 5.2.3-5 bacula-director-mysql < 5.2.3-5
Provides:           bacula-director-sqlite = %{version}-%{release} bacula-director-postgresql = %{version}-%{release}
Obsoletes:          bacula-director-sqlite < 5.2.3-5 bacula-director-postgresql < 5.2.3-5
Provides:           bacula-storage-common = %{version}-%{release} bacula-storage = %{version}-%{release}
Obsoletes:          bacula-storage-common < 5.2.2-2 bacula-storage < %{version}-%{release}
Provides:           bacula-storage-mysql = %{version}-%{release} bacula-storage-sqlite = %{version}-%{release}
Obsoletes:          bacula-storage-mysql < 5.2.0 bacula-storage-sqlite < 5.2.0 bacula-console-wxwidgets <= 5.0.3
Provides:           bacula-storage-postgresql = %{version}-%{release} bacula-console = %{version}-%{release}
Obsoletes:          bacula-storage-postgresql < 5.2.0 bacula-console < %{version}-%{release} bacula-console-gnome <= 2.4
Provides:           bacula-console-bat = %{version}-%{release} bacula-traymonitor = %{version}-%{release}
Obsoletes:          bacula-console-bat < %{version}-%{release} bacula-traymonitor < %{version}-%{release}
Requires:           mt-st mtx dejavu-lgc-sans-fonts
Requires(pre):      shadow-utils
Requires(postun):   shadow-utils
%{?systemd requires}

%description common
This package contains files common to all Bacula daemons.

%package logwatch
Summary:            Bacula Director logwatch scripts
BuildArch:          noarch
Requires:           bacula-common = %{version}-%{release}
Requires:           logwatch

%description logwatch
This package contains logwatch scripts for Bacula Director.

%package client
Summary:            Bacula backup client
Requires:           bacula-common = %{version}-%{release}
%{?systemd requires}

%description client
This package contains the bacula client, the daemon running on the system to be
backed up.

%package devel
Summary:            Bacula development files
Requires:           bacula-common = %{version}-%{release}

%description devel
This development package contains static libraries and header files.

%package -n nagios-plugins-bacula
Summary:            Nagios Plugin - check_bacula
Requires:           bacula-common = %{version}-%{release} nagios-common%{?_isa}

%description -n nagios-plugins-bacula
Provides check_bacula support for Nagios.

%prep
%autosetup -p1
sed -i -r 's/(hardcode_into_libs)=.*$/\1=no/' configure
find updatedb -type f | xargs chmod -x

%build
export CFLAGS="%{optflags} -I%{_includedir}/ncurses"
export CPPFLAGS="%{optflags} -I%{_includedir}/ncurses"
export PATH="$PATH:%{_qt5_bindir}"

%configure \
    --with-x \
    --disable-s3 \
    --with-mysql \
    --enable-bat \
    --with-sqlite3 \
    --with-openssl \
    --disable-conio \
    --disable-docker-plugin \
    --disable-rpath \
    --with-postgresql \
    --enable-readline \
    --enable-includes \
    --enable-largefile \
    --enable-build-dird \
    --enable-smartalloc \
    --enable-batch-insert \
    --enable-build-stored \
    --with-basename=bacula \
    --with-hostname=localhost \
    --with-smtp-host=localhost \
    --docdir=%{_datadir}/bacula \
    --with-plugindir=%{_libdir}/bacula \
    --sysconfdir=%{_sysconfdir}/bacula \
    --with-sd-password=@@SD_PASSWORD@@ \
    --with-fd-password=@@FD_PASSWORD@@ \
    --with-pid-dir=%{_localstatedir}/run \
    --with-dir-password=@@DIR_PASSWORD@@ \
    --with-scriptdir=%{_libexecdir}/bacula \
    --with-logdir=%{_localstatedir}/log/bacula \
    --with-mon-fd-password=@@MON_FD_PASSWORD@@ \
    --with-mon-sd-password=@@MON_SD_PASSWORD@@ \
    --with-mon-dir-password=@@MON_DIR_PASSWORD@@ \
    --with-bsrdir=%{_localstatedir}/spool/bacula \
    --with-subsys-dir=%{_localstatedir}/lock/subsys \
    --with-working-dir=%{_localstatedir}/spool/bacula \

# Remove RPATH
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

%make_build
%make_build -C examples/nagios/check_bacula

pushd src/qt-console/tray-monitor
    %{?qmake_qt5}%{!?qmake_qt5:qmake-qt5} tray-monitor.pro
    %make_build
    cp -f .libs/bacula-tray-monitor .
popd


%install
%make_install

install -p -m 755 -D examples/nagios/check_bacula/.libs/check_bacula %{buildroot}%{_libdir}/nagios/plugins/check_bacula
install -p -m 644 -D src/qt-console/images/bat_icon.png %{buildroot}%{_datadir}/pixmaps/bat_icon.png
install -p -m 644 -D scripts/bat.desktop %{buildroot}%{_datadir}/applications/bat.desktop
install -p -m 755 -D src/qt-console/.libs/bat %{buildroot}%{_sbindir}/bat
install -p -m 755 -D src/qt-console/tray-monitor/bacula-tray-monitor %{buildroot}%{_sbindir}/bacula-tray-monitor
install -p -m 644 -D src/qt-console/tray-monitor/tray-monitor.conf %{buildroot}%{_sysconfdir}/bacula/tray-monitor.conf
install -p -m 644 -D manpages/bacula-tray-monitor.1 %{buildroot}%{_mandir}/man1/bacula-tray-monitor.1
install -p -m 644 -D scripts/bacula-tray-monitor.desktop %{buildroot}%{_datadir}/applications/bacula-tray-monitor.desktop
mkdir -p %{buildroot}%{_localstatedir}/log/bacula
install -p -m 644 -D %{SOURCE6} %{buildroot}%{_sysconfdir}/logrotate.d/bacula
install -p -m 755 -D scripts/logwatch/bacula %{buildroot}%{_sysconfdir}/logwatch/scripts/services/bacula
install -p -m 644 -D scripts/logwatch/logfile.bacula.conf %{buildroot}%{_sysconfdir}/logwatch/conf/logfiles/bacula.conf
install -p -m 644 -D scripts/logwatch/services.bacula.conf %{buildroot}%{_sysconfdir}/logwatch/conf/services/bacula.conf
install -p -m 755 -D scripts/logwatch/applybaculadate %{buildroot}%{_sysconfdir}/logwatch/scripts/shared/applybaculadate
mkdir -p %{buildroot}%{_unitdir}
install -p -m 644 -D %{SOURCE10} %{buildroot}%{_unitdir}/bacula-fd.service
install -p -m 644 -D %{SOURCE11} %{buildroot}%{_unitdir}/bacula-dir.service
install -p -m 644 -D %{SOURCE12} %{buildroot}%{_unitdir}/bacula-sd.service
install -p -m 644 -D %{SOURCE15} %{buildroot}%{_sysconfdir}/sysconfig/bacula-fd
install -p -m 644 -D %{SOURCE16} %{buildroot}%{_sysconfdir}/sysconfig/bacula-dir
install -p -m 644 -D %{SOURCE17} %{buildroot}%{_sysconfdir}/sysconfig/bacula-sd
mkdir -p %{buildroot}%{_localstatedir}/spool/bacula

rm -f %{buildroot}%{_libdir}/*.la
rm -f %{buildroot}%{_sbindir}/bacula
rm -f %{buildroot}%{_libdir}/libbaccats.so
rm -f %{buildroot}%{_mandir}/man8/bacula.8.gz
rm -f %{buildroot}%{_datadir}/bacula/{ChangeLog,INSTALL,LICENSE*,README,ReleaseNotes,VERIFYING,technotes}
rm -f %{buildroot}%{_libexecdir}/bacula/{bacula,bacula-ctl-*,startmysql,stopmysql,bconsole,make_catalog_backup}

chmod 755 %{buildroot}%{_sbindir}/*
chmod 755 %{buildroot}%{_libdir}/bacula/*
chmod 755 %{buildroot}%{_libexecdir}/bacula/*
chmod 644 %{buildroot}%{_libexecdir}/bacula/btraceback.*

%ldconfig_scriptlets common

%pre common
getent group %username >/dev/null || groupadd -g %uid -r %username &>/dev/null || :
getent passwd %username >/dev/null || useradd -u %uid -r -s /sbin/nologin \
    -d /var/spool/bacula -M -c 'Bacula Backup System' -g %username %username &>/dev/null || :
exit 0

%post common
/usr/sbin/alternatives --install %{_libdir}/libbaccats.so libbaccats.so %{_libdir}/libbaccats-mysql.so 50
/usr/sbin/alternatives --install %{_libdir}/libbaccats.so libbaccats.so %{_libdir}/libbaccats-sqlite3.so 40
/usr/sbin/alternatives --install %{_libdir}/libbaccats.so libbaccats.so %{_libdir}/libbaccats-postgresql.so 60

if readlink /etc/alternatives/libbaccats.so | grep --silent mysql || \
    readlink /etc/alternatives/bacula-dir | grep --silent mysql || \
    readlink /etc/alternatives/bacula-sd | grep --silent mysql; then
    /usr/sbin/alternatives --set libbaccats.so %{_libdir}/libbaccats-mysql.so
elif readlink /etc/alternatives/libbaccats.so | grep --silent sqlite || \
    readlink /etc/alternatives/bacula-dir | grep --silent sqlite || \
    readlink /etc/alternatives/bacula-sd | grep --silent sqlite; then
    /usr/sbin/alternatives --set libbaccats.so %{_libdir}/libbaccats-sqlite3.so
else
    /usr/sbin/alternatives --set libbaccats.so %{_libdir}/libbaccats-postgresql.so
fi
%{?ldconfig}
%systemd_post %{name}-dir.service %{name}-sd.service

%ldconfig_postun common

%preun common
if [ "$1" = 0 ]; then
    /usr/sbin/alternatives --remove libbaccats.so %{_libdir}/libbaccats-mysql.so
    /usr/sbin/alternatives --remove libbaccats.so %{_libdir}/libbaccats-sqlite3.so
    /usr/sbin/alternatives --remove libbaccats.so %{_libdir}/libbaccats-postgresql.so
fi
%systemd_preun %{name}-dir.service %{name}-sd.service

%postun common
%systemd_postun_with_restart %{name}-dir.service %{name}-sd.service

%post client
%systemd_post bacula-fd.service

%preun client
%systemd_preun bacula-fd.service

%postun client
%systemd_postun_with_restart bacula-fd.service

%files common
%defattr(-,root,root)
%doc SUPPORT ReleaseNotes
%doc updatedb examples/sample-query.sql
%license LICENSE COPYING COPYRIGHT LICENSE-FAQ LICENSE-FOSS
%config(noreplace) %{_sysconfdir}/logrotate.d/bacula
%config(noreplace) %{_sysconfdir}/sysconfig/bacula-sd
%config(noreplace) %{_sysconfdir}/sysconfig/bacula-dir
%config(noreplace) %{_sysconfdir}/bacula/bat.conf %attr(640,root,root)
%config(noreplace) /usr/libexec/bacula/query.sql %attr(640,root,bacula)
%config(noreplace) %{_sysconfdir}/bacula/bconsole.conf %attr(640,root,root)
%config(noreplace) %{_sysconfdir}/bacula/bacula-sd.conf %attr(640,root,root)
%config(noreplace) %{_sysconfdir}/bacula/tray-monitor.conf %attr(640,root,root)
%config(noreplace) %{_sysconfdir}/bacula/bacula-tray-monitor.conf %attr(640,root,root)
%config(noreplace) %{_sysconfdir}/bacula/bacula-dir.conf %attr(640,root,bacula)


%{_sbindir}/*
%{_unitdir}/bacula-sd.service
%{_unitdir}/bacula-dir.service
%{_libdir}/*.so
%dir %{_localstatedir}/log/bacula %attr(750, bacula, bacula)
%dir %{_localstatedir}/spool/bacula %attr(750, bacula, bacula)
%dir %{_libexecdir}/%{name}
%dir %{_sysconfdir}/%{name} %attr(755,root,root)
%{_libexecdir}/%{name}/*
%{_datadir}/bacula/*.png
%{_datadir}/bacula/*.html
%{_datadir}/pixmaps/bat_icon.png
%{_datadir}/applications/bat.desktop
%{_datadir}/applications/bacula-tray-monitor.desktop
%exclude %{_sbindir}/bfdjson
%exclude %{_sbindir}/bacula-fd
%exclude %{_libdir}/libbac.so
%exclude %{_libdir}/libbacsd.so
%exclude %{_libdir}/libbacsql.so
%exclude %{_libdir}/libbaccfg.so
%exclude %{_libdir}/libbacfind.so
%exclude %{_libdir}/libbaccats-%{version}.so

%files logwatch
%defattr(-,root,root)
%config(noreplace) %{_sysconfdir}/logwatch/conf/logfiles/bacula.conf
%config(noreplace) %{_sysconfdir}/logwatch/conf/services/bacula.conf
%{_sysconfdir}/logwatch/scripts/services/bacula
%{_sysconfdir}/logwatch/scripts/shared/applybaculadate

%files client
%defattr(-,root,root)
%config(noreplace) %{_sysconfdir}/sysconfig/bacula-fd
%config(noreplace) %{_sysconfdir}/bacula/bacula-fd.conf %attr(640,root,root)
%{_sbindir}/bfdjson
%{_sbindir}/bacula-fd
%{_unitdir}/bacula-fd.service
%{_libdir}/bacula/bpipe-fd.so

%files devel
%defattr(-,root,root)
%{_includedir}/bacula
%{_libdir}/libbac.so
%{_libdir}/libbacsd.so
%{_libdir}/libbaccfg.so
%{_libdir}/libbacsql.so
%{_libdir}/libbacfind.so

%files -n nagios-plugins-bacula
%defattr(-,root,root)
%{_libdir}/nagios/plugins/check_bacula

%files help
%defattr(-,root,root)
%doc AUTHORS ChangeLog README README.AIX SUPPORT VERIFYING
%{_mandir}/man*

%changelog
* Sat Jul 22 2023 xu_ping <707078654@qq.com> - 13.0.1-2
- Disable docker-plugin to fix build error.

* Sat Feb 04 2023 wenchaofan <349464272@qq.com> - 13.0.1-1
- Update to 13.0.1 version

* Fri Apr 15 2022 xu_ping <xuping33@h-partners.com> - 9.4.4-5
- Fix configure error

* Mon Mar 8 2021 zhangjiapeng <zhangjiapeng9@huawei.com> - 9.4.4-4
- Stop the service and the process exits normally

* Mon Sep 14 2020 Ge Wang <wangge20@huawei.com> - 9.4.4-3
- Modify Source0 Url

* Thu Nov 21 2019 openEuler Buildteam <buildteam@openeuler.org> - 9.4.4-2
- Package init
