#!/bin/bash
#
# /usr/sbin/sapconf
#
# Copyright (c) 2015 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 <hguo@suse.com>, 2015
#

# /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.'

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|reload)
    # 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.
    echo 'Forwarding action to tuned-adm.'
    if [[ $(cat /etc/tuned/active_profile) != sap* ]]; then
      tuned-adm profile sap-netweaver &> /dev/null
    fi
    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
  ;;

  stop)
    # To remain compatible with the original implementation of sapconf,
    # the stop action does nothing.
    echo 'Nothing to do.'
    exit 0
  ;;

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

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

esac

