#!/bin/sh

ip=
nfsroot=
root=

set -- $(cat /proc/cmdline)
for arg do
	key=${arg%%=*}
	if [ "$arg" = "$key" ]; then
		val=
	else
		val=${arg#*=}
	fi
	case "$key" in
		ip)
			ip=${val%%:*}
			;;
		nfsroot)
			nfsroot=$val
			;;
		root)
			root=$val
			;;
	esac
done

options=

if [ "$root" = "/dev/nfs" -o -n "$nfsroot" -a -n "$ip" ]; then
	set -- $(ip -4 -o addr show | awk '{print $2}' | grep -v '^lo$')
	for dev do
		case "$ip" in
			any|bootp|both|dhcp|on|rarp)
				options="-I $dev $options"
				;;
			*)
				dev_addr=`ip -4 -o addr show $dev | awk '{print $4}' | grep -o '^[0-9.]\+'`
				if [ "$ip" = "$dev_addr" ]; then
					options="-I $dev"
					break
				fi
				;;
		esac
	done
fi

[ -d /run/connmand ] || mkdir -p /run/connmand
echo "CONNMAND_OPTS=$options" >/run/connmand/connmand.env
