#!/bin/bash
# Copyright (C) 2014 Julien Bonjean <julien@bonjean.info>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

BUTTON=${button:-}
TYPE="${BLOCK_INSTANCE:-mem}"
VALUE_COLOR=${color:-$(xrescat i3xrocks.value.color "#D8DEE9")}
LABEL_ICON=${label_icon:-$(xrescat i3xrocks.label.memory M)}
LABEL_COLOR=${label_color:-$(xrescat i3xrocks.label.color "#7B8394")}
VALUE_FONT=${font:-$(xrescat i3xrocks.value.font "JetBrains Mono Medium 13")}
DISP=${display:-$(xrescat i3xrocks.memory.display "free")}
PERCENTAGE=${percentage:-$(xrescat i3xrocks.memory.percentage "false")}

awk -v type=$TYPE -v display=$DISP -v percentage=$PERCENTAGE -v vc="$VALUE_COLOR" -v li="$LABEL_ICON" -v lc="$LABEL_COLOR" -v vf="$VALUE_FONT" '
/^MemTotal:/ {
	mem_total=$2
}
/^MemFree:/ {
	mem_free=$2
}
/^Buffers:/ {
	mem_free+=$2
}
/^Cached:/ {
	mem_free+=$2
}
/^MemAvailable:/ {
	mem_available=$2
}
/^SwapTotal:/ {
	swap_total=$2
}
/^SwapFree:/ {
	swap_free=$2
}
END {
	# fallback for mem_available
	if (mem_available == "")
		mem_available=mem_free
	# full text
	if (type == "swap") {
		to_display = swap_free/1024/1024
		if (display == "used")
			to_display = swap_total/1024/1024 - to_display
		if (percentage == "true")
			printf("<span font_desc=\"%s\" color=\"%s\">%s</span><span font_desc=\"%s\" color=\"%s\"> %02d%%</span>\n", vf, lc, li, vf, vc, 100 * to_display / (swap_total/1024/1024))
		else
			printf("<span font_desc=\"%s\" color=\"%s\">%s</span><span font_desc=\"%s\" color=\"%s\"> %.1fG</span>\n", vf, lc, li, vf, vc, to_display)
	}
	else {
		to_display = mem_free/1024/1024
		if (display == "used")
			to_display = mem_total/1024/1024 - to_display
		if (percentage == "true")
			printf("<span font_desc=\"%s\" color=\"%s\">%s</span><span font_desc=\"%s\" color=\"%s\"> %02d%%</span>\n", vf, lc, li, vf, vc, 100 * to_display / (mem_total/1024/1024))
		else
			printf("<span font_desc=\"%s\" color=\"%s\">%s</span><span font_desc=\"%s\" color=\"%s\"> %.1fG</span>\n", vf, lc, li, vf, vc, to_display)
	}
}
' /proc/meminfo

if [ "x${BUTTON}" == "x1" ]; then
    ACTION=$(xrescat i3xrocks.action.memory "/usr/bin/gnome-system-monitor --class=floating_window --show-resources-tab")
    /usr/bin/i3-msg -q exec "$ACTION"
fi
