#!/bin/sh

# Before we do anything, make sure this is being run as root
if [ `id -u` != "0" ]
then
  echo "ERROR: This command must be run as root!"
  exit 1
fi

# Do an arg check to make sure 1 arg was given
if [ "$#" != "1" ]
then
  echo "ERROR: This command only takes one argument!"
  echo "ERROR: Please review your command line entry and try again!"
  exit 1
fi

# Get the path where this script resides to find our other tools
export EDBG_HOME=`dirname \`dirname "$(readlink -f "$0")"\``

# Point the LD_LIBRARY_PATH to our required libs
export LD_LIBRARY_PATH=$EDBG_HOME/lib:$LD_LIBRARY_PATH

# Call edbgdetcnfg to create a config file on this system
# Put the new output from the command into the trash to preserve behavior
$EDBG_HOME/bin/edbgdetcnfg > /dev/null
rc=$?
if [ "$rc" != "0" ]
then
  exit $rc
fi

# Set env var edbg expects
export EDBG_DTB=none

# Call the underlying VPD keyword modification
echo "Updating system model number to $1"
$EDBG_HOME/bin/edbg putvpdkeyword nochip fru "OSYS" "MM" "$1" -i a -quiet
rc=$?
if [ "$rc" != "0" ]
then
  echo "ERROR: A problem occurred updating the model number. Please see previous output for reason."
  exit $rc
else
  echo "Update complete!"
fi
