%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.3
Release:       1
Summary:       A backup solution over network to disk
License:       BSD and GPLv3+ and GPLv2+ and GPLv2
URL:           http://www.amanda.org
Source0:       https://github.com/zmanda/amanda/archive/tag-community-%{version}/amanda-%{version}.tar.gz
Source1:       amanda.crontab
Source4:       disklist
Source8:       amandahosts
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

Patch1:        amanda-3.1.1-xattrs.patch
Patch2:        amanda-3.1.1-tcpport.patch
Patch3:        amanda-3.2.0-config-dir.patch
# Don't mention xinetd files in amserverconfig (#1460763)
Patch4:        amanda-3.4.5-no-xinetd.patch
# Support tirpc
Patch5:        patch-tirpc.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 rpcgen gnuplot
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}-tag-community-%{version}
sed -i "s/3.5.2/3.5.3/g" 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 \
           --disable-static \
	   --with-amandahosts \
           --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 -m 600 %SOURCE8 $RPM_BUILD_ROOT%{_localstatedir}/lib/amanda/.amandahosts
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
cp ${RPM_SOURCE_DIR}/amanda.crontab .%{_sysconfdir}/amanda/crontab.sample
cp ${RPM_SOURCE_DIR}/disklist .%{_sysconfdir}/amanda/%defconfig
cp ${RPM_SOURCE_DIR}/disklist .%{_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
					 
%license COPYRIGHT COPYRIGHT.BSD
%doc ChangeLog README.md ReleaseNotes
%{_unitdir}/amanda@.service
%{_unitdir}/amanda.socket
%{_unitdir}/amanda-udp.service
%{_unitdir}/amanda-udp.socket
%{_unitdir}/kamanda@.service
%{_unitdir}/kamanda.socket

			 
			   
%dir %{_libexecdir}/amanda
%{_libexecdir}/amanda/amandad
%{_libexecdir}/amanda/amanda-sh-lib.sh
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/ambind
%{_libexecdir}/amanda/amndmjob
%{_libexecdir}/amanda/amcat.awk
%{_libexecdir}/amanda/amplot.awk
%{_libexecdir}/amanda/amplot.g
%{_libexecdir}/amanda/amplot.gp
%{_libexecdir}/amanda/ndmjob

%{_sbindir}/amandad
%{_sbindir}/amaespipe
%{_sbindir}/amarchiver
%{_sbindir}/amcrypt
%{_sbindir}/amcrypt-ossl
%{_sbindir}/amcrypt-ossl-asym
%{_sbindir}/amcryptsimple
%{_sbindir}/amgetconf
%{_sbindir}/amgpgcrypt
%{_sbindir}/amplot
%{_sbindir}/amssl

%{perl_vendorarch}/Amanda/
%{perl_vendorarch}/auto/Amanda/

%dir %{_sysconfdir}/amanda/
%dir %{_sysconfdir}/amanda/%defconfig

%attr(-,%amanda_user,%amanda_group)     %dir %{_localstatedir}/lib/amanda/
%attr(600,%amanda_user,%amanda_group)   %config(noreplace) %{_localstatedir}/lib/amanda/.amandahosts
%attr(02770,%amanda_user,%amanda_group) %dir /var/log/amanda
%attr(02770,%amanda_user,%amanda_group) %dir /var/log/amanda/amandad

%{_libdir}/libamdevice*.so
%{_libdir}/libamserver*.so
%{_libdir}/libamclient*.so
%{_libdir}/libamanda-*.so
%{_libdir}/libamanda.so
%{_libdir}/libamandad*.so
%{_libdir}/libamar*.so
%{_libdir}/libamglue*.so
%{_libdir}/libamxfer*.so
%{_libdir}/libndmjob*.so
%{_libdir}/libndmlib*.so

%{_libexecdir}/amanda/amdumpd
%{_libexecdir}/amanda/amcheck-device
%{_libexecdir}/amanda/amidxtaped
%{_libexecdir}/amanda/amindexd
%{_libexecdir}/amanda/amlogroll
%{_libexecdir}/amanda/amtrmidx
%{_libexecdir}/amanda/amtrmlog
%{_libexecdir}/amanda/driver
%{_libexecdir}/amanda/amadmin_perl
%{_libexecdir}/amanda/ambackupd
%{_libexecdir}/amanda/rest-server/
%{_libexecdir}/amanda/dumper
%{_libexecdir}/amanda/chunker
%{_libexecdir}/amanda/planner
%{_libexecdir}/amanda/taper

%{_sbindir}/activate-devpay
%{_sbindir}/amaddclient
%{_sbindir}/amadmin
%{_sbindir}/amcleanup
%{_sbindir}/amcleanupdisk
%{_sbindir}/amdevcheck
%{_sbindir}/amdump
%{_sbindir}/amfetchdump
%{_sbindir}/amflush
%{_sbindir}/amcheck
%{_sbindir}/amcheckdb
%{_sbindir}/amcheckdump
%{_sbindir}/amlabel
%{_sbindir}/amoverview
%{_sbindir}/amreport
%{_sbindir}/amrestore
%{_sbindir}/amrmtape
%{_sbindir}/amserverconfig
%{_sbindir}/amservice
%{_sbindir}/amstatus
%{_sbindir}/amtape
%{_sbindir}/amtapetype
%{_sbindir}/amtoc
%{_sbindir}/amvault
%{_sbindir}/amanda-rest-server
%{_sbindir}/amreindex

%config(noreplace) %{_sysconfdir}/amanda/crontab.sample
%config(noreplace) %{_sysconfdir}/amanda/%defconfig/*
%exclude %{_sysconfdir}/amanda/%defconfig/amanda-client.conf
%exclude %{_sysconfdir}/amanda/%defconfig/amanda-client-postgresql.conf

%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/*

%dir %{_libexecdir}/amanda/application/
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/application/ambsdtar
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/application/amgtar
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/application/amstar
%{_libexecdir}/amanda/application/amlog-script
%{_libexecdir}/amanda/application/ampgsql
%{_libexecdir}/amanda/application/amraw
%{_libexecdir}/amanda/application/amsamba
%{_libexecdir}/amanda/application/amsuntar
%{_libexecdir}/amanda/application/amzfs-sendrecv
%{_libexecdir}/amanda/application/amzfs-snapshot
%{_libexecdir}/amanda/application/script-email
%{_libexecdir}/amanda/application/amrandom
%{_libexecdir}/amanda/application/script-fail
%{_libexecdir}/amanda/restore
%{_libexecdir}/amanda/senddiscover

%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/calcsize
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/killpgrp
%{_libexecdir}/amanda/noop
%{_libexecdir}/amanda/patch-system
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/rundump
%attr(4750,root,%amanda_group) %{_libexecdir}/amanda/runtar
%{_libexecdir}/amanda/selfcheck
%{_libexecdir}/amanda/sendbackup
%{_libexecdir}/amanda/sendsize
%{_libexecdir}/amanda/teecount

%{_sbindir}/amdump_client
%{_sbindir}/amoldrecover
%{_sbindir}/amrecover
%{_sbindir}/ambackup

%{_sysconfdir}/amanda/amanda-security.conf

%config(noreplace) %{_sysconfdir}/amanda/%defconfig/amanda-client.conf
%config(noreplace) %{_sysconfdir}/amanda/%defconfig/amanda-client-postgresql.conf

%attr(-,%amanda_user,%amanda_group) %config(noreplace) %{_localstatedir}/lib/amanda/amandates
%attr(-,%amanda_user,%amanda_group) %{_localstatedir}/lib/amanda/gnutar-lists/
%attr(0644, root, root) %config(noreplace) %{_sysconfdir}/amanda-security.conf

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

%changelog
* Mon May 08 2023 chenchen <chen_aka_jan@163.com> - 3.5.3-1
- Update to version 3.5.3

* 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
