#!/bin/sh

# CDE DT seems to require LOGNAME to be set up
LOGNAME=${LOGNAME:-$USER}
export LOGNAME

if [ "x$1" = "x-h" ]; then
  echo "Usage: $0 [ -tt <ttsession-args> ]" 1>&2
  exit
fi

# do we have the CDE DT directory in our path?
case ":${PATH}:" in
  *:/usr/dt/bin:*)
    ;;
  *)
    PATH=${PATH}:/usr/dt/bin
    export PATH
    ;;
esac

# we need a display for the rest
if [ -n "$DISPLAY" ]; then
  # CDE DT has trouble creating this directory if you don't use dtlogin
  d=/var/dt/tmp/${USER}-`echo $DISPLAY|tr ':' '-'`
  if [ ! -d $d ]; then
    echo creating $d
    mkdir $d
    chmod ugo+rwx $d
  fi

  # make sure we are at home
  cd
  unset cdDepth

  # now start up ToolTalk and Desktop sessions
  if [ "x$1" = "x-tt" ]; then
    shift
    ttsession "$@"
  else
    ttsession -s -d "$DISPLAY"
  fi

  sleep 1
  dtsession &
fi
