#!/bin/sh -e
#
#    byobu - wrapper script
#    Copyright (C) 2008-2009 Canonical Ltd.
#
#    Authors: Dustin Kirkland <kirkland@ubuntu.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/>.

VERSION=4.37
PKG="byobu"

# All sorts of things go wrong if you don't own your $HOME dir.
# This happens under sudo, if you don't use the -H option; Byobu will
# create a bunch of files in your $HOME which will be owned by root.
if [ ! -O "$HOME" ]; then
	echo "Cannot run $PKG because [$USER] does not own [$HOME]" 1>&2
	if [ -n "$SUDO_USER" ]; then
		echo "To run $PKG under sudo, you MUST use 'sudo -H'" 1>&2
	fi
	exit 1
fi

# Determine backend as early as possible, if applicable
case "$0" in
	*byobu-screen) BYOBU_BACKEND="screen" ;;
	*byobu-tmux) BYOBU_BACKEND="tmux" ;;
esac

[ -z "${BYOBU_PREFIX}" ] && export BYOBU_PREFIX="/usr" || export BYOBU_PREFIX
. "${BYOBU_PREFIX}/lib/${PKG}/.common"

# At this point, we're sure BYOBU_BACKEND is properly defined
export BYOBU_BACKEND

# Add a version argument for debugging purposes
if [ "$#" = "1" ]; then
	case "$1" in
		-v|--version)
			echo "$PKG version $VERSION"
			exec $BYOBU_BACKEND $BYOBU_ARG_VERSION
			exit 0
		;;
	esac
fi

# Check if we're being autolaunched, and this user explicitly does not want it.
if [ "$0" = "/etc/profile.d/Z98-$PKG.sh" ] && [ -r "$BYOBU_CONFIG_DIR/disable-autolaunch" ]; then
	exit 0
fi

# Sanitize the environment
byobu-janitor --force

# Set the window title
[ -n "$BYOBU_NO_TITLE" ] || printf "\033]0;${USER}@${HOSTNAME:-$(hostname)} - ${PKG}\007"

# Drop a symlink to the ssh socket in $HOME, since we can ensure that exists
if [ -S "$SSH_AUTH_SOCK" ] && [ ! -h "$SSH_AUTH_SOCK" ]; then
	ln -sf "$SSH_AUTH_SOCK" "$BYOBU_CONFIG_DIR/.ssh-agent"
fi

case $BYOBU_BACKEND in
	tmux)
		# Check if our terminfo supports 256 colors
		if command -v tput >/dev/null; then
			if [ "$(tput colors 2>/dev/null || echo 0)" = "256" ]; then
				export TERM="screen-256color"
			fi
		fi
		PROFILE="-f $BYOBU_PREFIX/share/$PKG/profiles/tmuxrc"
	;;
	*)
		# Allow override of default window list, with BYOBU_WINDOWS environment variable
		CUSTOM_WINDOW_SET=0
		if [ -s "$BYOBU_WINDOWS" ]; then
			CUSTOM_WINDOW_SET=1
		elif [ -s "$BYOBU_CONFIG_DIR/windows.$BYOBU_WINDOWS" ]; then
			CUSTOM_WINDOW_SET=1
			BYOBU_WINDOWS="$BYOBU_CONFIG_DIR/windows.$BYOBU_WINDOWS"
		elif [ "$#" = "0" ]; then
			BYOBU_WINDOWS="$BYOBU_CONFIG_DIR/windows"
		else
			BYOBU_WINDOWS="/dev/null"
		fi
		export BYOBU_WINDOWS
		# Launch shell, unless the user has default windows set to launch
		uncommented_lines < "$BYOBU_WINDOWS" && DEFAULT_WINDOW= || DEFAULT_WINDOW="-t ${SHELL##*/} byobu-shell"
		# Check if our terminfo supports 256 colors
		if command -v tput >/dev/null; then
			if [ "$(tput colors 2>/dev/null || echo 0)" = "256" ]; then
				export SCREEN_TERM="-T screen-256color"
			fi
		fi
		# Some users want to maintain separate configurations
		# if they use both GNU Screen and byobu on the same system
		if [ -r "$HOME/.byoburc" ]; then
			PROFILE="-c $BYOBU_PREFIX/share/$PKG/profiles/byoburc"
		else
			PROFILE="-c $BYOBU_PREFIX/share/$PKG/profiles/screenrc"
		fi
		NAME="-S $PKG"
		# Zero out $NAME if user has specified a session name
		for i in "$@"; do
			case $i in -*r*|-*d*|-*D*|-*S*|-ls|-list) NAME= ;; esac
		done
	;;
esac

# Now let's execute the backend!
if [ "$#" = "0" ]; then
	out=$($BYOBU_BACKEND -wipe 2>/dev/null) || true
	if [ "$CUSTOM_WINDOW_SET" = "1" ]; then
		# Start new custom window set session
		exec $BYOBU_BACKEND $SCREEN_TERM $NAME $PROFILE
	else
		case "$out" in
			*\(*\)*)
				# Select and attach to an existing session
				exec byobu-select-session
			;;
			*)
				# Start new default session
				exec $BYOBU_BACKEND $SCREEN_TERM $NAME $PROFILE $DEFAULT_WINDOW
			;;
		esac
	fi
else
	# Launch with command line args
	exec $BYOBU_BACKEND $SCREEN_TERM $NAME $PROFILE "$@"
fi

# vi: syntax=sh ts=4 noexpandtab
