#!/bin/sh

# prepare variables
IFACE=pan0
HCI_PATH=$(/usr/bin/dbus-send --system --type=method_call --print-reply --dest=org.bluez / org.freedesktop.DBus.ObjectManager.GetManagedObjects | grep -m 1 -o '/org/bluez/hci[[:digit:]]\+')
IPCFG_DIR=/var/pan
DHCP_DAEMON=$IPCFG_DIR/dhcpd-$IFACE
RADV_DAEMON=$IPCFG_DIR/radvd-$IFACE
DHCP4_CONF=$IPCFG_DIR/dhcpd4-$IFACE.conf
DHCP6_CONF=$IPCFG_DIR/dhcpd6-$IFACE.conf
DHCP4_LEASES=$IPCFG_DIR/dhcpd4-$IFACE.leases
DHCP6_LEASES=$IPCFG_DIR/dhcpd6-$IFACE.leases
RADV_CONF=$IPCFG_DIR/radvd-$IFACE.conf
RADV_PID=$IPCFG_DIR/radvd-$IFACE.pid
DEFAULT_LEASE_TIME=3600
. /opt/bluetooth/etc/settings
. /etc/rc.d/init.d/ip

case "$1" in

  start)
    # create bridge interface
    if [ -z "$(/usr/sbin/brctl show $IFACE 2>/dev/null)" ]; then
      /usr/sbin/brctl addbr $IFACE
      /usr/sbin/brctl setfd $IFACE 0
      /usr/sbin/brctl stp $IFACE off
      if [ -n "$MOD_BLUETOOTH_PAN_IPADDR4" ]; then
        /sbin/ip -4 addr add $MOD_BLUETOOTH_PAN_IPADDR4/$MOD_BLUETOOTH_PAN_NETMASK4 dev $IFACE
      fi
      if [ -n "$MOD_BLUETOOTH_PAN_IPADDR6" ]; then
        /sbin/ip -6 addr add $MOD_BLUETOOTH_PAN_IPADDR6/$MOD_BLUETOOTH_PAN_PREFIX6 dev $IFACE
      fi
      /sbin/ip link set $IFACE up
    fi

    # start DHCP4 server for bridge interface
    if [ -n "$MOD_BLUETOOTH_PAN_IPADDR4" -a -n "$MOD_BLUETOOTH_PAN_POOLLO4" ]; then
      mkdir -p $IPCFG_DIR
      ln -sf /usr/sbin/dhcpd $DHCP_DAEMON

      echo "max-lease-time 86400;" > $DHCP4_CONF
      echo "min-lease-time 60;" >> $DHCP4_CONF
      echo "subnet `ip_calc_network $MOD_BLUETOOTH_PAN_IPADDR4 $MOD_BLUETOOTH_PAN_NETMASK4` netmask $MOD_BLUETOOTH_PAN_NETMASK4 {" >> $DHCP4_CONF
      echo "  range $MOD_BLUETOOTH_PAN_POOLLO4 $MOD_BLUETOOTH_PAN_POOLHI4;" >> $DHCP4_CONF
      echo "  option routers $MOD_BLUETOOTH_PAN_IPADDR4;" >> $DHCP4_CONF
      echo -n "  option domain-name-servers " >> $DHCP4_CONF
      if [ -n "$MOD_BLUETOOTH_PAN_DNS4" ]; then
        echo -n $MOD_BLUETOOTH_PAN_DNS4 >> $DHCP4_CONF
      else
        echo -n $MOD_BLUETOOTH_PAN_IPADDR4 >> $DHCP4_CONF
      fi
      echo ";" >> $DHCP4_CONF
      if [ -n "$MOD_BLUETOOTH_PAN_LEASETIME4" ]; then
        echo "  default-lease-time $MOD_BLUETOOTH_PAN_LEASETIME4;" >> $DHCP4_CONF
      else
        echo "  default-lease-time $DEFAULT_LEASE_TIME;" >> $DHCP4_CONF
      fi
      echo "}" >> $DHCP4_CONF

      touch $DHCP4_LEASES
      $DHCP_DAEMON -4 -q -user daemon -cf $DHCP4_CONF -lf $DHCP4_LEASES --no-pid $IFACE
    fi

    # start RADV and DHCP6 server for bridge interface
    if [ -n "$MOD_BLUETOOTH_PAN_IPADDR6" -a -n "$MOD_BLUETOOTH_PAN_POOLLO6" ]; then
      mkdir -p $IPCFG_DIR
      ln -sf /usr/sbin/dhcpd $DHCP_DAEMON
      ln -sf /usr/sbin/radvd $RADV_DAEMON

      echo "max-lease-time 86400;" > $DHCP6_CONF
      echo "min-lease-time 60;" >> $DHCP6_CONF
      echo "subnet6 `ip_calc_network6 $MOD_BLUETOOTH_PAN_IPADDR6 $MOD_BLUETOOTH_PAN_PREFIX6`/$MOD_BLUETOOTH_PAN_PREFIX6 {" >> $DHCP6_CONF
      echo "  range6 $MOD_BLUETOOTH_PAN_POOLLO6 $MOD_BLUETOOTH_PAN_POOLHI6;" >> $DHCP6_CONF
      if [ -n "$MOD_BLUETOOTH_PAN_DNS6" ]; then
        echo -n "  option dhcp6.name-servers $MOD_BLUETOOTH_PAN_DNS6;" >> $DHCP6_CONF
      else
        echo -n "  option dhcp6.name-servers $MOD_BLUETOOTH_PAN_IPADDR6;" >> $DHCP6_CONF
      fi
      if [ -n "$MOD_BLUETOOTH_PAN_LEASETIME6" ]; then
        echo "  default-lease-time $MOD_BLUETOOTH_PAN_LEASETIME6;" >> $DHCP6_CONF
      else
        echo "  default-lease-time $DEFAULT_LEASE_TIME;" >> $DHCP6_CONF
      fi
      echo "}" >> $DHCP6_CONF

      echo "interface $IFACE" > $RADV_CONF
      echo "{" >> $RADV_CONF
      echo "  AdvSendAdvert on;" >> $RADV_CONF
      echo "  AdvManagedFlag on;" >> $RADV_CONF
      echo "  prefix `ip_calc_network6 $MOD_BLUETOOTH_PAN_IPADDR6 $MOD_BLUETOOTH_PAN_PREFIX6`/$MOD_BLUETOOTH_PAN_PREFIX6" >> $RADV_CONF
      echo "  {" >> $RADV_CONF
      echo "    AdvOnLink on;" >> $RADV_CONF
      echo "    AdvAutonomous on;" >> $RADV_CONF
      if [ -n "$MOD_BLUETOOTH_PAN_LEASETIME6" ]; then
        echo "    AdvValidLifetime $MOD_BLUETOOTH_PAN_LEASETIME6;" >> $RADV_CONF
        echo "    AdvPreferredLifetimee $MOD_BLUETOOTH_PAN_LEASETIME6;" >> $RADV_CONF
      else
        echo "    AdvValidLifetime $DEFAULT_LEASE_TIME;" >> $RADV_CONF
        echo "    AdvPreferredLifetime $DEFAULT_LEASE_TIME;" >> $RADV_CONF
      fi
      echo "  };" >> $RADV_CONF
      if [ -n "$MOD_BLUETOOTH_PAN_DNS6" ]; then
        echo "  RDNSS $MOD_BLUETOOTH_PAN_DNS6" >> $RADV_CONF
      else
        echo "  RDNSS $MOD_BLUETOOTH_PAN_IPADDR6" >> $RADV_CONF
      fi
      echo "  {" >> $RADV_CONF
      echo "  };" >> $RADV_CONF
      echo "};" >> $RADV_CONF

      touch $DHCP6_LEASES
      $RADV_DAEMON -u daemon -C $RADV_CONF -p $RADV_PID
      $DHCP_DAEMON -6 -q -user daemon -cf $DHCP6_CONF -lf $DHCP6_LEASES --no-pid $IFACE
    fi

    # create Bluetooth PAN service
    /usr/bin/dbus-send --system --type=method_call --dest=org.bluez $HCI_PATH org.bluez.NetworkServer1.Register string:"nap" string:"$IFACE"

    exit 0
  ;;

  stop)
    /usr/bin/dbus-send --system --type=method_call --dest=org.bluez $HCI_PATH org.bluez.NetworkServer1.Unregister string:"nap"
    killall -q SIGTERM dhcpd-$IFACE
    killall -q SIGTERM radvd-$IFACE
    rm -rf $DHCP_DAEMON $DHCP4_CONF $DHCP6_CONF $DHCP4_LEASE $DHCP6_LEASE $RADV_DAEMON $RADV_CONF $RADV_PID
    /usr/bin/ip link delete $IFACE 2>/dev/null
    exit 0
  ;;

  status)
    if [ -z "$(/usr/sbin/brctl show $IFACE 2>/dev/null)" ]; then
      echo "Bridge interface $IFACE does not exist"
      exit 1
    fi
    if [ -z "$(/sbin/ip link show $IFACE | grep UP)" ]; then
      echo "Bridge interface $IFACE si down"
      exit 1
    fi
    if ! killall -q -0 dhcpd-$IFACE; then
      echo "DHCP server for $IFACE interface is not running"
      exit 1
    fi
    if [ -z "$(bluetoothctl show | grep "UUID: NAP ")" ]; then
      echo "NAP service with is not registered"
      exit 1
    fi
    echo "PAN is fully working"
    echo "Connected clients: $(/usr/sbin/brctl show $IFACE | grep -o bnep | wc -l)"
    exit 0
  ;;

esac