#!/bin/sh
########################################################################
# Begin /lib/services/bridge
#
# Description : Bridge Boot Script
#
# Authors     : Nathan Coulson - nathan@linuxfromscratch.org
#               Bruce Dubbs - bdubbs@linuxfromscratch.org
#
# Version     : LFS-7.0
#
########################################################################

. /lib/lsb/init-functions

if [ -z ${1} ] ; then
  log_failure_msg "No ifconfig file specified"
  exit 1
fi
IFCONFIG=$1
. ${IFCONFIG}

# Call the bridge ${BRIDGE_IF} for clarity.
if [ -z ${BRIDGE_IF} ]; then
  log_failure_msg "BRIDGE_IF variable missing from ${IFCONFIG}"
  exit 1
fi

case "${2}" in
  up)
     log_info_msg2 "\n"
     log_info_msg "Creating the ${BRIDGE_IF} interface..."
     brctl addbr ${BRIDGE_IF}
     ip link set ${BRIDGE_IF} up
     evaluate_retval
     for PHY in ${PHYS}; do
        log_info_msg "Adding ${PHY} to ${BRIDGE_IF}..."
        brctl addif ${BRIDGE_IF} ${PHY}
        evaluate_retval
     done

     if [ "${IP_FORWARD}" = "y"    -o \
          "${IP_FORWARD}" = "yes"  -o \
          "${IP_FORWARD}" = "t"    -o \
          "${IP_FORWARD}" = "true" -o \
          "${IP_FORWARD}" = "1" ]; then
       sysctl -w net.ipv4.ip_forward=1 > /dev/null
       log_success_msg "Setting net.ipv4.ip_forward = 1"
     fi
  ;;

  down)
     for PHY in ${PHYS}; do
        log_info_msg "Removing ${PHY} from ${BRIDGE_IF}..."
        brctl delif ${BRIDGE_IF} ${PHY}
        evaluate_retval
     done

     log_info_msg "Bringing down the ${BRIDGE_IF} interface..."
     ip link set ${BRIDGE_IF} down
     brctl delbr ${BRIDGE_IF}
     evaluate_retval
  ;;

  *)
     echo "Usage: ${0} <ifconfig-file> {up|down}"
     exit 1
  ;;
esac

# End /lib/services/bridge
