#!/bin/ksh -p
#
# ident "@(#)utatiref_script.sh	1.1 08/10/20 SMI"
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

# In this example, back_end_db lines must be of the form:
#
# insert_token=TOKEN name='NAME' otherInfo='OTHER INFO'
#
# name *must* be present, and name and otherInfo values *must* be
# single-quoted and must not contain a single-quote

DBFILE=/opt/SUNWutref/ati/back_end_db

get_keyval_pair() {
    key=$1
    value=$2
    return_key=$3

    sed -n "s/^${key}=${value}[ 	].*\(${return_key}=\)'\([^']*\)'.*/\1\2/p" $DBFILE | tail -1
}

getbytoken() {
    # Look for lines that begin with the token keyword and match the supplied token
    # Output the last registered seen on any such lines
    get_keyval_pair insert_token ${insert_token} name
 
    # Look for lines that begin with the token keyword and match the supplied token
    # Output the last registered seen on any such lines
    get_keyval_pair insert_token ${insert_token} otherInfo
}

# exit if the mapping file is missing or unreadable
if [ ! -r "$DBFILE" ]
then
    echo "$0: Missing or unreadable mapping file '$DBFILE'" 1>&2
    exit 3
fi

# parse the args into shell vars
while read A
do
    eval "$A"
done

OUTPUT=`getbytoken`
if [ -n "$OUTPUT" ]; then
	print -r "$OUTPUT"
	echo "registered=1"
else
	echo "registered=0"
fi
exit 0
