#!/bin/bash
# Version: 0.3.0-bwbar
# Copyright (c) 2005-2022:
#   Darren 'Tadgy' Austin <darren (at) afterdark.org.uk>
# Licensed under the terms of the GNU General Public License version 3.

# shellcheck disable=SC2016,SC1090

BWBAR_EXEC="/opt/sbin/bwbar"
DAEMON_EXEC="/usr/bin/daemon"
BWBAR_ARGS=('-f' '/run/bwbar.txt' '-l' '/run/bwbar-light.png' '-d' '/run/bwbar-dark.png' '${MONINTERFACE:-eth0}' '$(ethtool "${MONINTERFACE:-eth0}" | sed -nre "/Speed:/ s/[^[:digit:]]*([[:digit:]]+).*/\1/ p")' '--Gbps')
DAEMON_ARGS=('-N' '-n' "${0##*rc.}" '-r' '-a' '60' '-A' '5' '-L' '3600' '-M' '3' '-l' 'daemon.err' '-b' 'daemon.debug' '-o' 'daemon.info' '--')
RUNUSER="$(whoami)"

# Allow configuration in /etc/default to override.
# Additional available variables:
#   DAEMON_ENVIRONMENT=()	# Extra environment passed to $DAEMON_EXEC.  Must be an array.
#   RUNUSER=""			# Username to run the daemon as.  Default: User who starts the script.
#   MONINTERFACE=""		# The interface to monitor for bandwidth usage.  Default: eth0.
#   SLAY_DELAY=""		# Delay between the SIGTERM and SIGKILL on a 'stop'.  Default: 2s.
#   RESTART_DELAY=""		# Delay between stopping and starting on a 'restart'.  Default: 2s.
[[ -e "/etc/default/${0##*rc.}" ]] && { source "/etc/default/${0##*rc.}" || return 1 2>/dev/null || exit 1; }

error() {
  printf "%s: %s: %s\\n" "${BASH_SOURCE[0]##*/}" "${BWBAR_EXEC##*/}" "$*" >&2
}

checkconfigured() {
  # This function can be used to perform any pre-start tests; hopfully to insure the daemon
  # can start correctly, before actually trying to start it.  A return value of 0 means the
  # tests were passed and the daemon should be started.  Any other value prevents the
  # daemon from being started, and an error message will be emitted.
  id "${RUNUSER:-$(whoami)}" >/dev/null 2>&1 || return 1
  ip link show "${MONINTERFACE:-eth0}" >/dev/null 2>&1 || return 1
  return 0
}

checkstatus() {
  local RET
  if su - "${RUNUSER:-$(whoami)}" -c "\"$DAEMON_EXEC\" --running -n \"${0##*rc.}\""; then
    printf "%s: %s: %s\\n" "${BASH_SOURCE[0]##*/}" "${BWBAR_EXEC##*/}" "running"
    RET=0
  else
    printf "%s: %s: %s\\n" "${BASH_SOURCE[0]##*/}" "${BWBAR_EXEC##*/}" "stopped"
    RET=1
  fi
  return $RET
}

startdaemon() {
  checkconfigured || {
    error "not started - pre-start checks failed"
    return 2
  }
  su - "${RUNUSER:-$(whoami)}" -c "${DAEMON_ENVIRONMENT:+declare ${DAEMON_ENVIRONMENT[*]};} \"$DAEMON_EXEC\" ${DAEMON_ARGS[*]} \"$BWBAR_EXEC\" ${BWBAR_ARGS[*]}"
  # shellcheck disable=SC2181
  if (( $? != 0 )); then
    error "error starting 'daemon'"
    return 2
  else
    return 0
  fi
}

stopdaemon() {
  su - "${RUNUSER:-$(whoami)}" -c "\"$DAEMON_EXEC\" -n \"${0##*rc.}\" --stop 2>/dev/null"
  sleep "${SLAY_DELAY:-2}"
  checkstatus >/dev/null && {
    error "failed to stop gracefully - slaying"
    su - "${RUNUSER:-$(whoami)}" -c "\"$DAEMON_EXEC\" -n \"${0##*rc.}\" --signal=kill 2>/dev/null"
  }
  return 0
}

for EXEC in "$BWBAR_EXEC" "$DAEMON_EXEC"; do
  [[ ! -e "$EXEC" ]] && {
    error "$EXEC:" "not found"
    return 1 2>/dev/null || exit 1
  }
  [[ ! -x "$EXEC" ]] && {
    error "$EXEC:" "not executable"
    return 1 2>/dev/null || exit 1
  }
done

case "$1" in
  'start')
    if checkstatus >/dev/null; then
      error "already running"
      printf "  %s\\n" "Try: ${BASH_SOURCE[0]} status" >&2
      RET=1
    else
      startdaemon
      RET=$?
    fi
    ;;
  'stop')
    if ! checkstatus >/dev/null; then
      error "not running"
      printf "  %s\\n" "Try: ${BASH_SOURCE[0]} status" >&2
      RET=1
    else
      stopdaemon
      RET=$?
    fi
    ;;
  'restart')
    checkstatus >/dev/null
    (( $? != 3 )) && {
      stopdaemon >/dev/null 2>&1
      sleep "${RESTART_DELAY:-2}"
    }
    startdaemon
    RET=$?
    ;;
  'status')
    checkstatus
    RET=$?
    ;;
  *)
    printf "%s\\n" "Usage: ${BASH_SOURCE[0]} <start|stop|restart|status>" >&2
    RET=1
    ;;
esac

return $RET 2>/dev/null || exit $RET
