#!/bin/sh
#
# hamachi.sh - load tap driver and start Hamachi's tuncfg daemon
#
# (C) Copyright 2007 by Lapo Luchini
# (loosely based on ports/security/openvpn/files/openvpn.sh.in 1.9 by Matthias Andree)
#
# $FreeBSD: ports/security/hamachi/files/hamachi.sh.in,v 1.1 2007/09/07 07:47:07 edwin Exp $
# 
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.

# PROVIDE: hamachi
# REQUIRE: DAEMON
# KEYWORD: shutdown

#TODO: serve la KEYWORD:shutdown? che fa?

# Note that we deliberately refrain from unloading drivers.

. /etc/rc.subr

name="hamachi"
rcvar=`set_rcvar`

load_rc_config $name

: ${hamachi_enable="NO"}

command="/usr/local/sbin/hamachi-tuncfg"
start_precmd="hamachi_precmd"

hamachi_precmd() {
	# FreeBSD <= 5.4 does not know kldstat's -m option
	# FreeBSD >= 6.0 does not add debug.* sysctl information
	# in the default build - we check both to keep things simple
	if ! sysctl debug.if_tap_debug >/dev/null 2>&1 \
			&& ! kldstat -m if_tap >/dev/null 2>&1 ; then
		if ! kldload if_tap ; then
			warn "Could not load tap module."
			return 1
		fi
	fi
	if ! sysctl compat.linux >/dev/null 2>&1 \
			&& ! kldstat -m linuxelf >/dev/null 2>&1 ; then
		if ! kldload linux ; then
			warn "Could not load linux module."
			return 1
		fi
	fi
	return 0
}

run_rc_command "$1"
