################################################################################
#
# Copyright (c) 2001-2006, Intel Corporation 
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met:
# 
#  1. Redistributions of source code must retain the above copyright notice, 
#     this list of conditions and the following disclaimer.
# 
#  2. Redistributions in binary form must reproduce the above copyright 
#     notice, this list of conditions and the following disclaimer in the 
#     documentation and/or other materials provided with the distribution.
# 
#  3. Neither the name of the Intel Corporation nor the names of its 
#     contributors may be used to endorse or promote products derived from 
#     this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
################################################################################

#!/bin/sh
APP=/usr/sbin/asfcli
SCRIPT=/tmp/tmpscript.tmp
TELNET_TIMEOUT=10    #increase this if connecting to remote machine using "telnet" takes longer

#Version
########
COMP_VERSION=4.0.0


keywordlist="USERNAME PASSWORD TARGET ENABLE DESTINATIONIP PING PINGINTERVAL HEARTBEAT HEARTBEATINTERVAL WATCHDOG WATCHDOGINTERVAL SNMPCOMMUNITY ADAPTER"		    
		    
function to_vars() {    

    while [ $# != 0 ]; do
        left=`echo $1 | perl -ne 's/(.*)\=.*/\1/; tr/[a-z]/[A-Z]/; print $_'`
        right=`echo $1 | perl -ne 's/.*\=(.*)/\1/; print $_'`
        found=0
        for keyword in $keywordlist; do
            if [ "$left" == "$keyword" ]; then
                found=1
            fi
        done
        if [ $found == 0 ]; then
            echo Unknown command $left
	    usage
            exit 1
        fi
        eval $left=\"$right\"
        shift
    done
}

function remote_script() {
    hostname=$1
    username=$2
    password=$3
    scriptname=$4
    
    echo "Connecting to $hostname..."
    ( sleep $TELNET_TIMEOUT; echo $password; cat $scriptname; echo exit; sleep 5) | telnet $hostname -l$username 1>/dev/null 2>&1 
    echo "$hostname: Done."
}

						 
function usage() {
    echo "Usage:"
    echo "    $0 version"
    echo "    $0 <keyword1=value1> <keyword2=value2> ..."
    echo "    Available keywords: "
    for keyword in $keywordlist; do
	echo "        $keyword"
    done
    echo "    The keywords DESTINATIONIP and ADAPTER are obligatory."     
    echo "    Defaults: TARGET - localhost, USERNAME - name of user running the script."
    echo "    The USERNAME used must be a superuser on the configured machine."
    echo "    For boolean values, use the strings true/false."     
}
	
	
	
if [ $# == 1 ]; then	
    if [ $1 == "version" ]; then
	echo "Remote ASF confuguration script. version: $COMP_VERSION."
	exit 1
    fi
fi

						 
to_vars "$@"

([ $DESTINATIONIP ] && [ $ADAPTER ]) || { usage; exit 1; }

\rm -f $SCRIPT
touch $SCRIPT

[ $TARGET ] || TARGET=localhost
[ $USERNAME ] || USERNAME=$USER

echo $APP init >> $SCRIPT

[ "$HEARTBEAT" == "false" ] && echo $APP sethb 0 >> $SCRIPT
[ "$HEARTBEAT" != "false" ] && [ $HEARTBEATINTERVAL ] && echo $APP sethb $HEARTBEATINTERVAL >> $SCRIPT

[ "$PING" == "false" ] && echo $APP setping 0 >> $SCRIPT
[ "$PING" != "false" ] && [ $PINGINTERVAL ] && echo $APP setping $PINGINTERVAL>> $SCRIPT 

[ "$WATCHDOG" == "false" ] && echo $APP setwd 0 >> $SCRIPT 
[ "$WATCHDOG" != "false" ] && [ $WATCHDOGINTERVAL ] && echo $APP setwd $WATCHDOGINTERVAL >> $SCRIPT

[ "$SNMPCOMMUNITY" ] && echo $APP setcom \"$SNMPCOMMUNITY\" >> $SCRIPT
echo $APP apply $ADAPTER $DESTINATIONIP >> $SCRIPT
[ "$ENABLE" == "false" ] && echo $APP disable >> $SCRIPT
remote_script $TARGET $USERNAME $PASSWORD $SCRIPT



