#!/bin/bash
#
# chkconfig: 35 99 05
# description: Service to start private networks defined by ConVirt
# processname: convirtnw
#
### BEGIN INIT INFO
# Provides:          convirt-nw 
# Required-Start:    $syslog $remote_fs $network xend SuSEfirewall2_setup
# Should-Start:      xendomains $xendomains $ALL
# Required-Stop:     $syslog $remote_fs 
# Should-Stop:       
# Default-Start:     3 5
# Default-Stop:      0 1 2 4 6
# Short-Description: Starts and stops convirtnw
# Description:       Starts and stops convirtnw automatically when the
#                    host starts and stops.
### END INIT INFO
###
#   ConVirt   -  Copyright (c) 2008 Convirture Corp.
#   ======
#
# ConVirt is a Virtualization management tool with a graphical user
# interface that allows for performing the standard set of VM operations
# (start, stop, pause, kill, shutdown, reboot, snapshot, etc...). It
# also attempts to simplify various aspects of VM lifecycle management.
#
#
# This software is subject to the GNU General Public License, Version 2 (GPLv2)
# and for details, please consult it at:
#
#    http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
# 
#
# author : mkelkar@users.sourceforge.net
#

prg_name=`readlink -f $0`
base=`dirname $prg_name`
if [ "${base}" == "" ]; then
    base="."
fi

# Its better to hard code the path as links may not work in /etc/init.d
### common_scripts=${base}/../../../common/scripts
common_scripts=/var/cache/convirt/common/scripts

NETWORK_CONF_FILE=/var/cache/convirt/networks/privatenw.conf
UTILS_CODE=${common_scripts}/utils
FUNCTIONS_CODE=${common_scripts}/functions
HELPER_CODE=${common_scripts}/nw_functions

# NOTE: This code assumes that you have setup a private n/w on this 
#       managed node once, thus having the helper functions available
#       at /var/cache/convirt/common/scripts area.

for code in $UTILS_CODE $FUNCTIONS_CODE $HELPER_CODE
do
    if [ -e $code ]; then
        source "$code"
    else
       echo "$code not found. Possibly no virtual networks defined."
       exit 0
    fi
done

# Detect the distro and source that file.
detect_distro
if [ "$?" != "0" ]; then
   echo "Error detecting Linux distribution.Exiting."
   exit 1
fi

# dump information
#echo "DISTRO ${DIST}"
#echo "VER ${VER}"
#echo "CODENAME ${CODE_NAME}"
#echo "KERNEL ${KERNEL}"
#echo "ARCH ${ARCH}"

# include the distro specific file if it exists.
distro_functions=$common_scripts/${DIST}_functions
if [ -r $distro_functions ]; then
    echo "Info: Sourcing $distro_functions"
    source $distro_functions
else
   echo "Info: $distro_functions not found."
fi

start()
{
  if [ -e $NETWORK_CONF_FILE ]; then
    enable_ip_forward
    cat $NETWORK_CONF_FILE | while read line
    do
      bridge_name=`echo $line | awk -F";" '{print $1}' | cut -d'=' -f2`
      ip_network=`echo $line | awk -F";" '{print $2}'  | cut -d'=' -f2`
      ip_address=`echo $line | awk -F";" '{print $3}'  | cut -d'=' -f2`
      dhcp_start=`echo $line | awk -F";" '{print $4}'  | cut -d'=' -f2`
      dhcp_end=`echo $line | awk -F";" '{print $5}'    | cut -d'=' -f2`
      netmask=`echo $line | awk -F";" '{print $6}'    | cut -d'=' -f2`
      interfaceName=`echo $line | awk -F";" '{print $7}'    | cut -d'=' -f2`
      setup_privatenw $bridge_name $ip_network $ip_address $dhcp_start $dhcp_end $netmask $interfaceName
    done
  fi
}


case "$1" in
   start)
      start
      exit 0
      ;;
   stop)
      exit 0
      ;;
   restart):
      start
      exit 0
     ;;
esac
