#!/bin/bash

set -e

initialCwd=`pwd -P`

apmPath=$0
builtin cd "`dirname "$apmPath"`"
binDir=`basename "$apmPath"`

# Detect CPU arch.
archs=`uname -m`
case "$archs" in
  i?86) archs='ia32' ;;
  x86_64) archs='x64' ;;
  *) archs='arm' ;;
esac

# Detect current OS, see http://stackoverflow.com/q/3466166/682252.
if [ "`uname`" == 'Darwin' ]; then
  nodeBin="node"
elif [ "`expr substr $(uname -s) 1 5`" == 'Linux' ]; then
  nodeBin="node"
elif [ "`expr substr $(uname -s) 1 10`" == 'MINGW32_NT' ]; then
  nodeBin="node.exe"
else
  echo "Your platform (`uname -a`) is not supported."
  exit 1
fi

while [ -L "$binDir" ]
do
  binDir=`readlink "$binDir"`
  builtin cd "`dirname "$binDir"`"
  binDir=`basename "$binDir"`
done

binDir=`pwd -P`
builtin cd "$initialCwd"
"$binDir/$nodeBin" --harmony_collections "$binDir/../lib/cli.js" "$@"
