%global _hardened_build 1

%define _libexecdir %{_libdir}
%define defconfig DailySet1
%define indexserver amandahost
%define tapeserver %{indexserver}
%define amanda_user amandabackup
%define amanda_group disk

Name:          amanda
Version:       3.5.1
Release:       23
Summary:       A backup solution over network to disk
License:       BSD and GPLv3+ and GPLv2+ and GPLv2
URL:           http://www.amanda.org
Source:        http://downloads.sourceforge.net/amanda/amanda-%{version}.tar.gz
Source9:       amanda.socket
Source10:      amanda@.service
Source11:      activate-devpay.1.gz
Source12:      killpgrp.8
Source13:      amanda-udp.socket
Source14:      amanda-udp.service
Source15:      kamanda.socket
Source16:      kamanda@.service

Patch5:        patch-tirpc.patch
Patch6:        fix-multiple-definition.patch
#Refer:        https://github.com/zmanda/amanda/pull/176/
Patch7:        fix-hexencode-test.patch
Patch8:        CVE-2022-37704.patch
Patch9:        CVE-2022-37704_part_2.patch
Patch10:        CVE-2022-37705.patch

BuildRequires: automake autoconf libtool dump xfsdump cups
BuildRequires: samba-client tar grep gcc-c++ readline-devel libtirpc-devel
BuildRequires: krb5-devel openssh-clients ncompress mtx mt-st
BuildRequires: perl-devel perl-generators perl perl-Test-Simple
BuildRequires: glib2-devel openssl-devel swig bison flex
BuildRequires: libcurl-devel procps-ng systemd
Requires:      grep tar /bin/mail perl sharutils
Requires(post):   /sbin/ldconfig
Requires(postun): /sbin/ldconfig

Provides:      %{name}-libs = %{version}-%{release}
Provides:      %{name}-libs%{?_isa} = %{version}-%{release}
Provides:      %{name}-client = %{version}-%{release}
Provides:      %{name}-client%{?_isa} = %{version}-%{release}
Provides:      %{name}-server = %{version}-%{release}
Provides:      %{name}-server%{?_isa} = %{version}-%{release}
Obsoletes:     %{name}-libs < %{version}-%{release}
Obsoletes:     %{name}-client < %{version}-%{release}
Obsoletes:     %{name}-server < %{version}-%{release}

%{?systemd_requires}
%global __provides_exclude %{?__provides_exclude:%__provides_exclude|}^perl\\(Math::BigInt\\)
%global __provides_exclude_from %{?__provides_exclude_from:%{__provides_exclude_from}|}^%{perl_vendorarch}/auto/.*\\.so$
%global __requires_exclude ^perl\\(Dancer2\\)

%description
AMANDA, the Advanced Maryland Automatic Network Disk Archiver, is a
backup system that allows the administrator of a LAN to set up a single
master backup server to back up multiple hosts to a single large capacity
tape or disk drive. Amanda uses native tools (such as GNUtar, dump) for
backup and can back up a large number of workstations running multiple
versions of Unix/Mac OS X/Linux/Windows.

%package_help

%prep
%autosetup -p1 -n %{name}-%{version}

%build
./autogen

export MAILER=/bin/mail CFLAGS="$RPM_OPT_FLAGS -fPIE" LDFLAGS="$RPM_LD_FLAGS -pie"
%configure --enable-shared \
           --disable-rpath \
           --disable-dependency-tracking \
           --disable-installperms \
           --with-amdatadir=%{_localstatedir}/lib/amanda \
           --with-amlibdir=%{_libdir} \
           --with-amperldir=%{perl_vendorarch} \
           --with-index-server=%{indexserver} \
           --with-tape-server=%{tapeserver} \
           --with-config=%{defconfig} \
           --with-gnutar-listdir=%{_localstatedir}/lib/amanda/gnutar-lists \
           --with-smbclient=%{_bindir}/smbclient \
           --with-amandates=%{_localstatedir}/lib/amanda/amandates \
           --with-user=%amanda_user \
           --with-group=%amanda_group \
           --with-tmpdir=/var/log/amanda \
           --with-gnutar=/bin/tar \
           --with-ssh-security \
           --with-rsh-security \
           --with-bsdtcp-security \
           --with-bsdudp-security \
           --with-krb5-security

pushd perl
make maintainer-clean-am
popd

%make_build

%install
%make_install BINARY_OWNER=%(id -un) SETUID_GROUP=%(id -gn)

pushd %{buildroot}
mkdir -p ./%{_sysconfdir}/amanda
mkdir -p ./var/log/amanda/amandad
mkdir -p ./%{_localstatedir}/lib/amanda

install -p -m 644 -D %{SOURCE9} ./%{_unitdir}/amanda.socket
install -p -m 644 -D %{SOURCE10} ./%{_unitdir}/amanda@.service
install -p -m 644 -D %{SOURCE13} ./%{_unitdir}/amanda-udp.socket
install -p -m 644 -D %{SOURCE14} ./%{_unitdir}/amanda-udp.service
install -p -m 644 -D %{SOURCE15} ./%{_unitdir}/kamanda.socket
install -p -m 644 -D %{SOURCE16} ./%{_unitdir}/kamanda@.service
install -D %{SOURCE11}  ./%{_mandir}/man1/activate-devpay.1.gz
install -D %{SOURCE12}  ./%{_mandir}/man8/killpgrp.8
install -m 644 ./%{_sysconfdir}/amanda/amanda-security.conf ./%{_sysconfdir}/amanda-security.conf
popd

ln -s %{_libexecdir}/amanda/amandad %{buildroot}%{_sbindir}/amandad
ln -s amrecover.8.gz %{buildroot}%{_mandir}/man8/amoldrecover.8

pushd %{buildroot}
mv .%{_localstatedir}/lib/amanda/example .%{_sysconfdir}/amanda/%defconfig
rm -f .%{_sysconfdir}/amanda/%defconfig/xinetd*
rm -f .%{_sysconfdir}/amanda/%defconfig/inetd*
mkdir -p .%{_localstatedir}/lib/amanda/gnutar-lists
mkdir -p .%{_localstatedir}/lib/amanda/%defconfig/index
touch .%{_localstatedir}/lib/amanda/amandates
popd

rm -rf %{buildroot}%{_datadir}/amanda
%delete_la_and_a

%check
make check

%pre
%{_sbindir}/useradd -M -N -g %amanda_group -o -r -d %{_localstatedir}/lib/amanda -s /bin/bash \
        -c "Amanda user" -u 33 %amanda_user >/dev/null 2>&1 || :
%{_sbindir}/gpasswd -a %amanda_user tape >/dev/null 2>&1 || :

%post
%{?ldconfig}
%systemd_post amanda.socket amanda-udp.socket kamanda.socket

%preun
%systemd_preun amanda.socket amanda-udp.socket kamanda.socket

%postun
%{?ldconfig}
%systemd_postun_with_restart amanda.socket amanda-udp.socket kamanda.socket
%ldconfig_scriptlets client server

%files
%defattr(-,root,root)
%license COPYRIGHT
%doc AUTHORS README
%dir %{_sysconfdir}/amanda/
%{_sysconfdir}/amanda/amanda-security.conf
%config(noreplace) %{_sysconfdir}/amanda/%defconfig/*
%attr(0644, root, root) %config(noreplace) %{_sysconfdir}/amanda-security.conf

%{_sbindir}/*
%{_libdir}/*.so
%dir %{_libexecdir}/amanda
%{_libexecdir}/amanda/*
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/runtar
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/ambind
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/rundump
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/calcsize
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/killpgrp
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/application/amgtar
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/application/amstar
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/application/ambsdtar

%{_unitdir}/*.socket
%{_unitdir}/*.service
%{perl_vendorarch}/Amanda/
%{perl_vendorarch}/auto/Amanda/

%attr(02770,%amanda_user,%amanda_group) %dir /var/log/amanda
%attr(02770,%amanda_user,%amanda_group) %dir /var/log/amanda/amandad
%attr(-,%amanda_user,%amanda_group)     %dir %{_localstatedir}/lib/amanda/
%attr(-,%amanda_user,%amanda_group)     %dir %{_localstatedir}/lib/amanda/%defconfig/
%attr(-,%amanda_user,%amanda_group)     %dir %{_localstatedir}/lib/amanda/%defconfig/index
%attr(-,%amanda_user,%amanda_group)     %dir %{_localstatedir}/lib/amanda/template.d
%attr(-,%amanda_user,%amanda_group)     %config(noreplace) %{_localstatedir}/lib/amanda/template.d/*
%attr(-,%amanda_user,%amanda_group)     %{_localstatedir}/lib/amanda/gnutar-lists/
%attr(-,%amanda_user,%amanda_group)     %config(noreplace) %{_localstatedir}/lib/amanda/amandates

%files help
%defattr(-,root,root)
%doc NEWS
%{_mandir}/man*

%changelog
* Thu Mar 2 2023 caodongxia <caodongxia@h-partners.com> - 3.5.1-23
- Add install require sharutils 

* Tue Feb 28 2023 luopihui <luopihui@ncti-gba.cn> - 3.5.1-22
- Fix CVE-2022-37704 CVE-2022-37705

* Fri Nov 11 2022 caodongxia <caodongxia@h-partners.com> - 3.5.1-21
- fix hexencode test failed

* Mon Jun 20 2022 liyanan <liyanan32@h-partners.com> - 3.5.1-20
- Skip hexencode test case as it fails with new glib2

* Fri Jul 30 2021 Haiwei Li <lihaiwei8@huawei.com> - 3.5.1-19
- Fix multiple definition tu_debugging_enabled

* Sat Feb 29 2020 Ling Yang <lingyang2@huawei.com> - 3.5.1-18
- Fixed build error

* Fri Feb 14 2020 gulining<gulining1@huawei.com> - 3.5.1-17
- Package init
