#!/bin/bash

# This is the script that we install somewhere in your $PATH (as "meteor")
# when you run
#   $ curl https://install.meteor.com/ | sh
# In fact, all that the curl script does is install this script and run it
# once. It's the only file that we install globally on your system; each user of
# Meteor gets their own personal package and tools repository, called the
# warehouse, in ~/.meteor/. This means that a user can share packages among
# multiple apps and automatically update to new releases without having to have
# permissions to write them to anywhere global.
#
# All this script does is exec ~/.meteor/meteor. But what if you don't have it
# yet? In that case, it downloads a "bootstrap tarball", which contains the
# latest version of the Meteor tools, and plops it down at ~/.meteor. In fact,
# once you've run this once, you don't even really need this script: you can put
# ~/.meteor/ into your PATH, or a symlink to ~/.meteor/meteor into some other
# PATH directory. No special permissions needed!
#
# To uninstall Meteor from your system, just delete this shell script, and
# delete your warehouse (~/.meteor/).
#
# We'll keep around a copy of this file at
# ~/.meteor/engines/latest/launch-meteor just in case you ever want to find it
# again. And if you put it somewhere that other users on your system can run it,
# then when they run it they'll get their very own warehouse too!


set -e
set -u
set -o pipefail  # so curl failure triggers the "set -e"

BOOTSTRAP_URL='https://install-bootstrap.meteor.com/'
METEOR_WAREHOUSE_DIR="${METEOR_WAREHOUSE_DIR:-$HOME/.meteor}"

if [ ! -x "$METEOR_WAREHOUSE_DIR/meteor" ]; then
  if [ -e "$METEOR_WAREHOUSE_DIR" ]; then
    echo "'$METEOR_WAREHOUSE_DIR' exists, but '$METEOR_WAREHOUSE_DIR/meteor' is not executable." 1>&2
    echo 1>&2
    echo "Remove it and try again." 1>&2
    exit 1
  fi

  # Bootstrap .meteor from a tarball. First, figure out our architecture.

  UNAME=$(uname)
  if [ "$UNAME" != "Linux" -a "$UNAME" != "Darwin" ] ; then
      echo "Sorry, this OS is not supported yet." 1>&2
      exit 1
  fi

  # If you change this, also change host() in tools/archinfo.js
  if [ "$UNAME" = "Darwin" ] ; then
    if [ "i386" != "$(uname -p)" -o "1" != "$(sysctl -n hw.cpu64bit_capable 2>/dev/null || echo 0)" ] ; then
      # Can't just test uname -m = x86_64, because Snow Leopard can
      # return other values.
      echo "Only 64-bit Intel processors are supported at this time." 1>&2
      exit 1
    fi
    ARCH="x86_64"
  elif [ "$UNAME" = "Linux" ] ; then
    ARCH="$(uname -m)"
    if [ "$ARCH" != "i686" -a "$ARCH" != "x86_64" ] ; then
      echo "Unsupported architecture: $ARCH" 1>&2
      echo "Meteor only supports i686 and x86_64 for now." 1>&2
      exit 1
    fi
  fi
  PLATFORM="${UNAME}_${ARCH}"

  # This returns something like https://warehouse.meteor.com/bootstrap/0.6.1
  ROOT_URL="$(curl -s --fail $BOOTSTRAP_URL)"
  TARBALL_URL="${ROOT_URL}/meteor-bootstrap-${PLATFORM}.tar.gz"

  INSTALL_TMPDIR="$(dirname "$METEOR_WAREHOUSE_DIR")/.meteor-install-tmp"
  rm -rf "$INSTALL_TMPDIR"
  mkdir "$INSTALL_TMPDIR"
  if [ -n "$USER" ]; then
    echo "$USER, this is your first time using Meteor!" 1>&2
  else
    echo "This is your first time using Meteor!" 1>&2
  fi
  echo "Installing a Meteor distribution in your home directory." 1>&2
  curl --progress-bar --fail "$TARBALL_URL" | tar -xzf - -C "$INSTALL_TMPDIR"
  # bomb out if it didn't work, eg no net
  test -x "${INSTALL_TMPDIR}/.meteor/meteor"
  mv "${INSTALL_TMPDIR}/.meteor" "$METEOR_WAREHOUSE_DIR"
  rmdir "${INSTALL_TMPDIR}"
  # just double-checking :)
  test -x "$METEOR_WAREHOUSE_DIR/meteor"
fi

exec "$METEOR_WAREHOUSE_DIR/meteor" "$@"
