if [ -z "$JAVA_HOME" ]
then
  echo "The JAVA_HOME environment variable was not detected, set environment variable first."
  exit 1
fi
if [ $# -ne 0 -a $# -ne 4 ]; then
  echo "Invalid parameters specified."
  exit 1
fi

if [ $# -eq 0 ]; then
  $JAVA_HOME/bin/java -ea -Xms40m -Xmx512m -cp loaddriver.jar -Djava.security.policy=loaddriver.policy -Dloaddriver.properties=loaddriver.properties Start
  exit 0
fi

if [ $# -eq 4 ]; then
  if [ "$1" = "systemadapterip" ]; then
    IP=$2
    if [ "$3" != "systemadapterport" ]; then
      echo "System adapter port parameter is missing."
      exit 1
    else
      PORT=$4
    fi
  else
    if [ "$1" = "systemadapterport" ]; then
      PORT=$2
      if [ "$3" != "systemadapterip" ]; then
        echo "System adapter ip parameter is missing."
        exit 1
      else
        IP=$4
      fi
    else
     echo "Invalid parameters specified."
     exit 1
    fi
  fi
  
  if [ -z "${IP+x}" -o -z "${PORT+x}" ]; then
	echo "System Adapter ip or port parameter not available."
    exit 1
  fi
  
  $JAVA_HOME/bin/java -ea -Xms40m -Xmx512m -cp loaddriver.jar -Djava.security.policy=loaddriver.policy -Dloaddriver.properties=loaddriver.properties Start --register --systemadapterip $IP --systemadapterport $PORT
  exit 0
fi
