#!/bin/sh
#
#    screen wrapper script
#    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/>.

OLDPKG="screen-profiles"
PKG="byobu"
SCREEN_REAL="/usr/bin/screen"

# Upgrade old config dir to the new name
[ -d "$HOME/.$OLDKPG" -a ! -e "$HOME/.$PKG" ] && mv -f "$HOME/.$OLDPKG" "$HOME/.$PKG"

# Create the .$PKG directory, if it doesn't already exist
[ -d "$HOME/.$PKG" ] || mkdir -p "$HOME/.$PKG"

# Use $SCREENRC if non-empty, and readable; otherwise, use the one in $HOME
[ -n "$SCREENRC" -a -r "$SCREENRC" ] || SCREENRC="$HOME/.screenrc"

# If $SCREENRC exists but $HOME/.$PKG/profile does not,
# this shows that the user has an existing custom screen configuration,
# and thus we will not force $PKG on them.
if [ -r "$SCREENRC" -a ! -e "$HOME/.$PKG/profile" -a ! -h "$HOME/.$PKG/profile" ]; then
	exec $SCREEN_REAL -c "$SCREENRC" "$@"
	exit $?
fi

# If the user is running byobu with some arguments, but has not selected
# their profile yet, don't bother them with profile selection at this time
if [ $# -gt 0 -a ! -h "$HOME/.$PKG/profile" ]; then
	exec $SCREEN_REAL "$@"
	exit $?
fi

DEFAULT_PROFILE="light"

# Ensure that the user has selected a screen profile
profile="$HOME/.$PKG/profile"
[ -h "$profile" ] || /usr/bin/select-screen-profile -s "$DEFAULT_PROFILE"

# Previously, profiles were prepended with ubuntu-.
# This is no longer the case, for cross-distro compatibility.
# Try to fix broken symlinks for upgrading users, or prompt to reselect.
if [ -h "$profile" -a ! -r "$profile" ]; then
	if stat "$profile" | head -n1 | grep -qs ".*->.*ubuntu\-.*"; then
		newsrc=$(stat "$profile" | head -n1 | sed "s/.*\`//" | sed "s/'.*//" | sed "s/ubuntu-//")
		if [ -r "$newsrc" ]; then
			ln -sf "$newsrc" "$profile"
		else
			/usr/bin/select-screen-profile -s "$DEFAULT_PROFILE"
		fi
	elif stat "$profile" | head -n1 | grep -qs ".*->.*plain'$"; then
		ln -sf "/usr/share/$PKG/profiles/NONE" "$profile"
	else
		/usr/bin/select-screen-profile -s "$DEFAULT_PROFILE"
	fi
fi

if [ ! -r "$profile" ]; then
	echo
	echo "Your selected profile is not accessible."
	echo
	echo "Either select a different profile:"
	echo " $ select-screen-profile"
	echo
	echo "Or install the extras package:"
	echo " $ sudo apt-get install $PKG-extras"
	echo
	exit 1
fi

# Ensure that their keybindings are seeded
[ -s "$HOME/.$PKG/keybindings" ] || echo "source /usr/share/$PKG/keybindings/common" > "$HOME/.$PKG/keybindings"
sed -i "s/$OLDPKG/$PKG/g" "$HOME/.$PKG/keybindings"

# Ensure that their default windows are seeded
[ -r "$HOME/.$PKG/windows" ] || touch "$HOME/.$PKG/windows"

# Ensure that the user's $SCREENRC at least exists
[ -r "$SCREENRC" ] || touch "$SCREENRC"

# Now let's execute screen!
if [ "$#" = "0" ]; then
	[ -n "$SHELL" -a -x "$SHELL" ] || SHELL="/bin/sh"
	exec $SCREEN_REAL -c "/usr/share/$PKG/profiles/byoburc" $SHELL /usr/bin/motd+shell
else
	exec $SCREEN_REAL -c "$HOME/.$PKG/profile" "$@"
fi
