#!/bin/sh -e
#
#    release: grab the os/distro release
#    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/>.

PKG="byobu"

if [ -r "$HOME/.$PKG/distro" ]; then
	printf "\005{+b }%s\005{-} " `cat "$HOME/.$PKG/distro"`
elif which lsb_release >/dev/null 2>&1; then
	if [ "$1" = "--detail" ]; then
		lsb_release -a 2>/dev/null
		exit 0
	fi
	# If lsb_release is available, use it
	r=$(lsb_release -s -d)
	if echo "$r" | grep -qs "^Ubuntu .*\..*\..*$"; then
		# Use the -d if an Ubuntu LTS
		printf "\005{+b }%s\005{-} " "$r"
	else
		# But for other distros the description
		# is too long, so build from -i and -r
		i=$(lsb_release -s -i)
		r=$(lsb_release -s -r)
		printf "\005{+b }%s %s\005{-} " "$i" "$r"
	fi
elif [ -r "/etc/issue" ]; then
	if [ "$1" = "--detail" ]; then
		cat /etc/issue
		exit 0
	fi
	# Otherwise, grab part of /etc/issue, ideally the distro and version
	printf "\005{+b }%s\005{-} " $(head -n1 /etc/issue | sed "s/ [^0-9]* / /" | awk '{print $1 " " $2}')
else
	echo "Byobu"
fi
