#!/bin/bash
#
# brew-cask
#
# bash shim to invoke brew-cask-cmd.rb
#

###
### settings
###

set -e
set -o pipefail
set +o histexpand
set -o nounset
shopt -s nocasematch
shopt -s nullglob
shopt -s dotglob

###
### functions
###

warn () {
    local message="$@"
    message="${message//\\t/$'\011'}"
    message="${message//\\n/$'\012'}"
    message="${message%"${message##*[![:space:]]}"}"
    printf "%s\n" "$message" 1>&2
}

die () {
    warn "$@"
    exit 1
}

resolve_dir () {
    local resolved="$(for dir; do cd "$dir"; done && /bin/pwd -P)"
    if [[ -d "$resolved" ]]; then
        printf "%s" "$resolved"
    else
        die "cannot resolve: '$@'"
    fi
}

ensure_dir () {
    local dir="$1"
    local message
    shift

    if [ "$#" -gt 0 ]; then
        message="$@"
    else
        message="brew-cask: no such directory: '$dir'"
    fi
    if ! [[ -d "$dir" ]]; then
        die "$message"
    fi
}

ensure_file () {
    local file="$1"
    local message
    shift

    if [ "$#" -gt 0 ]; then
        message="$@"
    else
        message="brew-cask: no such file: '$file'"
    fi
    if ! [[ -f "$file" ]]; then
        die "$message"
    fi
}

ensure_executable () {
    local executable="$1"
    local message
    shift

    if [ "$#" -gt 0 ]; then
        message="$@"
    else
        message="brew-cask: no such executable: '$executable'"
    fi
    if ! [[ -f "$executable" ]] || ! [[ -x "$executable" ]]; then
        die "$message"
    fi
}

find_ruby_2_plus () {
    declare -a rubies
    local favorite_ruby="/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby"
    local version_str

    if [[ -x "$favorite_ruby" ]]; then
        printf "%s" "$favorite_ruby"
    else
        IFS=$'\n' rubies=( $(/usr/bin/type -aP ruby)   \
                           "/usr/local/bin/ruby"       \
                           "$(brew --prefix 2>/dev/null)/bin/ruby" )
        for ruby in "${rubies[@]}"; do
            version_str="$("$ruby" --version 2>/dev/null)"
            if [[ "$version_str" =~ ^ruby.2 ]]; then
                printf "%s" "$ruby"
                break
            fi
        done
    fi
}

###
### main
###

_brew_cask () {
    local script_dir
    local symlink_target
    local symlink_target_dir
    local brewcask_lib_dir
    local brewcask_command
    local interpreter
    declare -a ruby_opts

    ruby_opts=( "-W0" "-EUTF-8:UTF-8" )

    script_dir="$(resolve_dir "${0%/*}")"
    ensure_dir "$script_dir" \
               "brew-cask: could not resolve script directory"

    # return "." in case we are not a link
    symlink_target="$(/usr/bin/readlink "$0" || echo ".")"

    # redefine script_dir because we are likely to be a relative link
    symlink_target_dir="$(/usr/bin/dirname "$symlink_target")"
    script_dir="$(resolve_dir "$script_dir" "$symlink_target_dir")"
    ensure_dir "$script_dir" \
               "brew-cask: could not resolve script directory"

    # The Homebrew install process replaces the lib element below with rubylib
    brewcask_lib_dir="$(resolve_dir "$script_dir"/../lib)"
    ensure_dir "$brewcask_lib_dir" \
               "brew-cask: could not resolve homebrew-cask library directory"

    brewcask_command="$brewcask_lib_dir/brew-cask-cmd.rb"
    ensure_file "$brewcask_command" \
                "brew-cask: could not find brew-cask-cmd.rb in '$brewcask_lib_dir'"

    interpreter="$(find_ruby_2_plus)"
    ensure_executable "$interpreter" \
                      "brew-cask: could not find Ruby 2.0 or greater.  Try 'brew install ruby'."

    exec "$interpreter" "${ruby_opts[@]}" "$brewcask_command" "${@}"
}

###
### initialization
###

unset GEM_HOME
unset GEM_PATH

###
### dispatch
###

_brew_cask "${@:-}"

#
