#!/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] [-name|-sname <name>]"
    U2="[-nettick <tick>] [-vsn <OTP version>] node trace [time [msgs [proc]]]"
    echo $U1 $U2
    exit
}

VSN=""
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=""
          ;;
      "-name"|"-sname")
          name_option=$1
          name=$2
          shift
          ;;
      "-*")
          echo unrecognized flag $1
          usage
          ;;
      *)
          NODE=$1
          shift
          break   # Let's hold on to the remaining arguments in $@.
          ;;
  esac
  shift
done

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

PATHS="-pa $self/ebin"
if [ -z "$name_option" ]; then
    # 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_"$$
fi
DISTR="-noshell -hidden $name_option $name $cookie $nettick"
START="-run redbug unix"

$echo erl $VSN $DISTR $PATHS $START $NODE "$@"

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