#!/bin/sh
#
# chkconfig: 2345 99 00
# description: Starts/stops Qubes default netvm
#
### BEGIN INIT INFO
# Provides:          qubes-networking
# Required-Start:    qubes-core
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Default-Enabled:   yes
# Short-Description: Start/stop qubes networking
# Description:       Starts and stops the qubes networking
### END INIT INFO


#
# Source function library.
. /etc/rc.d/init.d/functions

NETVM=$(qvm-get-default-netvm)

start()
{
    if [ x$NETVM = x ] ; then

        echo WARNING: Qubes NetVM not configured!
        echo -n $"Doing nothing:"

    elif [ $NETVM = "dom0" ] ; then 

        echo -n $"Setting up net backend in Dom0:"
        echo "NS1=10.0.0.1" > /var/run/qubes/qubes_ns
        echo "NS2=10.0.255.254" >> /var/run/qubes/qubes_ns
        /usr/lib/qubes/qubes_setup_dnat_to_ns
        echo "1" > /proc/sys/net/ipv4/ip_forward || exit 1
    else

        echo -n $"Starting default NetVM:"
        /usr/lib/qubes/unbind_all_network_devices || exit 1
        qvm-start -q --no-guid $NETVM || exit 1

    fi
    touch /var/lock/subsys/qubes_netvm
    success
    echo
	return 0
}

stop()
{
    if [ x$NETVM = x ] ; then

        echo WARNING: Qubes NetVM not configured!
        echo -n $"Doing nothing:"

    elif [ $NETVM = "dom0" ] ; then 

        echo -n $"Stopping Qubes networking in Dom0:"
    else

        echo -n $"Stopping default NetVM:"
        qvm-run -q --shutdown --wait $NETVM

    fi
    rm -f /var/lock/subsys/qubes_netvm
    success
    echo

	return 0
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  *)
	echo $"Usage: $0 {start|stop}"
	exit 3
	;;
esac

exit $RETVAL