#!/bin/sh

# Colors
GREEN='\033[0;32m'
LIGHTGREEN='\033[1;32m'
NC='\033[1;37m'
LIGHTBLUE='\033[01;34m'
NC='\033[0m' # No Color

# In directory
PACKAGE_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
suggest_current(){
    case "$1" in
        "editor")
            current=$EDITOR
        ;;
        *)
            current=$(which $1)
        ;;
    esac
    if [ -n $current ]; then
        echo $current | pbcopy
        echo "${NC}Press ⌘V to insert current (${current})"
    fi
}

main() {

    echo "${LIGHTBLUE}================================================"
    echo " Mac CLI Installer"
    echo "================================================${NC}\n"

    git clone https://github.com/guarinogabriel/mac-cli.git --depth 1 && cd mac-cli

    printf "${LIGHTGREEN}"
    read -r -p "Would you like Mac CLI to show you the commands that are run on each mac command? (Yes / No)" response
    case $response in
        [yY][eE][sS]|[yY])
            LC_CTYPE=C sed -i'' -e 's#echocommand="false"#echocommand="true"#g' "$PACKAGE_DIRECTORY/mac-cli/mac"
            ;;
        *)
            ;;
    esac
    printf "${NC}"

    sudo cp "$PACKAGE_DIRECTORY/mac-cli/mac" /usr/local/bin/mac
    sudo cp -r "$PACKAGE_DIRECTORY/mac-cli/mac-cli/" /usr/local/bin/mac-cli
    sudo chmod -R +x /usr/local/bin/mac-cli/*

    if ! [ -x "$(command -v brew)" ]; then
        echo "Installing Homebrew..."
        echo "${GREEN}/usr/bin/ruby -e '\$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)'\n${NC}"
        /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
    fi

    if ! [ -x "$(command -v git)" ]; then
        echo "Installing Git..."
        echo "${GREEN}brew install git\n${NC}"
        brew install git
	  fi

    if ! [ -x "$(command -v pv)" ]; then
        echo "Installing pv (Pipe Viewer)..."
        echo "${GREEN}brew install pv\n${NC}"
        brew install pv
	  fi

    if ! [ -x "$(command -v wget)" ]; then
        echo "Installing wget..."
        printf "${GREEN}brew install wget\n${NC}"
        brew install wget
	  fi

    if ! brew list | grep -q bash-completion ; then

            echo "Installing bash-completion\n"
            echo "${GREEN}brew install bash-completion\n\n${NC}"
            brew install bash-completion
            echo 'if [ -f $(brew --prefix)/etc/bash_completion ]; then source $(brew --prefix)/etc/bash_completion; fi' >> ~/.bashrc
            echo "Setting up Mac-CLI bash-completion\n"

        if [ -d /usr/local/etc/bash_completion.d/ ]; then
            cp /usr/local/bin/mac-cli/completion/bash_completion /usr/local/etc/bash_completion.d/mac
        fi

    else

        echo "Setting up Mac-CLI bash-completion\n"

        if [ -d $(brew --prefix)/etc/bash_completion.d/ ]; then
            cp /usr/local/bin/mac-cli/completion/bash_completion $(brew --prefix)/etc/bash_completion.d/mac
            echo 'if [ -f $(brew --prefix)/etc/bash_completion ]; then source $(brew --prefix)/etc/bash_completion; fi' >> ~/.bashrc
        fi

    fi

    #
    # Check if fish shell is installed and prompt user to copy fish_completion
    #
    if $(cat /etc/shells | grep -q fish) && [ -d ~/.config/fish ];
    then
	    printf "${LIGHTGREEN}"
	    read -r -p "Would you like to install fish shell suggestions/tab completion? (Yes / No) " response
	    case $response in
		    [yY][eE][sS]|[yY])
			    echo "Setting up Mac-CLI Fish completion"
			    if [ -d ~/.config/fish/completions ];then
				    cp /usr/local/bin/mac-cli/completion/fish_completion ~/.config/fish/completions/mac.fish
			    else
				    mkdir ~/.config/fish/completions && cp /usr/local/bin/mac-cli/completion/fish_completion ~/.config/fish/completions/mac.fish
			    fi
			    ;;
		    *)
			    ;;
	    esac
	    printf "${NC}"
    fi
}

main
