#!/usr/bin/env bash

if [ -z "$APP" ]
then
  APP="$(basename $ORIGIN_DIR)"
fi

# Your app must run under this user. All files will be created as this user.
# The context in which deliver gets run must be able to login as this user,
# without asking for a password or a passphrase. The price of automation...
#
if [ -z "$APP_USER" ]
then
  APP_USER="$APP"
fi

# If your app runs under 'foobar' system user, the code will be delivered to
# '~foobar/app' by default. You can deliver multiple apps under the same user
# by overwriting this value on a per app basis, but I would discourage this practice.
# Each app should run under its own system user.
#
if [ -z "$DELIVER_TO" ]
then
  DELIVER_TO="~$APP_USER/app"
fi

if [ -z "$GIT_PUSH" ]
then
  GIT_PUSH="${GIT_PUSH:=-f}"
fi

# Configure which refspec to push remotely
#
if [ -z "$REFSPEC" ]
then
  REFSPEC="${BRANCH:=master}"
fi

# Configure which revision to push remotely
#
if [ -z "$REVISION" ]
then
  REVISION="$(git rev-parse $BRANCH)"
fi

# Ruby is the default deliver strategy.
# node.js might be hip, but Ruby is beautiful.
#
if [ -z "$STRATEGY" ]
then
  STRATEGY="ruby"
fi

# Deliver used to be single-server, accounting for that here
#
SERVERS=$(__remote_friendly "$SERVER $SERVERS $HOSTS")
HOSTS="$SERVERS"

if [ -z "$LOG_FILE" ]
then
  LOG_FILE="/tmp/deliver"
fi

# Controls which directory the generated strategy puts the final files into
#
if [ -z "$GENERATED_DIR" ]
then
  GENERATED_DIR="generated"
fi

# The local git branch into which all generated content will be committed
#
if [ -z "$GENERATED_BRANCH" ]
then
  GENERATED_BRANCH="generated"
fi

# command to compile the source the remote build host
# rebar | mix
if [ -z "$BUILD_CMD" ]; then
  [[ -f "./mix.exs" ]] && BUILD_CMD=${BUILD_CMD:=mix} || BUILD_CMD=${BUILD_CMD:=rebar}
fi

# command to generate the release on the remote build host
# mix | rebar | relx
if [ -z "$RELEASE_CMD" ]; then
  if [[ -f "./relx.config" ]]; then
    RELEASE_CMD=${RELEASE_CMD:=relx}
  elif [[ -f "./mix.exs" ]]; then
    RELEASE_CMD=${RELEASE_CMD:=mix}
  else
    RELEASE_CMD=${RELEASE_CMD:=rebar}
  fi
fi

if [ "$RELEASE_CMD" = "mix" ]; then
    [[ -f "./rel/config.exs" ]] && USING_DISTILLERY="true"
fi

