#!/bin/bash

usage(){
    U1="usage: $0 [-setcookie <cookie>] [-nocookie] "
    U2="[-nettick <tick>] [-dbg] [-vsn <OTP version>]"
    echo $U1 $U2
    exit
}

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

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

EPATH=`dirname $EPATH`

n=3
SPATH=`(cd $EPATH ; pwd)`
while [ 0 -lt "$n" ]; do SPATH=`dirname $SPATH`; let n=n-1; done

PATHS=""
for p in eper gtknode; do
    BPATH=`find $SPATH -name $p.beam`
    PATHS=$PATHS" -pa "`dirname $BPATH`
done

detach="-detached"
VSN=""
START="-s sherk ni -s erlang halt"

while [ -n "$1" ]
  do
  case $1 in
      "-help"|"-h"|"-?")
          usage
          ;;
      "-dbg")
          START="-s sherk go"
          detach=""
          ;;
      "-vsn"|"-version")
          VSN="+R "$2
          shift
          ;;
      "-nettick"|"-tick")
          nettick="-kernel net_ticktime $2"
          shift
          ;;
      "-cookie"|"-setcookie")
          cookie="-setcookie $2"
          shift
          ;;
      "-nocookie")
          cookie=""
          ;;
      *)
          usage
          ;;
  esac
  shift
done

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

erl $VSN $FLAGS $DISTR $PATHS $START
