#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"

tar_build() {
  declare desc="builds apps from tarball via command line"
  local APP="$1"
  shift 1

  # clean up after ourselves
  local TAR_BUILD_TMP_WORK_DIR=$(mktemp -d "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")
  trap "rm -rf '$TAR_BUILD_TMP_WORK_DIR' >/dev/null" RETURN INT TERM EXIT

  # extract tar file
  chmod 755 "$TAR_BUILD_TMP_WORK_DIR"
  pushd "$TAR_BUILD_TMP_WORK_DIR" >/dev/null

  # Detect a common prefix that all files in the tar have, and strip off each directory found in it
  local COMMON_PREFIX=$(tar -tf "$DOKKU_ROOT/$APP/src.tar" | sed -e 'N;s/^\(.*\).*\n\1.*$/\1\n\1/;D')
  local BOGUS_PARTS=$(echo "$COMMON_PREFIX " | awk 'BEGIN{FS="/"} {print NF-1}')

  dokku_log_info1_quiet "Striping $BOGUS_PARTS worth of directories from tarball"

  tar -x -C "$TAR_BUILD_TMP_WORK_DIR" -f "$DOKKU_ROOT/$APP/src.tar" --strip-components="$BOGUS_PARTS"
  chmod -R u+r "$TAR_BUILD_TMP_WORK_DIR"

  local DOKKU_APP_DISABLE_ANSI_PREFIX_REMOVAL DOKKU_GLOBAL_DISABLE_ANSI_PREFIX_REMOVAL DOKKU_DISABLE_ANSI_PREFIX_REMOVAL
  DOKKU_APP_DISABLE_ANSI_PREFIX_REMOVAL=$(config_get "$APP" DOKKU_DISABLE_ANSI_PREFIX_REMOVAL || true)
  DOKKU_GLOBAL_DISABLE_ANSI_PREFIX_REMOVAL=$(config_get --global DOKKU_DISABLE_ANSI_PREFIX_REMOVAL || true)
  DOKKU_DISABLE_ANSI_PREFIX_REMOVAL=${DOKKU_APP_DISABLE_ANSI_PREFIX_REMOVAL:="$DOKKU_GLOBAL_DISABLE_ANSI_PREFIX_REMOVAL"}

  if [[ "$DOKKU_DISABLE_ANSI_PREFIX_REMOVAL" == "true" ]]; then
    tar_trigger_build "$APP" "$TAR_BUILD_TMP_WORK_DIR"
  else
    tar_trigger_build "$APP" "$TAR_BUILD_TMP_WORK_DIR" | sed -u "s/^/"$'\e[1G'"/"
  fi
}

tar_trigger_build() {
  declare desc="triggers the actual build process for a given app within a directory at a particular revision"
  declare APP="$1" TMP_WORK_DIR="$2" REV="$3"
  local BUILDER

  plugn trigger post-extract "$APP" "$TMP_WORK_DIR" "$REV"

  BUILDER="$(plugn trigger builder-detect "$APP" "$TMP_WORK_DIR" | head -n1 || true)"
  [[ -z "$BUILDER" ]] && BUILDER="herokuish"

  plugn trigger pre-receive-app "$APP" "$BUILDER" "$TMP_WORK_DIR" "$REV"
  dokku_receive "$APP" "$BUILDER" "$TMP_WORK_DIR"
}

cmd-tar-in() {
  declare desc="deploys app from tarball on STDIN via command line"
  declare cmd="tar:in"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1"

  verify_app_name "$APP"
  tee "$DOKKU_ROOT/$APP/src.tar" | wc -c
  tar_receive_app "$APP"
}

cmd-tar-from() {
  declare desc="deploys app from tarball at URL via command line"
  declare cmd="tar:from"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" URL="$2"

  verify_app_name "$APP"
  shift 2
  curl -# --insecure -L "$URL" | cmd-tar-in "tar:in" "$APP" "$@"
}

tar_receive_app() {
  declare desc="tar receive-app plugin trigger"
  declare APP="$1"

  # Don't trigger tar build if there is no tarball.
  if [[ ! -f "$DOKKU_ROOT/$APP/src.tar" ]]; then
    true
  else
    acquire_app_deploy_lock "$APP"
    tar_build "$@"
    release_app_deploy_lock "$APP"
  fi
}
