#!/bin/bash
# @file beetbox.sh
#
# This script faciliates interactions with beetbox vagrant box, a collabrative product of Melbourne Drupal Community.
#
# @usage
#   beetbox [{beet_command|vagrant_command}]		Detects a ready beetbox context and runs the `vagrant {vagrant_command}` except for init command.
#   list of commands:
#   - init github_user/repo
#   - [any vagrant command] e.g. status, up, halt
#
# @example
#     $ beet init thom8/beetbox
#     $ beet up
#

set -e

script_root=$(pwd)
vagrant_default_repo="beet/box"

function main() {

    beetbox_root=$(detect_beetbox_root)
    script_command=$([ "$1" ] && echo $1 || echo "status")
    if [ "$script_command" = "init" ]; then
        [ "$beetbox_root" ] && echo "Beetbox has been already initiated at $beetbox_root" && exit
        beetbox_root=$script_root
        init_url="git@github.com:"$2".git"
        tmp_dir=`mktemp -d -t beettmp` && cd $tmp_dir
        tmp_repo="$tmp_dir/repo"
        git clone $init_url $tmp_repo >/dev/null
        [ ! -d "$tmp_repo/.beetbox" ] && echo "The init repository does not provide .beetbox directory." && exit 1
        mkdir -p $beetbox_root/.beetbox
        rsync -av $tmp_repo/.beetbox/ $beetbox_root/.beetbox
        VAGRANT_CWD=$beetbox_root vagrant init $vagrant_default_repo
        rm -rf $tmp_dir
    else
        [ ! "$beetbox_root" ] && echo "No beetbox root found on the current path "$script_root && exit
        beetbox_config=$beetbox_root"/.beetbox/config.yml"
        [ ! -e "$beetbox_config" ] && echo "Config file is missing at "$beetbox_config && exit
        echo "Beetbox found at "$beetbox_root
        VAGRANT_CWD=$beetbox_root vagrant $script_command
    fi
}

function detect_beetbox_root() {
    local beetbox_root=""
    while [ ! "$(pwd)" = "/" ]
        do
        $(is_beetbox_root $(pwd)) && beetbox_root=$(pwd) && break || cd ..
    done
    echo $beetbox_root
}
function is_beetbox_root() {
	echo [ -d "$1/.beetbox" ]
}

main ${@}
