#!/bin/bash

SCRIPT=$(basename $0)

usage() {
	echo "Usage: $SCRIPT [--x] [--verbose] [[--su |{user}@]{host}] [--] [remote command...]"
}

termwrapper() {
	PRG=$@
	titletext="$title $titleopt"
	if [ "$V" = 1 ]; then
		echo $PRG
		sleep 4
	fi

	if [ -n "$PRG" ]; then
#		x-terminal-emulator +sb -sl 500 -n "$title" -title "$title" --title "$title" -e $PRG &
		x-terminal-emulator $opts $titleoptname "$titletext" -e $PRG &
#		exec x-terminal-emulator $opts $titleoptname "$title" -e $PRG &
	else
#		exec x-terminal-emulator +sb -sl 500 -n "$title" -title "$title" --title "$title" &
		x-terminal-emulator $opts $titleoptname "$titletext" &
#		exec x-terminal-emulator $opts $titleoptname "$title" &
	fi
}

# Initial setup
SU=''
X=''
V=''
SUHACK=''
HOST=''
USER=''
ssh_opts="-t"

args=''
while [ $# -gt 0 ]; do
	case $1 in
		-su|su)		SU=1;;
				# Become root indirectly through a regular user (same as local user if -u not used)
		-suu|suu)	SU=1; SUU=1;;
				# Remote user allowed to switch to root
		--user|-u)	USER=$2; shift;;
		--x|-x|x)	X=1; ssh_opts="$ssh_opts -X";;
		--ssh2|-2)	ssh_opts="$ssh_opts -2";;
		--verbose|-v)	V=1; ssh_opts="$ssh_opts -v";;
		--port|-p)	ssh_opts="$ssh_opts -p $2"; shift;;
		--host|-h)	HOST=$1;;
		-)		HOST="localhost";;
		--)		shift; args="$args$@ "; break;;
		-*)		usage; exit 1;;
		root@*)		HOST="$1"; SU=1; SUHACK=1;;
		*)		args="$args$1 ";;
	esac
	shift
done
set -- $args

# Make sure we have a hostname
if [ -z "$HOST" ]; then
	if [ $# -gt 0 ]; then
		HOST=$1
		shift
	else
		HOST="localhost"
	fi
fi

# Split program name from its options
if [ $# -gt 0 ]; then
	prg_base=$1
	shift
	prg_opts=$@
fi

if [ "$X" = 1 -a "$SU" = 1 -a "$SUHACK" != 1 ]; then
	args="root@$args"
	SUHACK=1
fi

titleopt="[$HOST]"
if [ $HOST != "localhost" ]; then
	ssh_opts="-C $ssh_opts"
fi

case `readlink /etc/alternatives/x-terminal-emulator | xargs basename` in
#    rxvt*)			opts="+sb -sl 500 -n \"$titletext\""; titleoptname="-title";;
    rxvt*)			opts="+sb -sl 500 -ls"; titleoptname="-title";;
    urxvt*)			opts="+sb -sl 500 -ls"; titleoptname="-title"; term="rxvt";;
    *xterm)			opts="+sb -sl 500 +wc"; titleoptname="-title";;
    gnome-terminal)		opts=""; titleoptname="--title";;
    gnome-terminal.wrapper)	opts=""; titleoptname="-title";;
    konsole*)			opts="--notoolbar"; titleoptname="-T";;
    *)				opts=""; titleoptname="-T";;
esac

# Pause for a moment if verbose
if [ "$V" = 1 ]; then
	prg_opts="$prg_opts; sleep 4"
fi

if [ -z "$prg_base" ]; then
	title="$SCRIPT"
	prg_base='/bin/bash'
else
	title="$prg_base"
	prg_opts_local="$prg_opts"
	prg_opts_remote="$prg_opts"
fi

# Special cases for specific programs
case $prg_base in
		# Force Midnight Commander colors and support slow connections
	mc)	prg_opts_local="-c $prg_opts_local"; prg_opts_remote="-s -c $prg_opts_remote";;
esac

prg_local="$prg_base $prg_opts_local"
if [ -n "$term" ]; then
	prg_remote="bash --login -i -c \"test -e /etc/debian_version && export TERM=rxvt || export TERM=xterm; $prg_base $prg_opts_remote\""
else
	prg_remote="bash --login -i -c \"test -e /etc/debian_version || export TERM=xterm; $prg_base $prg_opts_remote\""
fi
prg_remote_suu="echo -n 'Changing to root...: '; su $su_opts -c \"cd; $prg_base $prg_opts_remote\""

if [ "$SU" != 1 ]; then
	if [ $HOST = "localhost" ]; then
		termwrapper $prg_local
	else
		titleopt="[$HOST]"
		termwrapper ssh $ssh_opts $HOST $prg_remote
	fi
else
	if [ "$SUHACK" = 1 ]; then
		titleopt="[$HOST]"
#		termwrapper ssh $ssh_opts $HOST su $su_opts -c \"$prg_remote\"
		termwrapper ssh $ssh_opts $HOST $prg_remote
	else
		titleopt="[$HOST root]"
		if [ "$SUU" = 1 ]; then
			if [ -z "$USER" ]; then
				termwrapper ssh $ssh_opts $HOST $prg_remote_suu
			else
				termwrapper ssh $ssh_opts $USER@$HOST $prg_remote_suu
			fi
		else
			if [ $HOST = "localhost" ]; then
				termwrapper ssh $ssh_opts root@$HOST bash --login -i -c \"$prg_local\"
			else
				termwrapper ssh $ssh_opts root@$HOST $prg_remote
			fi
		fi
	fi
fi