#!/bin/bash
# This file is a simple script that start / stop / restart 
# services which is used from launchers
# It use menuexecg to start / stop / restart services and 
# show a notification to user
# The idea is we get rid of terminal with no useful usage
# author: Nong Hoang Tu <dmknght@parrotsec.org>

# Excepted usage: servicexc <name> <start / stop / restart>

export PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:$PATH

service_name="$1"
service_action="$2"
service_status="$(systemctl is-active $1)"
service_title="$service_name $service_action"
# Check status of current service
# If it is started, do not start it again
# if it is not started, do not stop or restart

if [ "$service_action" = "start" ] && [ "$service_status" = "active" ] ; then
  /usr/bin/notify-send "$service_title error" "$service_name is already activated!"
  exit
fi

if ([ "$service_action" = "stop" ] || [ "$service_action" == "restart" ]) && [ "$service_status" = "inactive" ] ; then
  /usr/bin/notify-send "$service_title error" "$service_name is not started!"
  exit
fi

"/usr/sbin/service" "$service_name" "$service_action"
service_status="$(systemctl is-active $1)"
# This is the command to show notification without icon
/usr/bin/notify-send "$service_name $service_status" "Command \"$service_name $service_action\" executed"
