#!/bin/sh

PATH="/usr/lib/alterator-net-common:$PATH"

. shell-config

### common iface options
write_iface_option()
{
    shell_config_set "$1/options" "$2" "$3"
}

read_iface_option()
{
    shell_config_get "$1/options" "$2"
}

### string ppp options
write_ppp_option()
{
    shell_config_set "$1/pppoptions" "$2" "$3" '[[:space:]]\+' ' '
    chmod o-rw "$name/pppoptions"
}

read_ppp_option()
{
    shell_config_get "$1/pppoptions" "$2" '[[:space:]]\+'
}

### boolean ppp options
read_ppp_option1()
{
    local file="$1";shift
    local name1="$1";shift
    local name2="$1";shift
    local defvalue="${1:-no}";shift ||:

    if grep -qs "^$name1\$" "$file/pppoptions";then
	echo 'yes'
    elif grep -qs "^$name2\$" "$file/pppoptions"; then
	echo 'no'
    else
	echo "$defvalue"
    fi
}

write_ppp_option1()
{
    local file="$1";shift
    local name1="$1";shift
    local name2="$1";shift
    local value="$1";shift

    [ -f "$file/pppoptions" ] || touch "$file/pppoptions"

    sed \
	-e "/^$name1/d" \
	-e "/^$name2/d " \
	-i "$file/pppoptions"

    if [ "$value" -eq 0 ]; then
	echo "$name1" >> "$file/pppoptions"
    else
	echo "$name2" >> "$file/pppoptions"
    fi
}

list_ppp()
{
	for i in `find /etc/net/ifaces -type d -name 'ppp*'`
	do
		[ -f $i/options ] &&
		    grep -qs "^PPPTYPE=$1\$" $i/options &&
		    echo "${i##*/}"
	done
}

next_ppp()
{
	local path="/etc/net/ifaces/ppp"
	local i=1	# not to interfere with dialup ppp0
	while true; do
		[ -d "$path$i" ] || { echo "ppp$i" && break; }
		i=$(($i + 1))
	done
}

list_eth()
{
    iflist | cut -f1 -d' '
}
