#!/bin/dash

###############################################################################
# FireTray v0.2 for Fatdog64 by SFR'2016									  #
# GNU GPL v2 applies														  #
# Req.: sit, gtkmenuplus, eztables											  #
###############################################################################

export TEXTDOMAIN=firetray
export OUTPUT_CHARSET=UTF-8

[ $(id -u) -ne 0 ] && exec gtksu "Firewall Tray Icon" $0 "$@"

APPNAME="FireTray v0.1"

# -----------------------------------------------------------------------------

INTERVAL=3	# in seconds

ICON_ON='/usr/share/pixmaps/midi-icons/ok.xpm'
ICON_OFF='/usr/share/pixmaps/midi-icons/error.xpm'

#FIREWALL_STATUS='service eztables status | grep -q running'
FIREWALL_STATUS='[ $(iptables -L -n | wc -l) -gt 10 ]'	# it's faster directly
FIREWALL_START='service eztables start'
FIREWALL_STOP='service eztables stop'
FIREWALL_CONFIG='/etc/eztables/eztables.cfg'

# -----------------------------------------------------------------------------

WORKDIR="/tmp/firetray_${USER}_${$}"
TMPICON="${WORKDIR}/icon"
TMPTOOLTIP="${WORKDIR}/tooltip"
MENU="${WORKDIR}/menu"

mkdir -p "$WORKDIR"

trap 'kill -0 $SIT_PID 2>/dev/null && kill $SIT_PID; rm -rf "${WORKDIR}"; exit' 0 INT TERM HUP

# -----------------------------------------------------------------------------

func_generate_menu () {
echo '
iconsize = 24

format = color="black" weight="bold"

item = '"$APPNAME"'
cmd = 

SEPARATOR

item =  <span color="green">'"$(gettext "Turn Firewall On")"'</span>
cmd = '$FIREWALL_START'
icon = '"$ICON_ON"'                                 
                                                               
item = <span color="red">'"$(gettext "Turn Firewall Off")"'</span>
cmd = '$FIREWALL_STOP'         
icon = '"$ICON_OFF"'

SEPARATOR

item = <span color="blue">'"$(gettext "Edit config file")"'</span>
cmd = defaulttexteditor '"$FIREWALL_CONFIG"'                          
icon = gtk-edit

SEPARATOR

item = <span color="black">'"$(gettext "Quit")"'</span>
cmd = 'kill $$ $SIT_PID'		# kill script _and_ sit to exit immediately
icon = gtk-quit

' > "$MENU"
}

# -----------------------------------------------------------------------------

func_update_status () {
  if eval $FIREWALL_STATUS; then
	CURRENT=on
    ICON="$ICON_ON"
    TOOLTIP="$(gettext "Firewall is running.")"
  else
	CURRENT=off
    ICON="$ICON_OFF"
    TOOLTIP="$(gettext "Firewall is stopped.")"
  fi
  
  if [ "$CURRENT" != "$PREVIOUS" ]; then
    # Update icon and tooltip
    kill -STOP $SIT_PID
    ln -sfT "$ICON" "$TMPICON"
    echo -n "$TOOLTIP" > "$TMPTOOLTIP"
    kill -CONT $SIT_PID
    sleep 0.1; touch -h "$TMPICON"	# needs extra touch, or else won't be recognized by sit (sometimes happens after restarting X)
  fi

  PREVIOUS="$CURRENT"
}

# -----------------------------------------------------------------------------

### START ###

PREVIOUS=none
ln -sfT "$ICON_OFF" "$TMPICON"
echo -n " " > "$TMPTOOLTIP"

sit "$TMPICON" "$TMPTOOLTIP" "gtkmenuplus "${MENU}"" "gtkmenuplus "${MENU}"" &
SIT_PID=$!

func_generate_menu	# must be here, to catch $SIT_PID

while true; do
  func_update_status
  sleep $INTERVAL
done

exit

# =============================================================================
 
