#!/bin/sh -e
#
#    screen-profile-export
#    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/>.

PROG="screen-profiles"
SHARE="/usr/share/$PROG"
TMPDIR=`mktemp -d screen-profiles.XXXXXXXX` || error "Could not create a temporary directory"
# Grab list of available colors
colors=`find $SHARE/profiles/ -type f | sed "s/^.*\///" | sed "s/^.*-//" | sort -u | egrep -v "common|plain"`

# Make sure we clean up $TMPDIR if we exit for any reason
trap "rm -rf "$TMPDIR" 2>/dev/null || true" EXIT HUP INT QUIT TERM

usage() {
	echo
	echo "Usage:"
	echo " $0 [-c COLOR] -f TARGET.tar.gz"
	echo
	echo "TARGET.tar.gz is required"
	echo
	echo "COLOR is obtained interactively if unspecified; or one of:"
	for c in $colors; do
		echo " * $c"
	done
	echo
	exit 1
}

error() {
	echo "ERROR: $1" 1>&2
	exit 1
}

choose() {
	i=0
	x=
	selected=
	for x in $@; do
		i=$(expr $i + 1)
		[ $i -lt 10 ] && i=" $i"
		echo " $i. $x" 1>&2
	done
	echo
	count=1
	while /bin/true; do
		if [ $count -gt 5 ]; then
			echo `gettext "ERROR: Invalid selection"`
			exit 1
		fi
		count=`expr $count + 1`
		if ! test $selected -gt 0 2>/dev/null; then
			read -p "`gettext 'Choose'` [1 -$i]: " -r selected
		elif ! test $selected -le $i 2>/dev/null; then
			read -p "`gettext 'Choose'` [1 -$i]: " -r selected
		else
			break
		fi
	done
	i=0
	for x in $@; do
		i=$(expr $i + 1)
		if [ "$i" = "$selected" ]; then
			SELECTED="$x"
			return 0
		fi
	done
	exit 1
}

status_config() {
	# Generate the status configuration
	# Disable the menu, since screen-profiles configurator is not available
	# Enable user@host in its place
	for i in $(ls /var/lib/screen-profiles/); do
		case "$i" in
			cpu-count|cpu-freq|hostname|load-average|logo|mem-available|mem-used|reboot-required|release|updates-available|whoami)
				echo "$i=1"
			;;
			*)
				echo "$i=0"
			;;
		esac
	done
}

hr() {
	echo "###############################################################################"
}

header() {
	hr
	echo "# This GNU Screen profile was generated by the screen-profile-export"
	echo "# program, which is part of the screen-profiles package, and contains a"
	echo "# subset of the functionality available from the full package."
	echo "#"
	echo "# For more information, source code, questions, and bugs, see:"
	echo "#  * https://launchpad.net/screen-profiles"
	echo "#"
	echo "#    Copyright (C) 2008 Canonical Ltd."
	echo "#"
	echo "#    Author: Dustin Kirkland <kirkland@canonical.com>"
	echo "#"
	echo "#    This program is free software: you can redistribute it and/or modify"
	echo "#    it under the terms of the GNU General Public License as published by"
	echo "#    the Free Software Foundation, version 3 of the License."
	echo "#"
	echo "#    This program is distributed in the hope that it will be useful,"
	echo "#    but WITHOUT ANY WARRANTY; without even the implied warranty of"
	echo "#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the"
	echo "#    GNU General Public License for more details."
	echo "#"
	echo "#    You should have received a copy of the GNU General Public License"
	echo "#    along with this program.  If not, see <http://www.gnu.org/licenses/>."
	hr
}

sources() {
	# insert the common profile, replace the status exe path
	echo
	cat $SHARE/profiles/common | sed "s:$PROG-status:\$HOME/.$PROG/$PROG-status:"
	hr
	echo
	cat $SHARE/keybindings/common
	hr
	echo
	hr
	cat "$SOURCE"
	hr
}


# Handle command line parameters
color=
file=
while [ $# -gt 1 ]; do
	case "$1" in
		-c)
			color="$2"
			shift 2
		;;
		-f)
			file="$2"
			shift 2
		;;
		*)
			usage
		;;
	esac
done

[ $# -eq 1 ] && file="$1"

[ -z "$file" ] && usage
echo "$file" | grep -qs "\.tar\.gz$" || error "Target file must be a '.tar.gz' archive"
if [ -e "$file" ]; then
	echo `gettext "File exists"` " [$file]"
	read -p "`gettext 'Remove file? [y/N] '`" -r remove
	if [ "$remove" = "Y" -o "$remove" = "y" ]; then
		rm -f "$file"
	else
		exit 1
	fi
fi

# Obtain selections
count=1
distro="ubuntu"
while /bin/true; do
	if [ $count -gt 5 ]; then
		echo `gettext "ERROR: Invalid selection"`
		exit 1
	fi
	SOURCE="$SHARE"/profiles/"$distro"-"$color"
	if [ -f "$SOURCE" ]; then
		break
	else
		# Try in the misc directory too
		SOURCE="$SHARE"/profiles/misc/"$distro"-"$color"
		if [ -f "$SOURCE" ]; then
			break
		fi
	fi
	echo
	echo `gettext "Select a color: "`
	choose $colors
	color=$SELECTED
	count=`expr $count + 1`
done

# Create workspace
PROFILE="$TMPDIR/.screenrc"
STATUS="$TMPDIR/.$PROG/status"
mkdir -p "$TMPDIR/.$PROG/bin"

# Copy status scripts
cp -a /var/lib/$PROG/* "$TMPDIR/.$PROG/bin"
cp -a /usr/bin/$PROG-status "$TMPDIR/.$PROG"

# Generate the monolithic profile
header > "$PROFILE"
sources >> "$PROFILE"
status_config > "$STATUS"

# Some gardening
# Drop additional "source" calls
sed -i "s:^source .*::" "$PROFILE"
# Use .screenrc instead
sed -i "s:.$PROG/profile\":\.screenrc\":" "$PROFILE"
# Drop the F9->Menu key
sed -i "s:^bindkey -k k9 screen -t help:#bindkey -k k9 screen -t help:" "$PROFILE"

# tar up the results
tar -zcf "$file" -C "$TMPDIR" . || error "Could not create archive"

echo
echo "Success!"
echo
echo "  "`gettext "Profile"` ": [$distro-$color]"
echo "  "`gettext "Archive"` ": [$file]"
echo
echo `gettext "Extract the archive in your home directory on the target system."`
echo
