#!/bin/sh
# Linuxbrew wrapper for Debian Package
#   1. run linuxbrew if detected the linuxbrew instance under
#      ${LINUXBREW_PREFIX}/.linuxbrew
#   2. run the upstream install script if user invoked brew
#      and the wrapper found no linuxbrew instance.
#
# This script is designed to be able to work outside of the
# Debian package which contains it.
#
# Copyright: 2015 Lumin <cdluminate@gmail.com>
# License:   BSD-2-Clause
set -e

# --- [ Check Environment ] ---
# provided for user to override default directory of linuxbrew.
# here I use LINUXBREW_PREFIX as the major ENV,
# and HOMEBREW_PREFIX is for somewhat compatibility.
if [ -z ${LINUXBREW_PREFIX} ] && [ -z ${HOMEBREW_PREFIX}]; then
  # when both ENV are not set, use default.
  export LINUXBREW_PREFIX="${HOME}/.linuxbrew"
  export HOMEBREW_PREFIX=${LINUXBREW_PREFIX}
elif [ -z ${HOMEBREW_PREFIX} ];then
  # LINUXBREW_PREFIX is set.
  export HOMEBREW_PREFIX=${LINUXBREW_PREFIX}
elif [ -z ${LINUXBREW_PREFIX} ];then
  # HOMEBREW_PREFIX is set.
  export LINUXBREW_PREFIX=${HOMEBREW_PREFIX}
else #[ ! -z ${LINUXBREW_PREFIX} ] && [ ! -z ${HOMEBREW_PREFIX}]
  # both ENV are set (even if they are conflicting to each other),
  # then we use LINUXBREW_PREFIX.  And export nothing
  true
fi

# --- [ Check ruby ] ---
#  Linuxbrew will not work without RUBY
if [ ! -x $(which ruby) ]; then
  printf "E: ruby interpreter not available, abort.\n"
  false
fi

# --- [ Set var ] ---
export LIB_HOMEBREW="/usr/lib/linuxbrew-wrapper"  # a fixed position
export BREW_EXEC="${LINUXBREW_PREFIX}/bin/brew"   # real brew executable
export BREW_INSTALL="${LIB_HOMEBREW}/install"     # local upstream install script
# ORIGIN is original upstream install script
export ORIGIN_INSTALL="https://raw.githubusercontent.com/Homebrew/linuxbrew/go/install"

# --- [ root ? ] ---
if [ `whoami` = "root" ]; then
  printf "Warn: take your own RISK utilizing linuxbrew with ROOT permission.\n"
fi

# --- [ misc function ] ---

check_arch () {
# Users under ARCHs other than amd64 may need to take care of themselves,
# as linuxbrew upstream only declared official support on amd64.
  if [ "x86_64" != $(arch) ]; then
    printf "
Warn: not running on amd64 architecture,
      which means you might experience build failures.\n\n"
  fi
}

first_time_hint () {
  printf "
========================================================================
If you are the first time running brew/linuxbrew, you may need to :

 * Modify PATH,MANPATH,INFOPATH environment in order to utilize software
   installed by linuxbrew.
   (See example file /usr/share/doc/linuxbrew-wrapper/examples/profile)
========================================================================
"
}

# --- [ do the job ] ---
# 1.Start: Let's see if there is brew instance available.
if [ -x ${BREW_EXEC} ]; then
  exec ${BREW_EXEC} $@
elif [ -r ${BREW_EXEC} ]; then
  printf "E: Please check the mode of ${BREW_EXEC}"
  false
elif [ -d ${LINUXBREW_PREFIX} ]; then
  printf "E: Directory ${LINUXBREW_PREFIX} exists,\n"
  printf "   but found no executable ${BREW}\n"
  printf " * Please check that directory.\n"
  false
fi
# 1.End: Well... we need to get linuxbrew from internet

# 2.Start: Let's install linuxbrew from internet
if [ -r ${BREW_INSTALL} ]; then
    check_arch
    first_time_hint
    ruby ${BREW_INSTALL}
    exec ${BREW_EXEC} $@
else
  check_arch
  printf "I: Trying to download linuxbrew install script then run it ...\n"
  ruby -e "$(curl -fsSL ${ORIGIN_INSTALL})"
  exec ${BREW_EXEC} $@
fi
# 2.End: things should be properly done.

#EOF
