#!/bin/bash
# shellcheck disable=SC2188

#
# /usr/sbin/sapconf
#
# Copyright (c) 2015-2017 SUSE LINUX GmbH, Nuernberg, Germany.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Author:  Werner Fink <werner@suse.de>, 2007
# Author:  Howard Guo 2015
# Author:  Angela Briel <abriel@suse.com>, 2016
# Author:  Zsolt KALMAR <zkalmar@suse.com>, 2018

# /usr/sbin/sapconf traditionally performed automatic system tuning for SAP
# NetWeaver products.
# During the course of SLES 12 SP1 development, the system tuning
# implementation is moved entirely into tune daemon profiles.
# In additional to support SAP NetWeaver tuning, sapconf is extended to be
# also able to activate HANA tuning profile via the tune daemon.

# echo 'Warning: sapconf is deprecated. Please use system tuning daemon (tuned) instead.'
# This warning has been commented out accorording to bnc#1098352

if ! rpm -q tuned &> /dev/null; then
  echo 'Missing package "tuned". Please install the package as user root using command line "zypper install tuned" and then try again.'
  exit 1
fi

case "$1" in
  start|restart|try-restart)
    # To remain compatible with the original implementation of sapconf,
    # if neither sap-netweaver nor sap-hana is active, sap-netwever profile
    # will be activiated via tune daemon.
    # changed behaviour because of bsc#1026862
    # activate sap-netwever profile only if no other profile is active
    echo 'Forwarding action to tuned-adm.'
    # bsc#1026862
    #if [[ $(cat /etc/tuned/active_profile) != sap* ]]; then
    if [[ $(cat /etc/tuned/active_profile) == "" ]]; then
      profile=$(cat /var/lib/sapconf/last_profile 2> /dev/null)
      profile=${profile:-sap-netweaver}
      tuned-adm profile "$profile" &> /dev/null
    fi
    if ! systemctl status tuned &> /dev/null; then
      systemctl start tuned
    fi
    systemctl status tuned &> /dev/null && exit 0 || exit 1
  ;;

  reload)
    if [[ $(cat /etc/tuned/active_profile) == sap-* ]]; then
      /usr/bin/systemctl restart tuned.service
    fi
  ;;

  netweaver)
    # This is a new option, an extension to the traditional sapconf.
    # It activates NetWeaver tuning profile via the tune daemon.
    echo 'Forwarding action to tuned-adm.'
    tuned-adm profile sap-netweaver &> /dev/null
    if ! systemctl status tuned &> /dev/null; then
      systemctl start tuned
    fi
    systemctl status tuned &> /dev/null && exit 0 || exit 1
  ;;

  hana|b1)
    # This is a new option, an extension to the traditional sapconf.
    # It activates HANA tuning profile via the tune daemon.
    echo 'Forwarding action to tuned-adm.'
    tuned-adm profile sap-hana &> /dev/null
    if ! systemctl status tuned &> /dev/null; then
      systemctl start tuned
    fi
    systemctl status tuned &> /dev/null && exit 0 || exit 1
  ;;

  ase|sybase)
    # This is a new option, an extension to the traditional sapconf.
    # It activates SAP ASE tuning profile via the tune daemon.
    echo 'Forwarding action to tuned-adm.'
    tuned-adm profile sap-ase &> /dev/null
    if ! systemctl status tuned &> /dev/null; then
      systemctl start tuned
    fi
    systemctl status tuned &> /dev/null && exit 0 || exit 1
  ;;

  bobj)
    # This is a new option, an extension to the traditional sapconf.
    # It activates SAP Business OBJects tuning profile via the tune daemon.
    echo 'Forwarding action to tuned-adm.'
    tuned-adm profile sap-bobj &> /dev/null
    if ! systemctl status tuned &> /dev/null; then
      systemctl start tuned
    fi
    systemctl status tuned &> /dev/null && exit 0 || exit 1
  ;;

  stop)
    # To remain compatible with the original implementation of sapconf,
    # the stop action does nothing.
    # changed behaviour because of fate#325363
    # if the current tuned profile is a sapconf profile
    # stop tuned and remove sapconf profile from active tuned profil
    if [[ $(cat /etc/tuned/active_profile) == sap-* ]]; then
        /usr/bin/systemctl stop tuned >/dev/null 2>&1
        cat /etc/tuned/active_profile > /var/lib/sapconf/last_profile
        > /etc/tuned/active_profile
        systemctl status tuned &> /dev/null && exit 1
    fi
    exit 0
  ;;

  status)
    # Tell the status of tune daemon.
    systemctl status tuned && exit 0 || exit 1
  ;;

  *)
    echo "Usage: $0 {start|reload|hana|b1|ase|sybase|bobj|stop|status}"
    exit 1
  ;;

esac

