#!/bin/bash
#
# develop_brew_cask
#
# Called via symlink as:
#  production_brew_cask
#

called_as="$(basename "$0")"

###
### settings
###

set -e               # exit on any uncaught error
set +o histexpand    # don't expand history expressions
shopt -s nocasematch # case-insensitive regular expressions

###
### configurable global variables
###

taps_subdir="Library/Taps"
cask_tap_subdir="caskroom/homebrew-cask"
dev_links=("cmd" "lib" "Casks")

###
### 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
}

cd_to_project_root () {
    local script_dir git_root
    script_dir="$(/usr/bin/dirname "$0")"
    cd "$script_dir"
    git_root="$(git rev-parse --show-toplevel)"
    if [[ -z "$git_root" ]]; then
        die "ERROR: Could not find git project root"
    fi
    cd "$git_root"
}

cd_to_tap_dir () {
    local taps_dir="$1"
    local tap_dir="$2"
    if [[ ! -d "$tap_dir" ]]; then
        die "ERROR: Could not find tap dir under $taps_dir/"
    fi
    cd "$tap_dir"
}

not_inside_homebrew () {
    local tap_dir="$1"
    local git_root="$2"
    if [[ "$(/usr/bin/stat -L -f '%i' -- "$tap_dir")" -eq "$(/usr/bin/stat -L -f '%i' -- "$git_root")" ]]; then
        die "\nERROR: Run this script in your private repo, not inside Homebrew.\n"
    fi
}

remove_dev_links () {
    for link_name in "${dev_links[@]}"; do
        remove_dev_link "$link_name"
    done
    printf "brew-cask is now in production mode\n"
    printf "It is safe to run 'brew update' if you are in production mode for all Caskroom repos.\n"
}

create_dev_links () {
    local git_root="$1"
    for link_name in "${dev_links[@]}"; do
        create_dev_link "$git_root" "$link_name"
    done
    printf "brew-cask is now in development mode\n"
    printf "Note: it is not safe to run 'brew update' while in development mode\n"
}

remove_dev_link () {
    local link_name="$1"
    /bin/rm -- "$link_name"
    /bin/mv -- "production_$link_name" "$link_name"
}

create_dev_link () {
    local git_root="$1"
    local link_name="$2"
    /bin/mv -- "$link_name" "production_$link_name"
    /bin/ln -s -- "$git_root/$link_name" .
}

###
### main
###

_develop_brew_cask_develop_action () {
    die "brew-cask is already set up for development"
}

_develop_brew_cask_production_action () {
    create_dev_links "$git_root"
}

_production_brew_cask_develop_action () {
    remove_dev_links
}

_production_brew_cask_production_action () {
    die "brew-cask is already set up for production"
}

_main () {
    local git_root brew_repository taps_dir tap_dir

    # initialization
    cd_to_project_root
    git_root="$(/bin/pwd)"
    brew_repository="$(brew --repository)"
    taps_dir="$brew_repository/$taps_subdir"
    tap_dir="$taps_dir/$cask_tap_subdir"

    # sanity check
    not_inside_homebrew "$tap_dir" "$git_root"

    # action
    cd_to_tap_dir "$taps_dir" "$tap_dir"
    if [[ -e "production_lib" ]]; then
        eval "_${called_as}_develop_action"
    else
        eval "_${called_as}_production_action"
    fi

}

_develop_brew_cask_usage () {

    printf "develop_brew_cask

Symlink private repo directories into Homebrew's Cellar, so
that the 'brew cask' command will use code and Casks from
the current development branch in your private repo.

Saves the production Homebrew directories under new names.

You can reverse this operation with 'production_brew_cask'.

Note: it is not safe to run 'brew update' while development
mode is in effect.

"

}

_production_brew_cask_usage () {

    printf "production_brew_cask

Undo all symlinks created by 'develop_brew_cask' so that the
'brew cask' command will use only released code and Casks
within Homebrew.

After running this command it is safe to run 'brew update',
unless you are using similar scripts to create symlinks into
other Caskroom development repos.

"

}

# ensure we're called by a valid name
case "${called_as}" in
    develop_brew_cask) ;;
    production_brew_cask) ;;
    *)
        die "ERROR: name ${called_as} not recognized"
        ;;
esac


# process args
if [[ $1 =~ ^-+h(elp)?$ ]]; then
    eval "_${called_as}_usage"
    exit
fi

# dispatch main
_main "${@}"

#
