#!/bin/sh

#origionally from /mi/kchen modified a little
# If no home machine is specified, use linerva.mit.edu.

if [ "x$ZEPHYR_HOME" = "x" ]; then
    ZEPHYR_HOME="linerva.mit.edu"
fi

# Figure out which kind of terminal to use.
if [ "x$ZEPHYR_SCREEN_TERMINAL" = "x" ]; then
    ZEPHYR_SCREEN_TERMINAL="gnome-terminal --title=barnowl"
fi

# Some terminals require quotes around the -e argument, and some do
# not.
#
# gnome-terminal requires quotes.
#
# xterm on Sun requires not having quotes.
# aterm requires not having quotes.
# rxvt requires not having quotes.
# Eterm requires not having quotes.
#
# xterm on Linux can use either.

QUOTE=''
echo "$ZEPHYR_SCREEN_TERMINAL" | grep "^gnome-terminal" > /dev/null
if [ $? -eq 0 ]; then
    QUOTE='"'
fi

# Don't do anything if we're just ssh'ing.

if [ "x$XSESSION" != "x" ]; then

  # Start zwgc with no subs.
  # This sends a login notification when appropriate, so even though
  # we're using screen and owl elsewhere, people can still find us.

  # This also ensures zephyrs sent by programs upon startup will
  # still send the zephyr, and not hang when started with &

  zwgc -f /dev/null -subfile /dev/null
  zctl cancel

  if [ "$HOST" = "$ZEPHYR_HOME" ]; then

      # We're on our home machine!
      # See if we already have a screen named owl.

      screenlist=`screen -list | grep owl`

      if [ "x$screenlist" = "x" ]; then
	  # No screen named owl exists, so create a new one with
	  # owl-screen.
	  # owl-screen will take care of kerberos, AFS, and naming the screen

	  $ZEPHYR_SCREEN_TERMINAL -e owl-screen > /dev/null &
      else
	  # One already exists, so reattach it.
	  eval $ZEPHYR_SCREEN_TERMINAL -e ${QUOTE}screen -dr owl${QUOTE} > /dev/null &
      fi
	  
  else

      # We're not on our home machine...
      # ssh to it and reattach the screen named owl.
      eval $ZEPHYR_SCREEN_TERMINAL -e ${QUOTE}ssh -t $ZEPHYR_HOME screen -dr owl${QUOTE} > /dev/null &

  fi

fi
