#!/bin/sh
#
# Add unlimited pmlc access from the local network to the default
# pmlogger.
#
# TODO IPv6?
#

tmp=/var/tmp/$$
sts=0
trap "rm -f $tmp.*; exit \$sts" 0 1 2 3 15

# add additional and optional directories
for dir in /sbin /usr/sbin
do
    if [ -d "$dir" ]
    then
	if echo ":$PATH:" | grep -q ":$dir:"
	then
	    :
	else
	    export PATH="$PATH:$dir"
	    #debug# echo add $dir to \$PATH
	fi
    fi
done

# same function is in check-vm ... need to track changes
#
_getnetworkaddr()
{
    if `which ifconfig >/dev/null 2>&1`
    then
	# expecting ifconfig lines like ...
        # inet 192.168.1.222  netmask 255.255.255.0  broadcast 192.168.1.255
	# inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0
	#
	# skip ip tunnels/vpns like
	# inet addr:10.8.3.138  P-t-P:10.8.3.137  Mask:255.255.255.255
	#
	ifconfig \
	| grep -v '[ :]127\.0\.0' \
	| sed -n >$tmp.addr \
	    -e '/^[ 	]*inet .*cast[ :]/{
s/^[ 	]*inet[ 	]//
s/^addr://
s/[ 	][ 	]*broadcast[ 	][^ 	]*//
s/:Bcast:[^ ]*//
s/[ 	][ 	]*netmask[ 	]/ /
s/[ 	][ 	]*Mask:/ /
p
}'
	#debug# cat $tmp.addr >&2
	num=`wc -l <$tmp.addr | sed -e 's/ //g'`
	if [ "$num" -gt 1 ]
	then
	    echo >&2 "_getnetworkaddr: confused by multiple network addresses"
	    echo >&2 "Please choose the primary address for incoming pmlc requests:"
	    rm -f $tmp.tmp
	    cat $tmp.addr \
	    | while read addr stuff
	    do
		echo -n >&2 "$addr [y|n]?"
		read ans </dev/tty
		if [ "X$ans" = Xy ]
		then
		    echo "$addr $stuff" >$tmp.tmp
		    break
		fi
	    done
	    mv $tmp.tmp $tmp.addr
	    num=`wc -l <$tmp.addr | sed -e 's/ //g'`
	fi
	case "$num"
	in
	    1)	# good
		addr=`cat $tmp.addr | sed -e 's/ .*//'`
		mask=`cat $tmp.addr | sed -e 's/.* //'`
		case "$mask"
		in
		    255.255.255.0|0xffffff00)	# /24 network
			echo "$addr" | sed -e 's/\.[0-9]*$/.*/'
			;;
		    # pmcd's [access] is not smart enough to handle other
		    # than /24 networks, so map the other likely options
		    # to the broader /24 network
		    #
		    255.255.255.128|255.255.255.192|255.255.255.224|255.255.255.240|255.255.255.248|255.255.255.252|255.255.255.254)
			echo "$addr" | sed -e 's/\.[0-9]*$/.*/'
			;;
		    *)
			echo >&2 "_getnetworkaddr: cannot handle network mask: $mask"
			;;
		esac
		;;
	    0)	# none?
		echo >&2 "_getnetworkaddr: cannot get network address"
		;;
	    *)	# multiple?
		echo >&2 "_getnetworkaddr: cannot handle multiple network addresses:"
		cat >&2 $tmp.addr
		;;
	esac
    else
	echo >&2 '_getnetworkaddr: no ifconfig?'
    fi
}

network=`_getnetworkaddr`

if [ -z "$network" ]
then
    echo "Arrgh ... cannot discover local network IP address"
    sts=1
    exit
fi

if [ -f /etc/pcp.conf ]
then
    . /etc/pcp.conf

    # TODO ... check in pmlogger for config file name ... other than
    # assuming config.default
    if [ -f $PCP_VAR_DIR/config/pmlogger/config.default ]
    then
	if grep -q "allow $network" $PCP_VAR_DIR/config/pmlogger/config.default
	then
	    echo "Already done."
	else
	    cp $PCP_VAR_DIR/config/pmlogger/config.default $tmp.conf
	    # TODO check for [access] at the end already?
	    echo "allow $network : all;" >>$tmp.conf
	    diff -u $PCP_VAR_DIR/config/pmlogger/config.default $tmp.conf
	    sudo cp $tmp.conf $PCP_VAR_DIR/config/pmlogger/config.default
	fi
    else
	echo "Warning: \"$PCP_VAR_DIR/config/pcp/pmlogger/config.default\" is missing"
    fi
else
    echo "Warning: \"/etc/pcp.conf\" is missing"
fi
