#!/bin/bash
#
################################################################################
#
#                     S H E L L   S C R I P T  F I L E
#
#                         COPYRIGHT 2011 MOTOROLA
#                           All Rights Reserved
#
################################################################################
#
#   FILE NAME       : omc_trap_config
#   FUNCTION        : show/delete/add entry from snmp table
#   PARAMETERS      :  
#                      
################################################################################
#
################################################################################
#
# Revision History:
#
# Date      Prob#          Description
# --------- -------------  -----------------------------------------------------
# 01-MAr-11   Initial version
################################################################################

################################# Includes #####################################

SUCCESS=0
ERROR=1

################################# functions ####################################

# ---------------------------------------------------------------------------
# Function: 
#    get parameters from user
# ---------------------------------------------------------------------------
function input_manager_id_from_user
{
	answered="false"
	while [ $answered == "false" ]; do
		read -p " Enter snmp_trap_manager_id: " TRAP_MANAGER_ID
		check_numeric ${TRAP_MANAGER_ID}
		if [ ${?} -ne ${SUCCESS} ]; then
			print_in_console_and_syslog "ERROR: ${TRAP_MANAGER_ID} isn't valid, try again"
		else
			answered="true"
		fi
	done
}

# ---------------------------------------------------------------------------
# Function: 
#    get parameters from user
# ---------------------------------------------------------------------------
function input_snmp_params_from_user
{
	answered="false"
	while [ $answered == "false" ]; do
		read -p " Enter snmp_trap_manager_id: " TRAP_MANAGER_ID
		check_numeric ${TRAP_MANAGER_ID}
		if [ ${?} -ne ${SUCCESS} ]; then
			print_in_console_and_syslog "ERROR: ${TRAP_MANAGER_ID} isn't valid, try again"
		else
			answered="true"
		fi
	done
	
	answered="false"
	while [ $answered == "false" ]; do
		read -p " Enter snmp_trap_manager_ip: " TRAP_MANAGER_IP
		check_ip ${TRAP_MANAGER_IP}
		if [ ${?} -ne ${SUCCESS} ]; then
			print_in_console_and_syslog "ERROR: ${TRAP_MANAGER_IP} isn't valid, try again"
		else
			answered="true"
		fi
	done

	answered="false"
	while [ $answered == "false" ]; do
		read -p " Enter trap_manager_community_string: " TRAP_COMMUNITY_STRING
#		check_ip ${TRAP_COMMUNITY_STRING}
#		if [ ${?} -ne ${SUCCESS} ]; then
#			print_in_console_and_syslog "ERROR: ${TRAP_COMMUNITY_STRING} isn't valid, try again"
#		else
			answered="true"
#		fi
	done
	
	answered="false"
	while [ $answered == "false" ]; do
		read -p " Enter snmp_trap_manager_port: " TRAP_MANAGER_PORT
		check_numeric ${TRAP_MANAGER_PORT}
		if [ ${?} -ne ${SUCCESS} ]; then
			print_in_console_and_syslog "ERROR: ${TRAP_MANAGER_PORT} isn't valid, try again"
		else
			answered="true"
		fi
	done

	answered="false"
	while [ $answered == "false" ]; do
		read -p " Enter snmp_trap_manager_enable: " TRAP_MANAGER_ENABLE
		check_binary ${TRAP_MANAGER_ENABLE}
		if [ ${?} -ne ${SUCCESS} ]; then
			print_in_console_and_syslog "ERROR: ${TRAP_MANAGER_ENABLE} isn't valid, try again"
		else
			answered="true"
		fi
	done		
}

# ---------------------------------------------------------------------------
# Function: 
#    validate Ip adress
#    terminate script
# Input:
#    ${*} - one or more string parameters
# ---------------------------------------------------------------------------
 function check_ip() {
	return_code=${SUCCESS}
	target=${1}
	regex="\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"

	CHECK=$(echo $target | egrep $regex)
	if [[ "$?" -ne 0 ]]
	then
		return_code=${ERROR}
	fi
	
	return ${return_code}
 }
# ---------------------------------------------------------------------------
# Function: 
#    print_in_console_and_syslog
# ---------------------------------------------------------------------------
function print_in_console_and_syslog
{
    printf ".... $1 ....\n"
    /usr/bin/logger -t $1
} 

# ---------------------------------------------------------------------------
# Function: 
#    delete_snmp_trap
#    
# Input:
#    ${*} - none
# ---------------------------------------------------------------------------
function delete_snmp_trap
{
	input_manager_id_from_user
	/opt/Motorola/mcd5000/omcserver/bin/omc_ipds_config delete_snmp -dtrap $TRAP_MANAGER_ID
	
 #   print_in_console_and_syslog "ERROR: ${*}"
 #   exit $FAILURE
}

# ---------------------------------------------------------------------------
# Function: 
#    validate numaric
#    terminate script
# Input:
#    ${*} - one or more string parameters
# ---------------------------------------------------------------------------
 function check_numeric() {
 
if [ -z "$1" ]; then
#	echo "Selection not entered";
	return $ERROR;
fi

if [[ ! -z $(echo $1 | sed 's/[0-9]//g') ]]
then
#	echo "integer only";
	return $ERROR;
fi
}

# ---------------------------------------------------------------------------
# Function: 
#    validate 0/1
#    terminate script
# Input:
#    ${*} - one or more string parameters
# ---------------------------------------------------------------------------
 function check_binary() {
 
if [[ ! -z $(echo $1 | sed 's/[0-9]//g') ]]
then
#	echo "0/1 integers only";
	return $ERROR;
fi

if [ ${1} -ne '0' -a ${1} -ne '1' ]; then
#	echo "0/1 integers only";
	return $ERROR;
fi
}

# ---------------------------------------------------------------------------
# Function: 
#    add_snmp_trap
#    
# Input:
#    ${*} - none
# ---------------------------------------------------------------------------
function add_snmp_trap
{
	input_snmp_params_from_user
	/opt/Motorola/mcd5000/omcserver/bin/omc_ipds_config add_snmp -manager_id ${TRAP_MANAGER_ID} -manager_ip ${TRAP_MANAGER_IP} -community_string ${TRAP_COMMUNITY_STRING} -manager_port ${TRAP_MANAGER_PORT} -manager_enable ${TRAP_MANAGER_ENABLE}
	}

# ---------------------------------------------------------------------------
# Function: 
#    update_snmp_trap
#    
# Input:
#    ${*} - none
# ---------------------------------------------------------------------------
function update_snmp_trap
{
	answered="false"
	while [ $answered == "false" ]; do
		read -p " Enter snmp_trap_manager_id: " TRAP_MANAGER_ID
		check_numeric ${TRAP_MANAGER_ID}
		if [ ${?} -ne ${SUCCESS} ]; then
			print_in_console_and_syslog "ERROR: ${TRAP_MANAGER_ID} isn't valid, try again"
		else
			answered="true"
		fi
	done
	
	/opt/Motorola/mcd5000/omcserver/bin/omc_ipds_config update_snmp -manager_id ${TRAP_MANAGER_ID}
}


# ---------------------------------------------------------------------------
# Function: 
#    show_menu
#   show and execute main menu
# Input: None.
# ---------------------------------------------------------------------------
function show_menu
{
  while [ 1 ]
  do
	  printf "\n"
      printf "          1 -> Show snmp_trap_manager table\n"
      printf "          2 -> Add new entry to snmp_trap_manager table\n"
	  printf "          3 -> Update an entry in snmp_trap_manager table\n"
      printf "          4 -> Delete entry from snmp_trap_manager table\n"
      printf "          5 -> Quit\n"
      printf "\n"
      printf "  Choice? "
      read -r userchoice

      case $userchoice in

	"1")
		#printf "\nsnmp_trap_manager table:\n"
		/opt/Motorola/mcd5000/omcserver/bin/omc_ipds_config show_snmp
	#	exit
		;;
	"2")
		printf "\nAdd entry to snmp_trap_manager table:\n"
		add_snmp_trap
	#	exit
		;;
	"3")
		printf "\nUpdate an entry in snmp_trap_manager table:\n"
		update_snmp_trap
	#	exit
		;;		
	"4")
		printf "\nDelete entry from snmp_trap_manager table:\n"
		delete_snmp_trap
	#	exit
		;;
	"5")
		printf "\nQuitting...\n"
		exit
		;;		
	*)
		printf "\n     -->  Input error: please insert 1-5  <--\n\n"
		;;
	esac
  done
}	
################################# Main Body ####################################
TRAP_MANAGER_ID=
TRAP_MANAGER_IP=
TRAP_COMMUNITY_STRING=
TRAP_MANAGER_PORT=
TRAP_MANAGER_ENABLE=


show_menu

#################################  E N D    ####################################
