#!/bin/sh
#############################################################################
# Name: /IBM/bin/writeNvram <parm-id> <parm-value>
# Purpose: Utility to write NVRAM parms for SNMP or other use
#          client subagent
# Input: parm id # <id>, as specified in nvID.h 
# Output: This will write the value of the parmeter in file /tmp/snmp/<id>
#         without any labels
# If error, look at log in file /tmp/snmp/w.err.$1
#############################################################################

PATH=$PATH:/configs:/local

if [ $2 ]; then

if [ -f /configs/nvram_settings ]; then
# echo "writing $2 into $1 "
/local/nvram_rw -w$1 $2 -BDIT/configs/nvram_settings -BDOT/configs/nvram_settings

else

   # check if seeprom device is loaded
   if [ ! -e /dev/seeprom ]; then
      echo "writeNvram ERROR: seeprom device driver is not loaded" >> /tmp/snmp/w.err.$1 
      exit 1
   fi
 
   # if nvram file is not present, create one
   if [ ! -f /tmp/snmp/nvram.bin ]; then
      cp /tmp/snmp/seeprom /tmp/snmp/nvram.bin
   fi


   # Call nvram_rw program to write NVRAM back to tmp binary file and save
   # it in NVRAM
   /IBM/bin/nvram_rw  -I/tmp/snmp/nvram.bin -w$1 $2 -OB/tmp/snmp/nvram.bin.tmp
#  Used for debug only - Under normal condition, use the above invocation
#   /IBM/bin/nvram_rw -d1 -I/tmp/nvram.bin -w$1 $2 -OB/tmp/nvram.bin.tmp  >> /tmp/snmp/w.err.$1 

   if [ -f /tmp/snmp/nvram.bin.tmp ]; then
      mv /tmp/snmp/nvram.bin.tmp  /tmp/snmp/nvram.bin
   else
      echo "writeNvram ERROR:  nvram_rw failed for id=$1, value=$2" >> /tmp/snmp/w.err.$1 
   fi 

   # Write back to NVRAM 512 byte
   cp /tmp/snmp/nvram.bin  /tmp/snmp/seeprom 
fi
else

 echo "writeNvram ERROR: No id and/or value specified"
 echo "  Usage: writeNvram <id> <value> "
 echo "         <id> as specified in nvID.h"
 echo "  Any write error will be logged in /tmp/snmp/w.err.<id> file"


fi
