#!/bin/bash

# import node variables
source /home/pi/monitoring/common/scripts/common_var

# import global functions
source /home/pi/monitoring/common/scripts/common_functions

# local variables
now="0"
intf_airodump=""

# functions
function print_usage() {
cat <<EOF
Usage:

  node_dump [OPTION]...

Options:

    -n, --now
      Disables the default sleep before starting monitoring (sleep
      duration is set to ${DUMP_SLEEP}, which is defined in node_var).

    -w, --wlan INTERFACE[ INTERFACE...]
      Specify one or more 2.4GHz wlan interface(s).
    
    -w5, --wlan5 INTERFACE[ INTERFACE...]
      Specify one or more 5GHz wlan interface(s).
    
    -b, --bt INTERFACE [AMNESIA]
      Specify exactly one bluetooth interface.
      Amnesia mode makes Bluelog forget logged devices older than set
      ammount of time in minutes (device will be logged again, as if
      it was seen for the first time).
    
    --help
      Print help page.

Now option affects ONLY interfaces specified AFTER this option.
All specified interfaces MUST exist.
Multiple wlan interfaces MUST be enclosed in apostrophes (').

Examples:

  node_dump -b hci0
  node_dump -n -w5 usbwlan5ghz
  node_dump -w usbwlan0 -n -b hci0 15
  node_dump -w 'usbwlan0 usbwlan1 usbwlan2'

EOF
}

function print_help() {
cat <<EOF
  
NODE: START MONITORING

This command is used to start monitoring and dumping log files with
specified bluetooth/wlan interface(s). Wlan interfaces will be put into
monitor mode prior to calling airodump-ng. Only one Bluetooth interface
can be specified and it will use Bluelog for logging. Amnesia mode can
be used to monitor number of Bluetooth devices present in set time period.

There is separate option for 2.4GHz and 5GHz wlan interfaces (-w and -w5)
to make sure that 5GHz adapters that also support 2.4GHz jump only on 5GHz
channels.

Interfaces will be setup in order in which they are written in command
options.

By default this script will sleep for $DUMP_SLEEP (defined in node_var)
before starting monitoring.

Processes spawned by this script will be run in background with output
redirected to /dev/null. Only logs that they create will be preserved.

Log location:         $DIR_NODE_LOGS
Bluelog location:     $DIR_NODE_BLUELOG

2.4GHz wlan log name: $LOG_NODE_AIRODUMP
5GHz wlan log name:   $LOG_NODE_AIRODUMP_5GHZ
Bluelog log name:     $LOG_NODE_BLUELOG

Location cannot be changed because scripts on remote devices might be
depending on it!

EOF
  print_usage
}

# options parsing

if [[ $# -eq 0 ]];
then
  echo ""
  print_usage
  exit 1
fi

while [ $# -gt 0 ]
do
  case $1 in
    --help)
      print_help
      exit 0
      ;;

    -n|--now)
      now="1";
      ;;

    -w|--wlan)
      # check if argument -w empty
      if [[ -z "$2" ]] || [[ "$2" == -* ]];
      then
        echo "ERROR: invalid argument $2 for option $1"
        print_usage
	exit 1
        break
      fi

      if [[ "$now" == "0" ]];
      then
        echo "Sleeping for ${DUMP_SLEEP}..."
        sleep "$DUMP_SLEEP" 
      fi
     
      # change wlan interface(s) into monitoring mode
      # and create string in airodump-ng format
      intf_airodump="";
      for i in $2
      do
        echo "Changing interface $i into monitoring mode..."
        sudo ip link set "$i" down
        sudo iw "$i" set monitor control
        sudo ip link set "$i" up
        # append interface, to string
        intf_airodump="${intf_airodump}${i},"
      done
      # remove last character (,) from string
      intf_airodump="${intf_airodump::-1}"

      # check existence of node log directory
      check_log_dir "node"

      echo "Starting airodump-ng with 2.4GHz interface(s) ${2}..."
      sudo airodump-ng -w "${DIR_NODE_LOGS}/${LOG_NODE_AIRODUMP}" -o csv -b bg "$intf_airodump" &>/dev/null &
     
      shift
      ;;

    -w5|--wlan5)
      # check if argument -w5 empty
      if [[ -z "$2" ]] || [[ "$2" == -* ]];
      then
        echo "ERROR: invalid argument $2 for option $1"
        print_usage
	exit 1
        break
      fi

      if [[ "$now" == "0" ]];
      then
        echo "Sleeping for ${DUMP_SLEEP}..."
        sleep "$DUMP_SLEEP" 
      fi
     
      # change wlan interface(s) into monitoring mode
      # and create string in airodump-ng format
      intf_airodump="";
      for i in $2
      do
        echo "Changing interface $i into monitoring mode..."
        sudo ip link set "$i" down
        sudo iw "$i" set monitor control
        sudo ip link set "$i" up
        # append interface, to string
        intf_airodump="${intf_airodump}${i},"
      done
      # remove last character (,) from string
      intf_airodump="${intf_airodump::-1}"

      # check existence of node log directory
      check_log_dir "node"

      echo "Starting airodump-ng with 5GHz interface(s) ${2}..."
      sudo airodump-ng -w "${DIR_NODE_LOGS}/${LOG_NODE_AIRODUMP_5GHZ}" -o csv -b a "$intf_airodump" &>/dev/null &
     
      shift
      ;;

    -b|--bt)
      # arguments
      # $2 - Bluetooth interface
      # $3 - [amnesia minutes] (optional)

      # check if argument $2 empty
      if [[ -z "$2" ]] || [[ "$2" == -* ]];
      then
        echo "ERROR: invalid argument '${2}' for option ${1}!"
        print_usage
	exit 1
        break
      fi
      
      if [[ "$now" == "0" ]];
      then
        echo "Sleeping for ${DUMP_SLEEP}..."
        sleep "$DUMP_SLEEP" 
      fi
      
      echo "Killing Bluelog process if there is one..."
      "${DIR_NODE_BLUELOG}/bluelog" -k

      # check existence of node log directory
      check_log_dir "node"
      
      # check optional argument $3
      if [[ -z "$3" ]] || [[ "$3" == -* ]];
      then
        echo "Starting Bluelog with interface ${2}..."
        "${DIR_NODE_BLUELOG}/bluelog" -o "${DIR_NODE_LOGS}/${LOG_NODE_BLUELOG}.log" -i "$2" -nmft &>/dev/null &
      else
        echo "Starting Bluelog with interface ${2} with amnesia mode set to ${3} minutes..."
        "${DIR_NODE_BLUELOG}/bluelog" -o "${DIR_NODE_LOGS}/${LOG_NODE_BLUELOG}.log" -i "$2" -nmft -a "$3" &>/dev/null &
        shift
      fi

      shift
      ;;

    *)
      echo "ERROR: option $1 uknown!"
      print_usage
      exit 1
      break
      ;;
  esac
  shift
done
