Package sources for project systemsmanagement:cockpit are received through scmsync. This is not supported by the OBS frontend

File saptune.spec of Package saptune.39867

#
# spec file for package saptune
#
# Copyright (c) 2017-2025 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#


%if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
  %define saptune_buildvers 16
  %define saptune_config_file /var/lib/saptune/config/saptune
%else
  %define saptune_config_file /etc/sysconfig/saptune
  %if 0%{?sle_version} >= 150000
    %define saptune_buildvers 15
  %else
    %define saptune_buildvers 12
  %endif
%endif

%define GONS github.com/SUSE
%define SRCDIR   src/%{GONS}/%{name}
%if !%{defined _fillupdir}
  %define _fillupdir %{_localstatedir}/adm/fillup-templates
%endif

Name:           saptune
Version:        3.2.0
Release:        0
Summary:        Comprehensive system tuning management for SAP solutions
License:        GPL-3.0-only
Group:          System/Management
URL:            https://www.suse.com/products/sles-for-sap
Source0:        %{name}-%{version}.tgz
Source1:        rpmlintrc
%if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
Obsoletes:      sapconf
Provides:       sapconf
%endif
BuildRequires:  bash-completion
BuildRequires:  go
Requires:       logrotate
Requires:       sysstat
Requires:       cpupower
#/usr/bin/md5sum - coreutils
Requires:       coreutils
Requires:       uuidd
Requires:       vim
ExclusiveArch:  x86_64 ppc64le
%{?systemd_requires}
%if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
Requires:       systemd >= 257.3
Requires:       sysctl-logger
%else
%if 0%{?sle_version} >= 150000
Requires:       systemd >= 234-24.42
%else
Requires:       systemd >= 228-142.1
%endif
%endif
%if 0%{?sle_version} >= 150400
Requires:       sysctl-logger
%endif

%description
The utility adjusts system parameters such as kernel parameters and resource
limits to allow running various SAP solutions at satisfactory performance.
The utility can be used in place of sapconf.

%prep
mkdir -p %{SRCDIR}
cd %{SRCDIR}
tar xf %{SOURCE0}

%build
export GOPATH=$(pwd)
export GO111MODULE=off
cd %{SRCDIR}
%if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
  ./ospackage/usr/share/saptune/scripts/upd_man
%endif
gzip ospackage/man/saptune.8
gzip ospackage/man/saptune-note.5
gzip ospackage/man/saptune-solution.5
gzip ospackage/man/saptune.7
gzip ospackage/man/saptune-migrate.7
go build -buildmode=pie -ldflags "-X '%{GONS}/%{name}/actions.RPMVersion=%{version}' -X '%{GONS}/%{name}/system.RPMBldVers=%{saptune_buildvers}'"

%install
cd %{SRCDIR}
mkdir -p %{buildroot}/%{_sbindir}
install -m 0755 %{name} %{buildroot}/%{_sbindir}/
install -m 0755 ospackage/bin/* %{buildroot}%{_sbindir}/

# Sysconfig file / saptune config Template
%if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
    mkdir -p %{buildroot}/%{_datadir}/%{name}
    mkdir -p %{buildroot}/%{_localstatedir}/lib/%{name}/config
    install -m 0644 ospackage/etc/sysconfig/%{name} %{buildroot}/%{_datadir}/%{name}/saptuneTemplate.conf
%else
    mkdir -p %{buildroot}/%{_fillupdir}
    install -m 0644 ospackage/etc/sysconfig/%{name} %{buildroot}/%{_fillupdir}/sysconfig.%{name}
%endif

# json v1.1 schemata
mkdir -p %{buildroot}/%{_datadir}/%{name}/schemas/1.1
pushd ospackage/%{_datadir}/%{name}/schemas/1.1
for schemafiles in *.json; do
    install -m 0644 $schemafiles %{buildroot}/%{_datadir}/%{name}/schemas/1.1
done
popd

# scripts location
mkdir -p %{buildroot}/%{_datadir}/%{name}/scripts
echo %{version} > %{buildroot}/%{_datadir}/%{name}/scripts/.updhelp
pushd ospackage/%{_datadir}/%{name}/scripts
for scriptfiles in *; do
    install -m 0755 $scriptfiles %{buildroot}/%{_datadir}/%{name}/scripts
done
popd

# note files location
mkdir -p %{buildroot}/%{_datadir}/%{name}/notes
pushd ospackage/%{_datadir}/%{name}/notes
for notefiles in *; do
    case $notefiles in
    3565382|3577842|1275776)
        # SLE16 notes
        %if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
            install -m 0644 $notefiles %{buildroot}/%{_datadir}/%{name}/notes/
        %endif
        ;;
    1984787|2205917|1557506)
        # SLE12 notes
        %if 0%{?sle_version} >= 120000 && 0%{?sle_version} < 150000
            install -m 0644 $notefiles %{buildroot}/%{_datadir}/%{name}/notes/
        %endif
        ;;
    2578899|2684254)
        # SLE15 notes
        %if 0%{?sle_version} >= 150000
            install -m 0644 $notefiles %{buildroot}/%{_datadir}/%{name}/notes/
        %endif
        ;;
    *)
        install -m 0644 $notefiles %{buildroot}/%{_datadir}/%{name}/notes/
        ;;
    esac
done
popd

# NoteTemplate file
%if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
    install -m 0644 ospackage/%{_datadir}/%{name}/NoteTemplate.conf_16 %{buildroot}/%{_datadir}/%{name}/NoteTemplate.conf
%else
    install -m 0644 ospackage/%{_datadir}/%{name}/NoteTemplate.conf %{buildroot}/%{_datadir}/%{name}/NoteTemplate.conf
%endif

# solution definition
mkdir -p %{buildroot}/%{_datadir}/%{name}/sols
%if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
    pushd ospackage/%{_datadir}/%{name}/sols_16
%else
  %if 0%{?sle_version} < 150000
    pushd ospackage/%{_datadir}/%{name}/sols
  %else
    pushd ospackage/%{_datadir}/%{name}/sols_15
  %endif
%endif
for solfiles in *; do
    install -m 0644 $solfiles %{buildroot}/%{_datadir}/%{name}/sols/
done
popd
# SolutionTemplate file
install -m 0644 ospackage/%{_datadir}/%{name}/SolutionTemplate.conf %{buildroot}/%{_datadir}/%{name}/SolutionTemplate.conf

# deprecated notes and solutions
mkdir -p %{buildroot}/%{_datadir}/%{name}/deprecated
pushd ospackage/%{_datadir}/%{name}/deprecated
for deprfiles in *; do
    %if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
        rm -f %{buildroot}/%{_datadir}/%{name}/notes/$deprfiles
    %else
        install -m 0644 $deprfiles %{buildroot}/%{_datadir}/%{name}/deprecated/
    %endif
done
popd

# vendor file location
mkdir -p %{buildroot}/%{_sysconfdir}/%{name}/extra

# override file location
mkdir -p %{buildroot}/%{_sysconfdir}/%{name}/override

# systemd service file
mkdir -p %{buildroot}%{_unitdir}/systemd/system/ %{buildroot}%{_sbindir}/
%if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
    # add systemd hardening starting 16.0
    install -m 0644 ospackage/svc/saptune.service %{buildroot}%{_unitdir}/
%else
  %if 0%{?sle_version} > 150300
    # add systemd hardening starting 15SP4 
    install -m 0644 ospackage/svc/saptune.service_15 %{buildroot}%{_unitdir}/saptune.service
  %else
    # systemd < 244 does not support all hardening options
    install -m 0644 ospackage/svc/saptune.service_no_hardening %{buildroot}%{_unitdir}/saptune.service
  %endif
%endif
ln -s /usr/sbin/service %{buildroot}%{_sbindir}/rcsaptune

# manual pages
mkdir -p %{buildroot}/%{_mandir}/man5
install -m 0644 ospackage/man/saptune-note.5.gz %{buildroot}/%{_mandir}/man5/
install -m 0644 ospackage/man/saptune-solution.5.gz %{buildroot}/%{_mandir}/man5/
mkdir -p %{buildroot}/%{_mandir}/man7
install -m 0644 ospackage/man/saptune.7.gz %{buildroot}/%{_mandir}/man7/
%if 0%{?suse_version} < 1600 && !0%{?is_opensuse}
    install -m 0644 ospackage/man/saptune-migrate.7.gz %{buildroot}/%{_mandir}/man7/
%endif
mkdir -p %{buildroot}/%{_mandir}/man8
install -m 0644 ospackage/man/saptune.8.gz %{buildroot}/%{_mandir}/man8/

# bash-completion
mkdir -p %{buildroot}/%{_datadir}/bash-completion/completions
%if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
    # bash-completion for 16.0
    install -m 0644 ospackage/%{_datadir}/bash-completion/completions/%{name}.completion_16 %{buildroot}%{_datadir}/bash-completion/completions/%{name}
%else
    # bash-completion for 12 and 15
    install -m 0644 ospackage/%{_datadir}/bash-completion/completions/%{name}.completion %{buildroot}%{_datadir}/bash-completion/completions/%{name}
%endif

# supportconfig plugin
install -D -m 755 ospackage/usr/lib/supportconfig/plugins/%{name} %{buildroot}/usr/lib/supportconfig/plugins/%{name}

# working and staging location
mkdir -p %{buildroot}/%{_localstatedir}/lib/%{name}/working
mkdir -p %{buildroot}/%{_localstatedir}/lib/%{name}/staging/latest

# logdir
mkdir -p %{buildroot}/%{_localstatedir}/log/%{name}

# logrotate
mkdir -p %{buildroot}/%{_sysconfdir}/logrotate.d
install -m 0644 ospackage/logrotate/%{name} %{buildroot}/%{_sysconfdir}/logrotate.d/

%pre
%service_add_pre saptune.service
if [ $1 -ne 1 ]; then
    # package update
    if [ -f /var/lib/saptune/config/saptune ]; then
        # SLE16, package version 3.2 or later
        :
    elif [ -f /etc/sysconfig/saptune ]; then
        # SLE12/SLE15 (or update to SLE16, anything needed? TODO)
        NOTEDIR=/usr/share/saptune/notes
        if [ ! -d ${NOTEDIR} ]; then
            # installed package version is < 2.0, update v1 to v3
            # indicated by missing directory /usr/share/saptune/notes

            # only change version to '1' (migration), if saptune is really used
            # so check, if a solution or a note is defined
            if (grep '^TUNE_FOR_SOLUTIONS[[:space:]]*=[[:space:]]*""' /etc/sysconfig/saptune >/dev/null 2>&1) && (grep '^TUNE_FOR_NOTES[[:space:]]*=[[:space:]]*""' /etc/sysconfig/saptune >/dev/null 2>&1); then
                echo "saptune NOT configured and NOT used - version will be set to '3'"
            else
                echo "ATTENTION: saptune Version 1 is currently configured and used - but this version of saptune is no longer supported. Please migrate to Version 3 after the package update is done. saptune will stop working"

                # to allow a migration from v1 to v3 after the installation, we need to preserve some 'old' data.
                touch /run/update_v1tov3_saptune_inst || :
                # preserve 'old' BOBJ and ASE note definition files for saptune
                # version 1 compatibility
                if [ -f /etc/saptune/extra/SAP_BOBJ-SAP_Business_OBJects.conf ]; then
                    cp /etc/saptune/extra/SAP_BOBJ-SAP_Business_OBJects.conf /etc/saptune/extra/SAP_BOBJ_n2c.conf
                fi
                if [ -f /etc/saptune/extra/SAP_ASE-SAP_Adaptive_Server_Enterprise.conf ]; then
                    cp /etc/saptune/extra/SAP_ASE-SAP_Adaptive_Server_Enterprise.conf /etc/saptune/extra/SAP_ASE_n2c.conf
                fi
            fi
        else
            # package version 2.0 or later
            # check SAPTUNE_VERSION
            stvers=$(grep ^SAPTUNE_VERSION= /etc/sysconfig/saptune | awk -F '"' '{ print $2 }')
            if [ "$stvers" == 1 ]; then
                # check, if saptune is really used
                # so check, if a solution or a note is defined
                if (grep '^TUNE_FOR_SOLUTIONS[[:space:]]*=[[:space:]]*""' /etc/sysconfig/saptune >/dev/null 2>&1) && (grep '^TUNE_FOR_NOTES[[:space:]]*=[[:space:]]*""' /etc/sysconfig/saptune >/dev/null 2>&1); then
                    # saptune NOT configured and NOT used
                    :
                else
                    echo "ATTENTION: saptune currently running in Version 1 compatibility mode. Please migrate to Version 3 after the package update is done."
                fi
            fi
            if [ ! -d /var/lib/saptune/working/sols ]; then
                # installed package version is 2.x, update v2 to v3, save 'old' solution definition file
                cp /usr/share/saptune/solutions /var/lib/saptune/.v2_solutions
            fi
            # special fix only for 3.0.0 installations
            if [ -d /var/lib/saptune/working/sols ] && [ ! -f /usr/share/saptune/scripts/.updhelp ]; then
                touch /run/update_fix_300_saptune_inst || :
            fi
        fi

        # to prevent saptune related tuned error messages anytime after this
        # saptune package installation switch off tuned to remove the 'active'
        # saptune profile
        # 'tuned-adm off' is sadly the only possibility to remove an 'active'
        # saptune profile
        systemctl -q is-active tuned && [[ $(cat /etc/tuned/active_profile 2>/dev/null) == saptune ]] && (echo "found active tuned with saptune profile"; touch /run/saptune_is_active_in_tuned; tuned-adm off) || :
        # if the tuned profile is saptune, try to switch off tuned
        # if 'tuned-adm off' before had worked, the profile is empty
        # if not try again
        [[ $(cat /etc/tuned/active_profile 2>/dev/null) == saptune ]] && (echo "found saptune as tuned profile, try to switch off tuned"; touch /run/saptune_is_active_in_tuned; tuned-adm off || systemctl stop tuned.service; > /etc/tuned/active_profile) || :
        # if the tuned profile is still saptune, try to override the file
        [[ $(cat /etc/tuned/active_profile 2>/dev/null) == saptune ]] && (echo "found saptune as tuned profile, override /etc/tuned/active_profile"; touch /run/saptune_is_active_in_tuned; systemctl stop tuned.service; > /etc/tuned/active_profile) || :
    fi
else
    # initial installation
    # check, if old config files from a former installation still exist
    if [ -f /etc/sysconfig/saptune ]; then
        mv /etc/sysconfig/saptune /etc/sysconfig/saptune.rpmold || :
    fi
fi
# action trigger for SLE16 initial installation
# and update plain SLE15 to plain SLE16 (update from sapconf to saptune)
# and update SLES4SAP15 to SLES4SAP16 (update from active sapconf to now active saptune)
# check os-release, as this is only valid for >=SLE16
osvers=$(grep '^VERSION_ID[[:space:]]*=' /etc/os-release | awk -F '"' '{ print $2 }') || :
if [[ "$osvers" =~ 16\..* ]]; then
    # check if sapconf is installed
    if rpm -q sapconf --quiet; then
        echo "found installed sapconf"
        touch /run/sapconf_pre_inst
        # check for enabled and/or active sapconf.service
        # to prevent a degraded systemd system status - stop and disable sapconf
        if systemd-detect-virt -q -r; then
            # running in chroot - systemctl is-active/start/stop does not
            # work as expected - acts as 'NOPs' and returns '0',
            # but enabel/disable does work
            ls /etc/systemd/system/multi-user.target.wants/sapconf.service >/dev/null 2>&1 && sc_enabled="enabled" || sc_enabled="disabled"
            if [[ $(ls /run/sapconf/active 2>/dev/null) || $(ls /run/sapconf_act_profile 2>/dev/null) ]] || ([[ $(ls /var/lib/sapconf/act_profile 2>/dev/null) ]] && [ -s /var/lib/sapconf/act_profile ]); then
                echo "found active sapconf.service"
                touch /run/sapconf_pre_actss_found || :
            fi
        else
            sc_enabled=$(systemctl is-enabled sapconf.service 2>/dev/null) || :
            sc_active=$(systemctl is-active sapconf.service 2>/dev/null) || :
            ([[ "$sc_active" == "activating" ]] || [[ "$sc_active" == "deactivating" ]] || [[ "$sc_active" == "active" ]]) && (echo "found active sapconf.service ($sc_active); stopping now..."; touch /run/sapconf_pre_actss_found; systemctl stop sapconf.service 2>/dev/null) || :
        fi
        ([[ "$sc_enabled" == "enabled" ]]) && (echo "found enabled sapconf.service, disable now..."; touch /run/sapconf_pre_enblss_found; systemctl disable sapconf.service 2>/dev/null) || :
        # check for configured saptune
        if [ ! -f /etc/sysconfig/saptune ] || ((grep '^TUNE_FOR_SOLUTIONS[[:space:]]*=[[:space:]]*""' /etc/sysconfig/saptune >/dev/null 2>&1) && (grep '^TUNE_FOR_NOTES[[:space:]]*=[[:space:]]*""' /etc/sysconfig/saptune >/dev/null 2>&1)); then
            echo "saptune not configured"
            touch /run/saptune_pre_not_configured || :
        else
            echo "saptune has configured notes or solutions"
            touch /run/saptune_pre_configured || :
        fi
    else
        touch /run/sapconf_not_inst
    fi
fi
touch /run/saptune_during_pkg_inst || :


%post
if [ -f /usr/share/saptune/saptuneTemplate.conf ]; then
    # SLE16
    if [ ! -f /var/lib/saptune/config/saptune ]; then
        # saptune config file missing - copy from template
        cp /usr/share/saptune/saptuneTemplate.conf /var/lib/saptune/config/saptune
    fi
    if [ -f /etc/sysconfig/saptune ]; then
        # update from 12/15 to 16
        # move old config file away
        mv /etc/sysconfig/saptune /var/lib/saptune/config/old_custom_saptune_config || :
    fi
    # update saptune config file with new entries, if needed.
    # Do not change existing ones
    # update config 12/15 to 16 (special task needed only once!)
    /usr/share/saptune/scripts/upd_helper updateConf || :
else
    # SLE12/15
    %fillup_only -n saptune

    # workaround for the missing directory.
    mkdir -p /etc/security/limits.d
fi
saptune_conf=%{saptune_config_file}
saptune_build=%{saptune_buildvers}
echo "saptune_conf $saptune_conf"
echo "saptune buildvers $saptune_build"

# cleanup 'typo' directory (bsc#1215969)
rm -rf /varlog || :

# handling of working area is the same for initial install or update
# initial install or update from v1 or v2 - STAGING is 'false' by default
# the entire content of package area gets copied directly to the working area
# which is empty at that state.
# update from v3 or later - STAGING may be 'true'
staging=$(grep ^STAGING= "$saptune_conf" | awk -F '"' '{ print $2 }')
if [ "$staging" == "true" ]; then
    # handle staging area and DON'T touch the working area
    touch /run/update_saptune_staging_area || :
else
    # staging is NOT active, same behavior as with v2
    # adjust the notes of an enabled solution, if needed
    /usr/share/saptune/scripts/upd_helper enabledSol || :
    if [ -f /var/lib/saptune/.v2_solutions ]; then
        # remove no longer needed old solution definition file
        rm -f /var/lib/saptune/.v2_solutions || :
    fi

    # set up working area
    if [ -d /var/lib/saptune/working/notes ] || [ -d /var/lib/saptune/working/sols ]; then
        rm -rf /var/lib/saptune/working/* || :
    fi
    mkdir -p /var/lib/saptune/working/notes || :
    cp /usr/share/saptune/notes/* /var/lib/saptune/working/notes || :
    mkdir -p /var/lib/saptune/working/sols || :
    cp /usr/share/saptune/sols/* /var/lib/saptune/working/sols || :
    # clean up of working area needed (deleted notes/sols) as the package area
    # still contains all notes/solutions from the old package version
    # removal of files no longer part of the package is done later by rpm
fi

if [ $1 -ne 1 ]; then
    # package update
    if [ ! -f /var/lib/saptune/config/saptune ]; then
        # SLE12/15
        # rewrite saptune version in /etc/sysconfig/saptune as fillup will not
        # change variables
        sed -i 's/SAPTUNE_VERSION="2"/SAPTUNE_VERSION="3"/' /etc/sysconfig/saptune || :
        if [ -f /run/update_v1tov3_saptune_inst ]; then
            # update from v1 to v3, same as v1 to v2 as nothing changed in v1
            # step is needed to support migration after package update
            /usr/share/saptune/scripts/upd_helper v1tov2pi || :
        else
            # update from v2 to v2 or higher, call update helper script in
            # posttrans
            touch /run/update_sle12tosel15_saptune_inst || :
            # clean up some leftover files from older saptune v2 versions
            /usr/share/saptune/scripts/upd_helper cleanup || :
        fi
        # special fix for update from 3.0.0 only
        if [ -f /run/update_fix_300_saptune_inst ]; then
            rm -f /run/update_fix_300_saptune_inst || :
            /usr/share/saptune/scripts/upd_helper fix_300 || :
        fi
    else
        # on SLE16 started with v3.2
        # call update helper script in posttrans
        touch /run/update_tosle16_saptune_inst || :
    fi
else
    # initial install
    touch /run/initial_saptune_inst || :
fi
%service_add_post saptune.service

%preun
saptune_conf=%{saptune_config_file}
%service_del_preun saptune.service
test -n "$FIRST_ARG" || FIRST_ARG=$1
if [ $FIRST_ARG -eq 0 ]; then
    # Package removal, not upgrade
    stvers=$(grep ^SAPTUNE_VERSION= "$saptune_conf" | awk -F '"' '{ print $2 }')
    # revert settings
    if (grep '^TUNE_FOR_SOLUTIONS[[:space:]]*=[[:space:]]*""' $saptune_conf >/dev/null 2>&1) && (grep '^TUNE_FOR_NOTES[[:space:]]*=[[:space:]]*""' $saptune_conf >/dev/null 2>&1); then
        # saptune not configured and not used - nothing to do
        :
    else
        # saptune configured and used - revert settings to clean up the system
        if [ "$stvers" == 1 ]; then
            saptune daemon revert >/dev/null 2>&1 || :
        else
            saptune service revert >/dev/null 2>&1 || :
        fi
    fi

    # to suppress error messages from tuned, if the current active profile is
    # the removed saptune profile
    (systemctl -q is-active tuned && [[ $(cat /etc/tuned/active_profile 2>/dev/null) == saptune ]] ) && (tuned-adm off; /usr/sbin/saptune daemon stop >/dev/null 2>&1) || :
    # if the tuned profile is saptune, try to switch off tuned
    # if 'tuned-adm off' before had worked, the profile is empty
    # if not try again
    [[ $(cat /etc/tuned/active_profile 2>/dev/null) == saptune ]] && (> /etc/tuned/active_profile) || :

    # clean up saved states left over
    rm -rf /run/saptune/parameter/* /run/saptune/sections/* /run/saptune/saved_state/* || :
    # clean up working and staging area
    rm -rf /var/lib/saptune/staging/latest/* /var/lib/saptune/working/* /var/lib/saptune/working/.tmbackup || :
    # preserve 'old' BOBJ and ASE note definition files for saptune
    # version 1 compatibility
    if [ -f /etc/saptune/extra/SAP_BOBJ-SAP_Business_OBJects.conf ]; then
       echo "warning: /etc/saptune/extra/SAP_BOBJ-SAP_Business_OBJects.conf saved as /etc/saptune/extra/SAP_BOBJ-SAP_Business_OBJects.rpmsave"
        mv /etc/saptune/extra/SAP_BOBJ-SAP_Business_OBJects.conf /etc/saptune/extra/SAP_BOBJ-SAP_Business_OBJects.rpmsave || :
    fi
    if [ -f /etc/saptune/extra/SAP_ASE-SAP_Adaptive_Server_Enterprise.conf ]; then
	echo "warning: /etc/saptune/extra/SAP_ASE-SAP_Adaptive_Server_Enterprise.conf saved as /etc/saptune/extra/SAP_ASE-SAP_Adaptive_Server_Enterprise.conf.rpmsave"
        mv /etc/saptune/extra/SAP_ASE-SAP_Adaptive_Server_Enterprise.conf /etc/saptune/extra/SAP_ASE-SAP_Adaptive_Server_Enterprise.conf.rpmsave || :
    fi
    # preserve saptune configuration, if saptune was used
    # so check, if a solution or a note is defined
    if (grep '^TUNE_FOR_SOLUTIONS[[:space:]]*=[[:space:]]*""' $saptune_conf >/dev/null 2>&1) && (grep '^TUNE_FOR_NOTES[[:space:]]*=[[:space:]]*""' $saptune_conf >/dev/null 2>&1) && (grep '^NOTE_APPLY_ORDER[[:space:]]*=[[:space:]]*""' $saptune_conf >/dev/null 2>&1); then
       rm -f $saptune_conf || :
    else
       if [ -f /var/lib/saptune/config/saptune ]; then
           # SLE16
           echo "warning: /var/lib/saptune/config/saptune saved as /etc/saptune/saptune_conf.rpmsave"
           mv /var/lib/saptune/config/saptune /etc/saptune/saptune_conf.rpmsave || :
       else
           echo "warning: /etc/sysconfig/saptune saved as /etc/sysconfig/saptune.rpmsave"
           mv /etc/sysconfig/saptune /etc/sysconfig/saptune.rpmsave || :
       fi
    fi
fi

%postun
test -n "$FIRST_ARG" || FIRST_ARG=$1
if [ $FIRST_ARG -eq 0 ]; then
    # Package removal, not upgrade
    %service_del_postun saptune.service
fi

%posttrans -p /bin/bash
# Use a real bash script with an explicit "exit 0" at the end to be by default fail safe
# an explicit "exit 1" must be use to enforce package install/upgrade/erase failure where needed

# Begin refresh systemd units and clean up possibly obsolete systemd units
# The following is a generic way how to refresh and/or clean up systemd units.
# A systemd unit may need a refresh after updating a package when the new package
# had installed a changed systemd unit file for an enabled systemd unit.
# A systemd unit may become obsolete by updating a package (see bnc#904215).
# A systemd unit is considered to have become obsolete when the systemd
# symlink /etc/systemd/system/.../unit_name -> /path/to/unit_file is broken.
# When during package update the new package does no longer provide a unit file
# then the systemd symlink becomes broken after the files of the old package
# had been actually removed by RPM.
# According to /usr/share/doc/packages/rpm/manual/triggers and according
# to https://en.opensuse.org/openSUSE:Packaging_scriptlet_snippets#Scriptlet_Ordering
# and http://fedoraproject.org/wiki/Packaging:ScriptletSnippets#Scriptlet_Ordering
# from the new package only "posttrans of new package" is run after "removal of old package"
# so that the new package must do the clean up as RPM posttrans scriptlet.
if systemctl --quiet is-enabled saptune.service 2>/dev/null; then
    # Refresh still valid enabled systemd units and clean up possibly obsoleted systemd units:
    # Enforce systemd to use the current unit file which is usually the unit file of the new package
    # but also in case of custom units (that use other unit files) a "reenable" won't hurt because
    # "reenable" does not implicitly stop a running service which is "the right thing" because
    # a RPM package installation must not automatically disrupt (restart) a running service.
    # Using "--force reenable" is essential to clean up possibly conflicting/broken symlinks.
    # (without "|| :" build fails with "Failed to get D-Bus connection: No connection to service manager. posttrans script ... failed"):
    systemctl --quiet --force reenable saptune.service 2>/dev/null || :
else
    # Refresh still valid disabled systemd units and clean up possibly obsoleted systemd units:
    # First using "--force reenable" is essential to clean up possibly conflicting/broken symlinks
    # because there is no "--force disable" that would clean up possibly conflicting/broken symlinks
    # see https://bugzilla.opensuse.org/show_bug.cgi?id=904215#c34
    # so that first the unit has a clean state and then it is set back to disabled (as it was before).
    # If a disabled systemd unit has become obsoleted, "systemctl --force reenable" will clean it up
    # which means the unit gets removed and the subsequent "systemctl disable" will do nothing.
    # (without "|| :" build fails with "Failed to get D-Bus connection: No connection to service manager. posttrans script ... failed"):
    systemctl --quiet --force reenable saptune.service 2>/dev/null || :
    systemctl --quiet disable saptune.service 2>/dev/null || :
fi
rm -f /run/saptune_during_pkg_inst
if [ ! -f /var/lib/saptune/config/saptune ]; then
    # only for SLE12/15
    if [ -f /run/update_v1tov3_saptune_inst ]; then
        rm -f /run/update_v1tov3_saptune_inst || :
        # get back custom note definition files for BOBJ and/or ASE
        # needed for migration, if customer had applied these notes
        /usr/share/saptune/scripts/upd_helper v1tov2pt || :
    else
        # cleanup of old saptune v1 sysconfig files
        # leftover from customer migration
        for file in saptune-note-SUSE-GUIDE-01 saptune-note-1275776 saptune-note-SUSE-GUIDE-02 saptune-note-1557506; do
            if [ -f /etc/sysconfig/${file} ]; then
                rm -f /etc/sysconfig/${file}
            fi
        done
    fi
fi
if [ -f /run/update_sle12tosel15_saptune_inst ]; then
    rm -f /run/update_sle12tosel15_saptune_inst || :
    # check for SAP Note name changes between SLE12 and SLE15
    /usr/share/saptune/scripts/upd_helper sle12to15pt || :
fi
if [ -f /run/update_tosle16_saptune_inst ]; then
    rm -f /run/update_tosle16_saptune_inst || :
    # check for SAP Note name changes between SLE12/15 and SLE16
    /usr/share/saptune/scripts/upd_helper sleto16pt || :
fi
if [ -f /run/update_saptune_staging_area ]; then
    rm -f /run/update_saptune_staging_area || :
    # handle staging area and DON'T touch the working area, needs to run
    # after 'upd_helper sle12to15pt' and 'upd_helper sleto16pt'
    /usr/share/saptune/scripts/upd_helper staging || :
fi

if [ -f /var/lib/saptune/config/saptune ]; then
    # SLE16
    # add base tuning, if we are in initial install and not update.
    if [ -f /run/initial_saptune_inst ]; then
        # package initial install
        saptuneBaseTuning=true
        # check sapconf state
        if [ -f /run/sapconf_pre_inst ]; then
            # sapconf package was installed before
            if [ ! -f /run/sapconf_pre_enblss_found ] && [ ! -f /run/sapconf_pre_actss_found ]; then
                echo "Attention: sapconf was installed, but sapconf service was not active and not enabled before, don't activate saptune base tuning"
                saptuneBaseTuning=false
            fi
            if [ -f /run/sapconf_pre_enblss_found ] || [ -f /run/sapconf_pre_actss_found ]; then
                echo "sapconf service was active/enabled before, activate saptune base tuning"
            fi
        fi
        if [ -f /run/sapconf_not_inst ]; then
            echo "Attention: sapconf was not installed, activate saptune base tuning"
        fi
        echo "saptune initial installation (on 16)"
    else
        # package update
        saptuneBaseTuning=false
        if ([ -f /run/sapconf_pre_enblss_found ] || [ -f /run/sapconf_pre_actss_found ]) && [ -f /run/saptune_pre_not_configured ]; then
            echo "sapconf service was active/enabled before and saptune was not configured, activate saptune base tuning"
            saptuneBaseTuning=true
            echo "saptune update installation (to 16)"
        fi
    fi

    # check, if saptune base tuning is desired
    if [ "$saptuneBaseTuning" == "true" ]; then
        echo "activate saptune base tuning - apply SAP_Base solution"
        systemctl start saptune.service || :
        saptune solution apply SAP_Base >/dev/null 2>&1 || :
    fi
else
    # SLES12/15
    if [ -f /run/saptune_is_active_in_tuned ]; then
        # cleanup 'saptune with tuned is active' indicator
        rm -f /run/saptune_is_active_in_tuned || :
        # if saptune with tuned support was used/active (in v2 mode)
        # stop and disable tuned service
        # enable and start saptune service
        # (jsc#SLE-10987 decision)
        (systemctl stop tuned.service; systemctl disable tuned.service; systemctl enable saptune.service; systemctl start saptune.service) || :
    fi
    # bsc#1194688 - sometimes the tuned active profile is still 'saptune' even that
    # the profile no longer exists. Try to clear.
    [[ $(cat /etc/tuned/active_profile 2>/dev/null) == saptune ]] && (> /etc/tuned/active_profile) || :
fi
# cleanup
rm -f /run/initial_saptune_inst /run/sapconf_not_inst /run/sapconf_pre_inst || :
rm -f /run/sapconf_pre_enblss_found /run/sapconf_pre_actss_found || :
rm -f /run/saptune_pre_not_configured /run/saptune_pre_configured || :

exit 0

%files
%defattr(-,root,root)
%{_sbindir}/%{name}
%{_sbindir}/saptune_check
%{_sbindir}/rcsaptune
%{_unitdir}/saptune.service
%{_mandir}/man5/*
%{_mandir}/man7/*
%{_mandir}/man8/*
%dir %{_sysconfdir}/%{name}
%dir %{_sysconfdir}/%{name}/extra
%dir %{_sysconfdir}/%{name}/override
%dir %{_datadir}/%{name}
%dir %{_datadir}/%{name}/notes
%dir %{_datadir}/%{name}/sols
%dir %{_datadir}/%{name}/schemas
%dir %{_datadir}/%{name}/schemas/1.1
%dir %{_datadir}/%{name}/scripts
%{_datadir}/%{name}/notes/*
%{_datadir}/%{name}/sols/*
%if 0%{?suse_version} < 1600 && !0%{?is_opensuse}
    # SLE12 and SLE15 config
    %dir %{_datadir}/%{name}/deprecated
    %{_datadir}/%{name}/deprecated/*
%endif
%{_datadir}/%{name}/schemas/1.1/*
%{_datadir}/%{name}/scripts/*
%{_datadir}/%{name}/scripts/.updhelp
%{_datadir}/%{name}/NoteTemplate.conf
%{_datadir}/%{name}/SolutionTemplate.conf
%{_datadir}/bash-completion/completions/%{name}
%dir %{_prefix}/lib/supportconfig
%dir %{_prefix}/lib/supportconfig/plugins
%{_prefix}/lib/supportconfig/plugins/%{name}
%dir %{_localstatedir}/lib/%{name}
%dir %{_localstatedir}/lib/%{name}/working
%dir %{_localstatedir}/lib/%{name}/staging/latest
%dir %{_localstatedir}/lib/%{name}/staging
%dir %{_localstatedir}/log/%{name}
%if 0%{?suse_version} >= 1600 && !0%{?is_opensuse}
    # SLE16 config
    %{_datadir}/%{name}/saptuneTemplate.conf
    %dir %{_localstatedir}/lib/%{name}/config
    %attr(644,root,root) %ghost %{_localstatedir}/lib/%{name}/config/saptune
%else
    # SLE12 and SLE15 config
    %{_fillupdir}/sysconfig.%{name}
%endif
#noreplace - to get saptune.rpmnew, if file on disk has changed
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
# Created at run-time by saptune executable
%attr(0755,root,root) %ghost %dir %{_localstatedir}/lib/%{name}/working/notes
%attr(0755,root,root) %ghost %dir %{_localstatedir}/lib/%{name}/working/sols

%changelog
openSUSE Build Service is sponsored by