#!/bin/bash

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

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

  node_service [OPTION]...

Options:

    -w, --wlan [ARGUMENT]
      Manages 2.4GHz wlan service ${SERVICE_NODE_WLAN}
    
    -w5, --wlan5 [ARGUMENT]
      Manages 5GHz wlan service ${SERVICE_NODE_WLAN_5GHZ}
    
    -b, --bt [ARGUMENT]
      Manages Bluetooth service ${SERVICE_NODE_WLAN}
    
    --help
      Print help page.

Arguments:

  start
    Start monitoring service until shutdown/stop.
  
  stop
    Stop monitoring service until start/reboot (if enabled).
  
  enable
    Enable automatic monitoring service start after boot
    (still can be controlled by start/stop for current session).
  
  disable
    Disable automatic monitoring after startup
    (still can be controlled by start/stop for current session).
  
  status
    Show status of given systemd service.

Examples:

  node_service -w status
  node_service -w5 enable
  node_service -b start

EOF
}

function print_help() { 
cat <<EOF

NODE: MANAGE SYSTEMD SERVICES

This command is used to start/stop monitoring through systemd service,
to enable/disable automatic start of this service at system boot or to
show current status of specified service.

For more information about monitoring try using 'node_dump --help'.
For more information about systemd services try using 'node_cs --help'.

There is separate option for 2.4GHz and 5GHz wlan interfaces (-w and -w5).

Systemd directory:        ${DIR_SYSTEMD_SERVICE}

2.4GHz wlan service name: ${SERVICE_NODE_WLAN}
5GHz wlan service name:   ${SERVICE_NODE_WLAN_5GHZ}
Bluetooth service name:   ${SERVICE_NODE_BT}

Location cannot be altered because scripts on this or remote device
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      
      ;;

    -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

      case $2 in
        start|stop|enable|disable|status)
          echo "systemctl ${SERVICE_NODE_WLAN}: $2"
          systemctl "$2" "${SERVICE_NODE_WLAN}"
          ;;

        *)
          echo "ERROR: argument $2 unknown!"
          print_usage
	  exit 1
          break
          ;;
      esac
      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

      case $2 in
        start|stop|enable|disable|status)
          echo "Performing action: $2 systemctl ${SERVICE_NODE_WLAN_5GHZ}"
          systemctl "$2" "${SERVICE_NODE_WLAN_5GHZ}"
          ;;

        *)
          echo "ERROR: argument $2 unknown!"
          print_usage
	  exit 1
          break
          ;;
      esac
      shift
      ;;

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

      case $2 in
        start|stop|enable|disable|status)
          echo "Performing action: $2 systemctl ${SERVICE_NODE_BT}"
          systemctl "$2" "${SERVICE_NODE_BT}"
          ;;

        *)
          echo "ERROR: argument $2 unknown!"
          print_usage
	  exit 1
          break;
          ;;
      esac
      shift
      ;;

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

