Name:           stansoft
Version:        10.0
Release:        1%{?dist}
Summary:        Standard Accounting Software

License:        AGPL-3.0-or-later
URL:            https://www.stansoft.org
Source0:        https://download.stansoft.org/files/%{name}-%{version}.tar.bz2
# man page
Source1:        https://download.stansoft.org/files/%{name}.1.gz
# Filter false-positive rpmlint errors and warnings
# explicit-lib-dependency libpq
#   false-positive on anything with lib in the package name
#   See https://bugzilla.redhat.com/show_bug.cgi?id=790869
# non-standard-uid, non-standard-gid /var/lib/stansoft
#   User stansoft owns the PostgreSQL database so must own this directory
Source2:        https://download.stansoft.org/files/%{name}.rpmlintrc

Requires:       postgresql-server
# rpmlint gives false-positive explicit-lib-dependency libpq, needed at runtime
Requires:       libpq
BuildRequires:  systemd-rpm-macros
BuildRequires:  gcc
BuildRequires:  aubit4gl-devel
BuildRequires:  postgresql-server
BuildRequires:  libpq-devel

%global aubit4glver %(rpm -q --qf '%%{VERSION}' aubit4gl | cut -d. -f1-3)


%description
Stansoft is a comprehensive double-entry financial accounting system.
It includes payroll for both the U.S. and UK. It is HMRC-recognised
for UK PAYE RTI payroll and MTD VAT returns.


%prep
%setup -q


%build
%if "%{getenv:AUBITDIR}" == ""
export AUBITDIR=%{_libdir}/aubit4gl
%endif

%configure
# The database must exist to compile so create it
%make_build createdb
# It does not compile with multiple threads
%make_build -j1
# Stop the database engine
%make_build stopdb


%install
rm -rf %{buildroot}%{_libdir}/%{name}
mkdir -p %{buildroot}%{_libdir}/%{name}
mkdir -p %{buildroot}%{_mandir}/man1
cp %{SOURCE1} %{buildroot}%{_mandir}/man1

%make_install

# Install the libraries into the system library directory
# Do not include unversioned libraries in non-devel package
cp -d %{buildroot}%{_libdir}/%{name}/lib/lib%{name}.so.* %{buildroot}%{_libdir}
rm -rf %{buildroot}%{_libdir}/%{name}/lib

# Install the binaries
mkdir -p %{buildroot}%{_bindir}
# Create a link to the startup script since it references the install dir
ln -sf ../..%{_libdir}/%{name}/%{name} %{buildroot}%{_bindir}/%{name}

# Fixup the path in the systemd file
sed -i 's:/opt:%{_libdir}:g' %{buildroot}%{_libdir}/%{name}/etc/%{name}.service

# Move the systemd file into place
mkdir -p %{buildroot}%{_unitdir}
# remove the link creation, move the file instead
#ln -sf ../../../..%%{_libdir}/%%{name}/etc/%%{name}.service %%{buildroot}%%{_unitdir}/%%{name}.service
mv %{buildroot}%{_libdir}/%{name}/etc/%{name}.service %{buildroot}%{_unitdir}

# Create the database directory
mkdir -p %{buildroot}%{_sharedstatedir}/%{name}

# Fixup the environment file with the correct path to the data directory
sed -i 's:$SSDIR/data:%{_sharedstatedir}/%{name}/data:g' %{buildroot}%{_libdir}/%{name}/etc/postgres.sh 

# Do not let the Stansoft install script change ownership or permissions
sed -i 's/\(chmod 775 "\$ssdir"\)/#\1/;
        s/\(chown -R stansoft:stansoft "\$ssdir"\)/#\1/' \
        %{buildroot}%{_libdir}/%{name}/installss

# docs are installed in the system location
rm -f %{buildroot}%{_libdir}/%{name}/README
rm -f %{buildroot}%{_libdir}/%{name}/changelog
rm -f %{buildroot}%{_libdir}/%{name}/COPYING
# Not including the pdf manual or fedora-review complains about size
# The man page has a web link to the manual
rm -rf %{buildroot}%{_libdir}/%{name}/doc


%pre
getent group %{name} >/dev/null || groupadd -f %{name} >/dev/null 2>&1 || :
getent passwd %{name} >/dev/null || useradd -g %{name} -m \
       -s /bin/bash %{name} >/dev/null 2>&1 || exit 0


%post
%systemd_post %{name}.service
semanage fcontext -a -t postgresql_db_t '%{_sharedstatedir}/%{name}(/.*)?'
restorecon %{_sharedstatedir}/%{name}


%preun
%systemd_preun %{name}.service


%postun
%systemd_postun_with_restart %{name}.service
semanage fcontext -d -t postgresql_db_t '%{_sharedstatedir}/%{name}(/.*)?'


%check
make check


%files
%license COPYING
%doc README changelog doc/USERDOC doc/PASSPORT doc/INFORMIX doc/FAQ
%dir %{_libdir}/%{name}
%dir %{_libdir}/%{name}/etc
%dir %{_libdir}/%{name}/bin
%dir %{_libdir}/%{name}/newdb
%dir %{_libdir}/%{name}/pytax
%{_libdir}/%{name}/etc/*
%{_libdir}/%{name}/bin/*
%{_libdir}/%{name}/newdb/*
%{_libdir}/%{name}/pytax/*
%{_libdir}/%{name}/%{name}
%{_libdir}/%{name}/installss
%{_libdir}/lib%{name}.so.*
%attr(700,%{name},%{name}) %{_sharedstatedir}/%{name}
%{_bindir}/%{name}
%{_unitdir}/%{name}.service
%{_mandir}/man1/*


%changelog
* Tue Jan 07 2025 Chad Lemmen <rpm@stansoft.org> - 10.0-1
- updated version

* Fri Mar 22 2024 Chad Lemmen <rpm@stansoft.org> - 9.1-1
- updated version

* Fri Feb 02 2024 Chad Lemmen <rpm@stansoft.org> - 9.0-1
- updated version

* Thu Dec 21 2023 Chad Lemmen <rpm@stansoft.org> - 8.6-1
- updated version
 
* Wed Oct 25 2023 Chad Lemmen <rpm@stansoft.org> - 8.5-1
- updated version

* Thu Feb 7 2019 Chad Lemmen <rpm@stansoft.org> - 7.15-1
- initial Fedora RPM packaging