#!/bin/bash

# Validate and manage setting sysctl settings.
#
# The script is called with name/value pairs which are stored
# in the system default sysctl.d directory. Before adding new
# settings a validation is done to ensure that conflicting
# sysctl settings have not been requested. Once finished sysctl
# is used to activate the changes.
set -eu

NAME=${1:-}
VALUE=${2:-}
# Optional comment used to describe the setting
COMMENT=${3:-"This file is managed via the TripleO sysctl image element."}

if [ -z "$NAME" -o -z "$VALUE" ]; then
    echo "NAME and VALUE are required."
    exit 1
fi

FILENAME="/etc/sysctl.d/${NAME}.conf"

if [ -f $FILENAME ]; then
    # check to make sure the settings match... otherwise fail
    if ! grep -q "^$NAME = $VALUE" $FILENAME; then
        echo "Conflicting sysctl.conf setting for $NAME == $VALUE. Found:"
        grep "^$NAME" $FILENAME
        exit 1
    fi
else

    if ! sysctl -a | grep -q "^$NAME"; then
        echo "Invalid sysctl key: $NAME"
        exit 1
    fi

    sysctl-write-value $NAME "$VALUE" "$COMMENT"

    sysctl -p $FILENAME

fi
