#!/bin/bash -l

JAM_INSTANCE="$1"

usage()
{
  echo "pass one numerical argument representing the instance of this websocket-gateway"
  exit 0
}

main()
{
  # default config values
  BUILD_NUMBER=`cat /var/lib/websocket-gateway/BUILD_NUMBER`

  CONFIG_FILE="/etc/websocket-gateway/upstart.conf"
  if [ -e "$CONFIG_FILE" ]; then
    . "$CONFIG_FILE"
  fi

  # I don't like doing this, but the next command (bundle exec) retouches/generates
  # the gemfile.  This unfortunately means the next debian update doesn't update this file.
  # Ultimately this means an old Gemfile.lock is left behind for a new package,
  # and bundle won't run because it thinks it has the wrong versions of gems
  rm -f Gemfile.lock

  GATEWAY_NAME="$GATEWAY_NAME" JAM_INSTANCE=$JAM_INSTANCE BUILD_NUMBER=$BUILD_NUMBER JAMENV=production exec bundle exec ruby -Ilib bin/websocket_gateway
}

[ "$#" -ne 1 ] && ( usage && exit 1 ) || main

