#!/bin/sh -e
#
#    byobu-status
#    Copyright (C) 2008 Canonical Ltd.
#
#    Authors: Dustin Kirkland <kirkland@canonical.com>
#
#    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, version 3 of the License.
#
#    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/>.

###########################################################
# We should be really "nice" about gathering status:
#renice 10 $$ >/dev/null 2>&1 || true
#ionice -c3 -p $$ >/dev/null 2>&1 || true
# However, this *really* slows down startup.
# We need a good way of only doing this for updates,
# but not at screen startup.
###########################################################

PKG="byobu"

migrate() {
	OLDPKG="screen-profiles"
        # Rename the config dir
        mv "$HOME/.$OLDPKG" "$HOME/.$PKG"
        ln -s "$HOME/.$PKG" "$HOME/.$OLDPKG"
        # Replace all instances of the old package name with the new
        find "$HOME/.$PKG" -type f | xargs sed -i "s/$OLDPKG/$PKG/g"
        # Fix the chosen profile symlink
        if [ -h "$HOME/.$PKG/profile" ]; then
                # Determine the chosen profile color
                profile=`stat "$HOME/.$PKG/profile" | head -n1 | sed "s:^.*[/-]::" | sed "s:'$::"`
                # Try to set that color, if it exists, otherwise default to light
                select-screen-profile -s "$profile" || select-screen-profile -s "light"
        fi
}

# If the old config dir exists, but the new one doesn't, provide a migration mechanism
[ -d "$HOME/.$OLDPKG" -a ! -d "$HOME/.$PKG" ] && migrate

if [ -d "$HOME/.$PKG/bin" ]; then
	DIR="$HOME/.$PKG/bin"
elif [ -d "/usr/lib/$PKG" ]; then
	DIR="/usr/lib/$PKG"
else
	exit 1
fi

P="$1"

case "$P" in
	# default = on, user must override to turn off
	cpu-count|cpu-freq|date|load-average|logo|mem-available|mem-used|menu|reboot-required|release|time|updates-available|uptime)
		grep -qs -m1 "^$P=0$" "$HOME/.$PKG/status" && exit 0
	;;
	# default = off, user must override to turn on
	arch|battery|disk-available|disk-used|ec2-cost|hostname|ip-address|network-down|network-up|processes|users|temp-c|temp-f|whoami|wifi-quality)
		grep -qs -m1 "^$P=1$" "$HOME/.$PKG/status" || exit 0
	;;
	--detail)
		VER=
		if [ -x "/usr/bin/dpkg-query" ]; then
			VER=`/usr/bin/dpkg-query --show $PKG | awk '{print "-" $2 }'`
		fi
		printf "$PKG$VER Detailed Status Navigation\n  Expand all - zr\t\tCollapse all - zm\n  Expand one - zo\t\tCollapse one - zc\n\n"
		for i in `ls "$DIR"`; do
			[ "$i" = "menu" ] && continue
			short=`"$DIR"/$i --short | sed 's/^\s*//' | sed 's/\s*$//' | sed 's/.{[^}]*}//g'` || true
			detail=`"$DIR"/$i --detail | sed '/^$/d' | sed 's/^/\t/g'` || true
			printf "%s\n\t(%s)\n" "$short" "$i"
			[ -n "$detail" ] && printf "%s\n" "$detail"
		done
		exit 0
	;;
	*)
		exit 1
	;;
esac

if [ -f "$DIR"/"$P" -a -x "$DIR"/"$P" ]; then
	exec "$DIR"/"$P"
fi
