# TUN/TAP (sys-apps/usermode-utilities) module for net-scripts
# Version 1.0.0
# Copyright (c) 2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License V2
# Contributed by Roy Marples (uberlord@gentoo.org)

# Fix any potential localisation problems
# Note that LC_ALL trumps LC_anything_else according to locale(7)
tunctl() {
	LC_ALL=C /usr/bin/tunctl "$@"
}

# char* tuntap_provides(void)
#
# Returns a string to change module definition for starting up
tuntap_provides() {
	echo "tuntap"
}

# void tuntap_depend(void)
#
# Sets up the dependancies for the module
tuntap_depend() {
	after interface
	before dhcp
}

# bool tuntap_check_installed(void)
#
# Returns 1 if tuntap is installed, otherwise 0
tuntap_check_installed() {
	[[ -x /usr/bin/tunctl ]] && return 0
	[[ ${1} == true ]] && eerror "For TunTap support, emerge sys-apps/usermode-utilities"
	return 1
}

# bool tuntap_check_depends(void)
#
# Checks to see if we have the needed functions
tuntap_check_depends() {
	return 0
}

# char* tuntap_get_vars(char *interface)
#
# Returns a string spaced with possible user set
# configuration variables
tuntap_get_vars() {
	echo "tuntap_${1}"
}

# bool tuntap_stop(char *iface)
#
# Removes the device
tuntap_stop() {
	local iface=${1}
	
	tuntap_check_installed || return 0

	# tunctl doesn't always error on on tun/tap
	#interfaces (mainly aliases, etc)
	if tunctl -d ${iface} &>${devnull} ; then
		interface_exists ${iface} || einfo "Destroyed Tun/Tap interface ${iface}"
	fi

	return $?
}

# bool tuntap_start(char *iface)
#
# Create the device, give it the right perms
tuntap_start() {
	local iface=${1} opts

	# We don't configure the interface directly
	(( label_counter-- ))

	# Get our options
	eval opts=\"\$\{tuntap_${iface}\}\"

	ebegin "Creating Tun/Tap interface ${iface}"
	tunctl ${opts} -t ${iface} &>${devnull}
	eend $? || return 1

	return 0
}
