#!/bin/bash

#
# bridge_switcher - script to switch bridge mode on/off
#                   (usually called from the WLANPi menu system)    
#
# Written by Nigel Bowden <wifinigel@gmail.com>.
#

NAME=bridge_switcher
DESC="Script to switch bridge mode on/off"
STATUS_FILE="/etc/wlanpi-state"
NETWORK_DIR=/etc/systemd/network

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 
   exit 1
fi

###############################################################################
#
# Activate bridge mode:
#
# 1. Backup existing network files
# 2. Link to new network config files
# 3. reboot
#
###############################################################################

bridge_on () {
  
  # check what state the WLAN Pi is in
  PI_STATUS=`cat $STATUS_FILE | grep 'classic'` || true
  if  [ -z "$PI_STATUS" ]; then
     echo "Failed - WLAN Pi is not in classic mode."
     exit 1
  fi

  echo "Enabling Bridge mode..."
  # Backup files & remove originals
  
  if [ -e $NETWORK_DIR/eth0.network ]; then
    cp $NETWORK_DIR/eth0.network $NETWORK_DIR/eth0.network.brm
    rm $NETWORK_DIR/eth0.network
  fi

  if [ -e $NETWORK_DIR/eth1.network ]; then
    cp $NETWORK_DIR/eth1.network $NETWORK_DIR/eth1.network.brm
    rm $NETWORK_DIR/eth1.network
  fi

  if [ -e $NETWORK_DIR/usb1.network ]; then
    cp $NETWORK_DIR/usb1.network $NETWORK_DIR/usb1.network.brm
    rm $NETWORK_DIR/usb1.network
  fi

  if [ -e /etc/network/interfaces ]; then
    cp /etc/network/interfaces /etc/network/interfaces.brm
    rm /etc/network/interfaces
  fi

  # Link to file in bridge mode dir
  ln -s /etc/wlanpi-bridge/systemd/network/eth0.network $NETWORK_DIR/eth0.network
  ln -s /etc/wlanpi-bridge/systemd/network/eth1.network $NETWORK_DIR/eth1.network
  ln -s /etc/wlanpi-bridge/systemd/network/usb1.network $NETWORK_DIR/usb1.network
  ln -s /etc/wlanpi-bridge/systemd/network/br0.network $NETWORK_DIR/br0.network
  ln -s /etc/wlanpi-bridge/systemd/network/br0.netdev $NETWORK_DIR/br0.netdev

  # Signal that bridge mode is active
  echo "bridge" > $STATUS_FILE
  echo "WLAN Pi will now reboot to launch bridge mode."
  sleep 1
  reboot

}

###############################################################################
#
# De-activate bridge mode:
#
# 1. Remove file links
# 2. Rename backedup files to original names
# 3. Reboot
#
###############################################################################

bridge_off () {

  # check what state the WLAN Pi is in
  PI_STATUS=`cat $STATUS_FILE | grep 'bridge'` || true
  if  [ -z "$PI_STATUS" ]; then
     echo "Failed - WLAN Pi is not in bridge mode."
     exit 1
  fi

  echo "Disabling bridge mode..."
  # Remove sym links to bridge mode
  if [ -e $NETWORK_DIR/eth0.network ]; then
    unlink $NETWORK_DIR/eth0.network
  fi
  
  if [ -e $NETWORK_DIR/eth1.network ]; then
    unlink $NETWORK_DIR/eth1.network
  fi

  if [ -e $NETWORK_DIR/usb1.network ]; then
    unlink $NETWORK_DIR/usb1.network
  fi

  if [ -e $NETWORK_DIR/br0.netdev ]; then
    unlink $NETWORK_DIR/br0.netdev
  fi

  if [ -e $NETWORK_DIR/br0.network ]; then
    unlink $NETWORK_DIR/br0.network
  fi

  # Restore old files
  if [ -e $NETWORK_DIR/eth0.network.brm ]; then
    cp $NETWORK_DIR/eth0.network.brm $NETWORK_DIR/eth0.network
  fi

  if [ -e $NETWORK_DIR/eth1.network.brm ]; then
    cp $NETWORK_DIR/eth1.network.brm $NETWORK_DIR/eth1.network
  fi

  if [ -e $NETWORK_DIR/usb1.network.brm ]; then
    cp $NETWORK_DIR/usb1.network.brm $NETWORK_DIR/usb1.network
  fi

  if [ -e /etc/network/interfaces.brm ]; then
    cp /etc/network/interfaces.brm /etc/network/interfaces
  fi

  echo "WLAN Pi will now reboot"
  echo "classic" > $STATUS_FILE
  sleep 1
  reboot

}


status () {

  PI_STATUS=`cat $STATUS_FILE | grep 'bridge'` || true
  if  [ -z "$PI_STATUS" ]; then
    echo "bridge mode is currently disabled"
    exit 0
  else
    echo "bridge mode is currently enabled"
    exit 0
  fi

}


case "$1" in
  on)
        bridge_on
        ;;
  off)
        bridge_off
        ;;
  status)
        status
        ;;
  *)
        N=/etc/wlanpi-bridge/$NAME
        echo "Usage: $N {on|off|status}" >&2
        exit 1
        ;;
esac

exit 0