#! /bin/sh
#  script to set access restriction for Linux agent

if [ "$(command -v LogMessage)x" != "LogMessagex" ]; then
	LogMessage()
	{
	  echo "${@}"
	}
fi

if [ "$(command -v ParamValue)x" != "ParamValuex" ]; then
  ParamValue()
  {
    echo "${1}" | cut -f2 -d=
  }
fi


SetAccessRestriction()
{
  access_setting=
  access_set=0

  for arg in "${@}"; do 
    case "${arg}" in
      -setaccess=*|-ACC=*)  access_setting=$(ParamValue "${arg}")
			    access_set=1 ;;
    esac
  done

  if [ ${access_set} -eq 1 ]; then
    if [ -z "${install_dir}" ]
    then
       install_dir=$(pwd)
    fi
    command="\"${install_dir}/buagent\" --setaccess \"${access_setting}\" 2>&1"
    sh -c "${command}"
    err=${?}

    if [ ${err} -ne 1 ]; then
      LogMessage "failed to apply access restriction setting."
    fi
  fi
}

if [ "$(basename "${0}")" = "accesscontrol" ]
then
  if [ -f ${install_dir}/Global.vvc ]; then
    old_rear_path=$(grep 'ReaR_Path' ${install_dir}/Global.vvc | sed 's/[[:space:]]//g' | cut -d '=' -f 2)
    if [ -n "${old_rear_path}" ]; then
      LogMessage "Cannot set folder restrictions because Bare Metal Restore (BMR) is enabled."
      exit 1
    fi 
  fi
  SetAccessRestriction "$@"
fi
