#!/bin/bash

usage(){
    cat <<- end
usage: `basename $0` [-setcookie <cookie>] [-nocookie] [-nettick <tick>]
       [-win] [-dbg] [-vsn <OTP version>] [-proxy proxynode] nodename
end
    exit
}

if [ ${0:0:1} == "/" ];then
    SPATH=$0
elif [ -x $PWD/$0 ];then
    SPATH=$PWD/$0
else
    SPATH=`which $0`
fi

if [ -L $SPATH ];then
    SPATH=`readlink $SPATH`
fi

n=3
while [ 0 -lt "$n" ]; do SPATH=`dirname $SPATH`; let n=n-1; done
ERL=erl
START="-s dtop start"

while [ -n "$1" ]
  do
  case $1 in
      "-help"|"-h"|"-?")
          usage
          ;;
      "-dbg")
          ;;
      "-win")
          WIN="xterm -sb -sl 9999 -bg gold -fg black -e"
          ;;
      "-vsn"|"-version")
          shift
          VSN="+R "$1
          ;;
      "-nettick"|"-tick")
          shift
          nettick="-kernel net_ticktime $1"
          ;;
      "-cookie"|"-setcookie")
          shift
          cookie="-setcookie $1"
          ;;
      "-nocookie")
          cookie=""
          ;;
      "-proxy")
          shift
          proxy="$1"
          ;;
      *)
          if [ $# -eq 1 ]; then
              TARG=$1
          else
              usage
          fi
          ;;
  esac
  shift
done

if [ -z "$TARG" ]; then
    usage;
else
    START="$START $TARG $proxy"
fi

name="dtop_"$$
FLAGS="-boot start_sasl -sasl errlog_type error +A16"
DISTR="-hidden -sname $name $cookie $nettick"

$WIN erl $VSN $FLAGS $DISTR -pa $SPATH/ebin $START
