#!/bin/bash
#
# Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
# Licensed under the GNU General Public License Version 3 as shown at https://www.gnu.org/licenses/gpl-3.0.txt.

property_whitelist="vm.max_map_count"

action=$1
property=$2
value=$3

if ! echo "$property_whitelist" | grep -w "$2"; then
      echo "$1 property is not permitted by this tool"
      exit 1
fi
if [ $action = "get" ]; then
  currVal=$(/sbin/sysctl $property -q -n)
  echo $currVal
else
  if [ $action = "set" ] ; then
    if [ `/sbin/sysctl $property -q -n` -lt $value ]; then
      grep -q $property /etc/sysctl.conf
      return_code=$?
      #echo return_code = $return_code
      if [ ${return_code} -eq 0 ]; then
        /sbin/sysctl -w $property=$value
        #property has already been added to /etc/sysctl.conf - just update the value
        sed -i "/$property/c $property = $value" /etc/sysctl.conf
      else
        /sbin/sysctl -w $property=$value
        echo "$property = $value" | sudo tee -a /etc/sysctl.conf
      fi
    fi
  fi
fi