#! /bin/sh

# gtmess - MSN Messenger client
# (c) 2002-2010 by George M. Tzoumas
#
# This script opens a terminal window and runs gtmess in it.
# It can be used as a desktop shortcut.
#
# This script 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.
#
# The source code of this script is ABSOLUTELY FREE
# You can do whatever you want with it ;-)

# Make sure the programs are in your PATH!

APP="gtmess"
APPARGS="-Opopup=1"

which $APP 2>/dev/null || exit 1

NAMETITLE="-name $APP -title $APP"
URXVT_FLAGS="-tn rxvt -fg gray -bg black -geometry 112x40 $NAMETITLE"
XTERM_FLAGS="-fg gray -bg black -geometry 112x40 $NAMETITLE"
RXVT_FLAGS="-fg gray -bg black -geometry 112x40 $NAMETITLE"
WTERM_FLAGS="-tr $RXVT_FLAGS"
#KONSOLE_FLAGS="--vt_sz 94x34 --noscrollbar --notabbar --name $APP"
KONSOLE_FLAGS="--noscrollbar --name $APP"
GTERM_FLAGS="--geometry 94x34 -t $APP"

PREFPROG="$1"

if test -z "$PREFPROG"; then
    if test "$KDE_FULL_SESSION" == "true"; then
        PREFPROG="konsole"
    elif test -n "$GNOME_DESKTOP_SESSION_ID"; then
        PREFPROG="gnome-terminal"
    fi
fi

for f in $PREFPROG urxvt wterm rxvt konsole gnome-terminal xterm; do
    if which $f >/dev/null; then
      TERMPROG="$f"
      break
    fi
done

test -z "$TERMPROG" && exit 2

ps -U $UID | grep -q "gtmess-notify" || gtmess-notify &
#sleep 2

case "$TERMPROG" in

    urxvt)
        # try to use the daemon first
        urxvtc $URXVT_FLAGS -e $APP
        test $? -eq 2 && urxvt $URXVT_FLAGS -e "$APP $APPARGS"
        ;;
        

    rxvt)
        rxvt $RXVT_FLAGS -e "$APP $APPARGS"
        ;;

    wterm)
        wterm $WTERM_FLAGS -e "$APP $APPARGS"
        ;;
        
    konsole)
        konsole $KONSOLE_FLAGS -e "$APP $APPARGS"
        ;;
    
    gnome-terminal)
        gnome-terminal $GTERM_FLAGS -e "$APP $APPARGS"
        ;;

    *)
        xterm $XTERM_FLAGS -e "$APP $APPARGS"
        ;;
esac

killall gtmess-notify > /dev/null 2>&1
