#!/usr/local/bin/bash

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

self=`dirname $SPATH`
self=`dirname $self`
self=`dirname $self`
self=`(cd $self ; pwd)`

usage(){
    U1="usage: `basename $0` [-setcookie <cookie>] [-nocookie] "
    U2="[-nettick <tick>] [-vsn <OTP version>] node trace [time [msgs [proc]]]"
    echo $U1 $U2
    exit
}

VSN=""
XTRA=""
echo=""
node=""

while [ -n "$1" ]
  do
  case $1 in
      "--help"|"-h"|"-?")
          usage
          ;;
      "-vsn"|"-version")
          VSN="+R "$2
          shift
          ;;
      "-nettick"|"-tick")
          nettick="-kernel net_ticktime $2"
          shift
          ;;
      "-cookie"|"-setcookie")
          cookie="-setcookie $2"
          shift
          ;;
      "-debug")
          echo=echo
          ;;
      "-nocookie")
          cookie=""
          ;;
      "-*")
          echo unrecognized flag $1
          usage
          ;;
      *)
          # The first non-option argument is the node name.
          if [ -z "$node" ]; then
              node=$1
          fi
          XTRA=" $XTRA $1"
          ;;
  esac
  shift
done

if [ -z "$XTRA" ]; then usage; fi

PATHS="-pa $self/ebin"
# If the node name contains a dot after the @ sign, it is a long name,
# and we need to use a long name ourselves.
case $node in
    *@*.*)
        name_option="-name" ;;
    *)
        name_option="-sname" ;;
esac
name="redbug_"$$
DISTR="-noshell -hidden $name_option $name $cookie $nettick"
START="-run redbug unix"

$echo erl $VSN $DISTR $PATHS $START $XTRA

if [ "$?" -eq "1" ]; then usage; fi
