#!/bin/bash
# ==========================================================================
# TmsBrdCfgTool
# ------------
#
# $RCSfile$
# $Author: schulzema1 $
# $Revision: V1.0.0.1$
# $Date$
# $State$
#
# This script allows access to EEPROM on the new KR
#
# 11/01/11   schulzema1:  removed broken bootloader access and extended 
#                         eeprom access
#
# 10/09/09   schulzema1:  creation
#
#
# Notes:
#
# ==========================================================================
VERSION=V1.0.0.1
#####
#  Global Variables
#####
VERBOSE=false
VERSION_STRING="This is TmsBrdCfgTool $VERSION"
# EEPROM Device File
EEPROM_B=/sys/bus/i2c/devices/2-0050/eeprom
EEPROM_C=/sys/class/i2c-dev/i2c-0/device/0-0052/eeprom

#####
#  EEPROM Function
#
# Args:
#   first  :  $1  : w for write access and r or something different for read
#   second :  $2  : variable name to access
#####
function access_eeprom(){
	RnW=1
	if [ "$1" = "w" ]; then
		RnW=0
	fi
	case "$2" in
		"all")
			grep -oe '^[[:alnum:]_\-]\+=[[:alnum:]]\+' $EEPROM_B || return $?
			grep -oe '^[[:alnum:]_\-]\+=[[:alnum:]]\+' $EEPROM_C || return $?
			;;
		*)
			if [ $RnW -ne 0 ]; then
				grep -oe '^'$2'\+=[[:alnum:]]\+' $EEPROM_B || grep -oe '^'$2'\+=[[:alnum:]_\-]\+' $EEPROM_C || return $?
			else
				VAR=`expr $2 : '\([[:alnum:]_\-]\+\)=[[:alnum:]]\+'`
				VAL=`expr $2 : '[[:alnum:]_\-]\+=\([[:alnum:]]\+\)'`
				grep -q $VAR $EEPROM_C
				if [ $? -ne 0 ]; then
					echo "[ERROR]Variable \"$VAR\"not found."
					return 1;
				fi
				TMPFILE=eeprom_$RANDOM
				cat $EEPROM_C | sed -e 's/\('$VAR'=\)[[:alnum:]]\+/\1'$VAL'/g' > $TMPFILE 
				cat $TMPFILE > $EEPROM_C
				rm $TMPFILE
			fi
			
			;;
	esac
	return 0
}
#####
#  Commandline options
#####
SHORTOPTIONS=
LONGOPTIONS=
OPTIONS=(
h help "\tPrints a useful help"
v verbose "Be verbose."
V version "Prints version informations"
p print "\tPrint all available Variables"
r: read: "\tRead <var> from EEPROM"
#w: write: "\tWrite <var> to EEPROM"
#s: srn: "\tWrite SERIAL_NUMBER=<var> to EEPROM"
)

i=0
while [ $i -lt ${#OPTIONS[@]} ]; do
	SHORTOPTIONS+=${OPTIONS[$i]}
	let i++
	LONGOPTIONS+=${OPTIONS[$i]},
	let i++
	let i++
done 
#####
#  print_help() function: uses OPTIONS variable
#####
function print_help(){
	echo -e "Usage: $0 [OPTION]\n"
	echo -e "Access the EEPROM safely.\n"
	i=0
	while [ $i -lt ${#OPTIONS[@]} ]; do
		if [[ ${OPTIONS[$i]} == *:: ]]; then
			echo -ne "  -${OPTIONS[$i]/::/ [var]},\t"
		elif [[ ${OPTIONS[$i]} == *: ]]; then
			echo -ne "  -${OPTIONS[$i]/:/ <var>},\t"
		else
			echo -ne "  -${OPTIONS[$i]},\t\t"
		fi
		let i++
		if [[ ${OPTIONS[$i]} == *:: ]]; then
			echo -ne "--${OPTIONS[$i]/::/ [var]}\t"
		elif [[ ${OPTIONS[$i]} == *: ]]; then
			echo -ne "--${OPTIONS[$i]/:/ <var>}\t"
		else
			echo -ne "--${OPTIONS[$i]}\t\t"
		fi
		let i++
		echo -e "${OPTIONS[$i]}"
		let i++
	done                                
	echo "Examples:"
	echo -e "$0 -r SERIAL_NUMBER\n\tPrints the SERIALNUMBER from EEPROM."
	echo "Environment:"
	echo "  EEPROM device files: $EEPROM_B "
	echo "                       $EEPROM_C "
}

#####
#  Eval Commandline Arguments
#####
args=`getopt -o $SHORTOPTIONS -l $LONGOPTIONS -- "$@"`
eval set -- "$args"
while [[ $1 != -- ]] ; do
  case $1 in
  	"-h" | "--help")
  		print_help
  		break
  		;;
  	"-v" | "--verbose")
  		VERBOSE=true
  		shift
  		;;
  	"-V" | "--version")
  		echo $VERSION_STRING
  		break
  		;;
  	"-p" | "--print")
  		access_eeprom r all || exit $?
  		shift
  		;;
  	"-r" | "--read")
  		access_eeprom r $2 || exit $?
  		shift 2
  		;;
  	"-w" | "--write")
  		access_eeprom w $2 || exit $?
  		shift 2
  		;;
  	"-s" | "--srn")
  		access_eeprom w SERIAL_NUMBER=$2 || exit $?
  		shift 2
  		;;
	*) 
		echo "Internal error!" ;
		exit 1 
		;;
  esac
done
 