#! /bin/bash

set -x

if [[ -z "${MAC_REPO_DIR}" ]]; then
  echo "Missing MAC_REPO_DIR env varible."
  exit 1
fi

if [[ -z "${MAC_REPO_STAGE_BRANCH}" ]]; then
  echo "Missing MAC_REPO_STAGE_BRANCH env varible."
  exit 2
fi

export VERSION=$( azk version | sed -e 's/^azk //; s/^version //; s/,.*//'  )

SHA256=$(shasum -a 256 shasum -a 256 "package/brew/azk_${VERSION}.tar.gz" | awk '{print $1}')

RELEASE_CHANNEL=$( echo "${VERSION}" | sed s/[^\\-]*// | sed s/^\\-// | sed s/\\..*// )
CLASS_NAME="Azk$(tr '[:lower:]' '[:upper:]' <<< ${RELEASE_CHANNEL:0:1})${RELEASE_CHANNEL:1}"
if [[ -z "${RELEASE_CHANNEL}" ]]; then
  CHANNEL_SUFFIX=
  CONFLICTS=
else
  CHANNEL_SUFFIX="-${RELEASE_CHANNEL}"
  CONFLICTS="
  conflicts_with 'azukiapp/azk/azk', :because => 'installation of azk in path'
  "
fi

MAC_REPO_URL="git@github.com:azukiapp/homebrew-azk.git"
MAC_FORMULA_DIR="${MAC_REPO_DIR}/Formula"
MAC_FORMULA_FILE="azk${CHANNEL_SUFFIX}.rb"
MAC_BUCKET_URL="repo-stage.azukiapp.com"

rm -Rf ${MAC_REPO_DIR}
(
  set -e
  git clone ${MAC_REPO_URL} ${MAC_REPO_DIR}
  cd ${MAC_REPO_DIR}
  git checkout ${MAC_REPO_STAGE_BRANCH}

  tee ${MAC_FORMULA_DIR}/${MAC_FORMULA_FILE} <<EOF
require "formula"

class ${CLASS_NAME} < Formula
  homepage "http://azk.io"
  url "http://${MAC_BUCKET_URL}/mac/azk_${VERSION}.tar.gz"
  version "${VERSION}"
  sha256 "${SHA256}"
  head "https://github.com/azukiapp/azk.git"
  ${CONFLICTS}
  depends_on :macos => :mountain_lion
  depends_on :arch => :x86_64

  def install
    items_path = '.'
    if build.head?
      ENV.deparallelize
      ENV['HOMEBREW_TEMP'] = buildpath
      system 'make', '-e', 'package_mac'

      items_path = 'package/brew/*/usr/lib/azk'
      items = %w{ bin lib node_modules shared package.json npm-shrinkwrap.json CHANGELOG.md LICENSE README.md }
    else
      items = ['*']
    end
    items += %w{ .dependencies .nvmrc .package-envs }

    items.each do |item|
      prefix.install Dir["#{items_path}/#{item}"]
    end
  end

  def post_install
    rmtree './package' if build.head?
  end

  test do
    system "azk", "version"
  end
end

EOF

  git add ${MAC_FORMULA_DIR}/${MAC_FORMULA_FILE}
  git commit -m "Bumping version to azk v${VERSION}."
)
