#!/bin/sh -e
#
#    select-screen-profile
#    Copyright (C) 2008 Canonical Ltd.
#
#    Authors: Dustin Kirkland <kirkland@canonical.com>
#             Nick Barcet <nick.barcet@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/>.


# If you change any strings, please generate localization information with:
#	./debian/rules get-po


TEXTDOMAIN="screen-profiles"

usage () {
    cat <<EOT
usage: select-screen-profile [(-l|--list)][(-h|--help)][(-s|--set) PROFILE]
    -l,--list           list available profiles
    -s,--set PROFILE    set profile
    -h,--help           this help

    Without any parameters, runs interactively.
EOT
}

# Initialize variables
BASE_DIR="/usr/share/screen-profiles"
PROFILE_DIR="$BASE_DIR/profiles"
profile=""
selected=-1

assert_symlink() {
	if [ -e "$1" ]; then
		if [ ! -L "$1" ]; then
			echo `gettext 'Error:'` $1 `gettext ' file exists, but is not a symlink'`
			exit 1
		fi
	fi
}

# If these files exist, they must be a symlink
assert_symlink "$HOME/.screen-profiles/profile"

listprofiles() {
	# Display list of profiles, one per line
	# Start with basic profiles
	basename $(ls $PROFILE_DIR/plain 2>/dev/null) 2>/dev/null
	basename $(ls $PROFILE_DIR/*-light 2>/dev/null) 2>/dev/null
	basename $(ls $PROFILE_DIR/*-dark 2>/dev/null) 2>/dev/null
	basename $(ls $PROFILE_DIR/*-black 2>/dev/null) 2>/dev/null
	# Now, list advanced profiles
	for x in $(ls $PROFILE_DIR/*-*_* 2>/dev/null); do
		x=$(basename "$x")
		if [ $x = "common" -o $x = "misc" ]; then
			# Skip the common profile, no value there
			continue
		fi
		if [ $x = "ubuntu" -a -r "$PROFILE_DIR/ubuntu-light" ]; then
			continue
		fi
		echo "$x"
	done
}

prompt() {
	# Prompt the user to choose among the available profiles
	echo
	echo `gettext "Select a screen profile: "`
	i=0
	profiles=$(listprofiles)
	for x in $profiles; do
		i=$(expr $i + 1)
		desc="          "
		if [ "$x" = "ubuntu" ]; then
			desc="-light\t"
		elif [ "$x" = "plain" ]; then
			simple=$i
		fi
		[ $i -lt 10 ] && i=" $i"
		echo " $i. $x$desc"
	done
	echo
	selected=x
	count=1
	while /bin/true; do
		if [ $count -gt 5 ]; then
			echo `gettext "ERROR: Invalid selection"`
			exit 1
		fi
		count=`expr $count + 1`
		if [ -z "$selected" -a ! -z "$simple" ]; then
			selected="$simple"
		elif ! test $selected -gt 0 2>/dev/null; then
			read -p "`gettext 'Choose'` 1-$i [$simple]: " -r selected
		elif ! test $selected -le $i 2>/dev/null; then
			read -p "`gettext 'Choose'` 1-$i [$simple]: " -r selected
		else
			break
		fi
	done
	SELECTED="$selected"
}

setprofile() {
	# Apply a profile by name or index
	if [ -n "$1" ]; then
		selected="$1"
	else
		selected="$SELECTED"
	fi
	i=0
	found=0
	profiles=$(listprofiles)
	for x in $profiles; do
		i=`expr $i + 1`
		if [ "$i" = "$selected" -o "$x" = "$selected" ]; then
			rm -f "$HOME/.screen-profiles/profile"
			ln -s "$PROFILE_DIR/$x" "$HOME/.screen-profiles/profile"
			found=1
			echo
			if [ "$TERM" = "screen" ]; then
				echo `gettext 'If you are using the default set of keybindings, press\n<F5><enter> to activate these changes.\n\nOtherwise, exit this screen session and start a new one.'`
			else
				echo `gettext 'Run "screen" to activate'`
			fi
			echo
			break
		fi
	done
	if [ $found -eq 0 ]; then
		echo "Invalid profile"
	fi
}

if [ $# -eq 0 ]; then
	prompt
	setprofile
else
	TEMP=`getopt -o lhs: --long list,help,set: -- "$@"`
	eval set -- "$TEMP"
	while true; do
		case "$1" in
			-s|--set)
				setprofile "$2"
				shift 2
				break
			;;
			-l|--list)
				listprofiles
				shift
				break
			;;
			*)
				usage
				exit 1
			;;
			--)
				shift
				break
			;;
		esac
	done
fi

exit 0
