#!/usr/bin/env bash

# Ensure this file is executable via `chmod a+x deken`, then place it
# somewhere on your $PATH, like ~/bin. The rest of Deken will self-install
# on first run into the ~/.deken/ directory.

# Much of this code is pilfered from Clojure's Leiningen tool

export DEKEN_VERSION="0.1"

if [ $(id -u) -eq 0 ] && [ "$DEKEN_ROOT" = "" ]; then
    echo "WARNING: You're currently running as root; probably by accident."
    echo "Press control-C to abort or Enter to continue as root."
    echo "Set DEKEN_ROOT to disable this warning."
    read _
fi

if [ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ]; then
    delimiter=";"
else
    delimiter=":"
fi

if [ "$OSTYPE" = "cygwin" ]; then
  cygwin=true
else
  cygwin=false
fi

# This needs to be defined before we call HTTP_CLIENT below
if [ "$HTTP_CLIENT" = "" ]; then
    if which curl >/dev/null; then
        if [ "$https_proxy" != "" ]; then
            CURL_PROXY="-x $https_proxy"
        fi
        HTTP_CLIENT="curl $CURL_PROXY -f -L -o"
    else
        HTTP_CLIENT="wget -O"
    fi
fi

export DEKEN_HOME="${DEKEN_HOME:-"$HOME/.deken"}"
DEKEN_BASE_URL="https://raw.githubusercontent.com/pure-data/deken/master"

if $cygwin; then
    export DEKEN_HOME=$(cygpath -w "$DEKEN_HOME")
fi

error() {
    echo "$@" 1>&2
}

bail_install() {
    error "Self-installation of Deken failed."
    error "Please paste any errors in the bug tracker at https://github.com/pure-data/deken/issues"
    # remove all trace of our attempts to install.
    rm -rf $DEKEN_HOME;
    # bail from this script.
    exit 1;
}
bail_install_msg() {
    error "$@"
    bail_install
}

install_virtualenv() {
	echo "Downloading & installing Virtualenv."
	rm -rf $DEKEN_HOME/virtualenv-source
	mkdir -p $DEKEN_HOME/virtualenv-source && \
	$HTTP_CLIENT $DEKEN_HOME/virtualenv.tar.gz https://pypi.python.org/packages/source/v/virtualenv/virtualenv-12.1.1.tar.gz && \
	tar -zxvf $DEKEN_HOME/virtualenv.tar.gz -C $DEKEN_HOME/virtualenv-source/ && \
	mv $DEKEN_HOME/virtualenv-source/virtualenv-*/* $DEKEN_HOME/virtualenv-source
	[ -d "$DEKEN_HOME/virtualenv-source" ] && (\
		cd $DEKEN_HOME/virtualenv-source && \
		/usr/bin/env python setup.py build ) \
		|| bail_install;
}

install_deken() {
    which python >/dev/null || \
        bail_install_msg "Oops, no Python found! You need Python to run Deken."
    which make >/dev/null || \
        bail_install_msg "Oops, no Make found! You need Make to run Deken."
    error "This is your first time running deken on this machine."
    error "I'm going to install myself and my dependencies into ~/.deken now."
    error "Feel free to ctrl-C now if you don't want to do this."
    sleep 3;
    [ -d "$DEKEN_HOME" ] || mkdir -p $DEKEN_HOME;
    [ -e "$DEKEN_HOME/requirements.txt" ] || (\
        ( echo "Fetching Python requirements file: $DEKEN_BASE_URL/requirements.txt" && \
        $HTTP_CLIENT $DEKEN_HOME/requirements.txt $DEKEN_BASE_URL/requirements.txt ) || bail_install)
    [ -e "$DEKEN_HOME/requirements.txt" ] || bail_install
    [ -e "$DEKEN_HOME/deken.hy" ] || (\
        ( echo "Fetching main hylang file: $DEKEN_BASE_URL/deken.hy" && \
        $HTTP_CLIENT $DEKEN_HOME/deken.hy $DEKEN_BASE_URL/deken.hy ) || bail_install)
    [ -e "$DEKEN_HOME/deken.hy" ] || bail_install
    [ -x "$DEKEN_HOME/virtualenv-source/virtualenv.py" ] || install_virtualenv;
    [ -d "$DEKEN_HOME/virtualenv" ] || (\
        echo "Setting up the virtual environment." && \
        $DEKEN_HOME/virtualenv-source/virtualenv.py "$DEKEN_HOME/virtualenv" || exit 1)
    [ -x "$DEKEN_HOME/virtualenv/bin/hy" ] || (\
        echo "Installing deken library dependencies." && \
        $DEKEN_HOME/virtualenv/bin/pip install -r $DEKEN_HOME/requirements.txt || exit 1)
}

upgrade_deken() {
    # first upgrade this script itself
    echo "Upgrading $0."
    $HTTP_CLIENT $0 $DEKEN_BASE_URL/deken
    # next upgrade our dependencies
    for f in requirements.txt deken.hy;
    do
        echo "Fetching $f file: $DEKEN_BASE_URL/$f";
        $HTTP_CLIENT $DEKEN_HOME/.upgrade-$f $DEKEN_BASE_URL/$f || ( error "Error upgrading $f"; exit 1; );
        mv $DEKEN_HOME/.upgrade-$f $DEKEN_HOME/$f;
    done
    # finally update the python dependencies
    $DEKEN_HOME/virtualenv/bin/pip install -r $DEKEN_HOME/requirements.txt
    echo "Successfully upgraded."
}

# make sure we are deployed
[ -d "$DEKEN_HOME" ] || install_deken

# last check to make sure we can bootstrap
[ -d "$DEKEN_HOME" ] || bail_install;

# catch the special "upgrade" command
[ "$1" = "upgrade" ] && \
    # run he upgrade command instead
    upgrade_deken || \
    # run the real deken command with args passed through
    $DEKEN_HOME/virtualenv/bin/hy $DEKEN_HOME/deken.hy $@
