#!/bin/bash

# default to adding plugins
remove=false

# default to https plugins
ssh=true

# Cd to public html foder
cd ${PWD%/public_html*}/public_html

function doWebpack(){
	line="$1:\t{path: path.resolve(plugins,'aquarius-$1')},"
	sed -i "/const aquariusPlugins/a $line" webpack/variables.js
}

# Loop through arguments passed
while test $# -gt 0; do
	case "$1" in
		-h|--help)
			#Print help message
			echo -e "Aquarius Plugin installer"
			echo -e "Adds submodules for commonly used plugins"
			echo -e "You can string multiple plugins together"
			echo -e "e.g. aquarius-plugin blocks clients"
			echo ""
			echo -e "-h, --help \t\t\t\t Show this help text"
			echo -e "-r, --remove \t\t\t\t Remove any plugins after this flag"
			echo -e "-s, --ssh \t\t\t\t Adds the ssh version for the submodule url"
			echo ""
			echo -e "blocks, aquarius-blocks \t\t install aquarius blocks"
			echo -e "clients, aquarius-clients \t\t install aquarius clients"
			echo -e "news, aquarius-news \t\t\t install aquarius news"
			echo -e "people, aquarius-people \t\t install aquarius people"
			echo -e "permissions, aquarius-permissions \t install aquarius permissions into mu-plugins"
			echo -e "slider, aquarius-slider \t\t install aquarius slider"
			echo -e "snippets, aquarius-snippets \t\t install aquarius snippets"
			echo -e "widgets, aquarius-widgets \t\t install aquarius widgets"
			exit 0
			;;
		-r|--remove)
			#start removing
			remove=true
			shift
			;;
		-s|--ssh)
			ssh=true
			shift
			;;
		--no-ssh)
			ssh=false
			shift
			;;
		aquarius-blocks|blocks)
			if [ "$remove" = true ]; then
				npm remove wp-content/plugins/aquarius-blocks
				git-delete-submodule "wp-content/plugins/aquarius-blocks"
			else
				if [ "$ssh" = true ]; then
					git submodule add git@bitbucket.org:fellowshipproductionsltd/aquarius-blocks.git wp-content/plugins/aquarius-blocks
				else
					git submodule add https://bitbucket.org/fellowshipproductionsltd/aquarius-blocks.git wp-content/plugins/aquarius-blocks
				fi
				npm install --save wp-content/plugins/aquarius-blocks
				doWebpack blocks
			fi
			shift
			;;
		aquarius-clients|clients)
			if [ "$remove" = true ]; then
				npm remove wp-content/plugins/aquarius-clients
				git-delete-submodule "wp-content/plugins/aquarius-clients"
			else
				if [ "$ssh" = true ]; then
					git submodule add git@bitbucket.org:fellowshipproductionsltd/aquarius-clients.git wp-content/plugins/aquarius-clients
				else
					git submodule add https://bitbucket.org/fellowshipproductionsltd/aquarius-clients.git wp-content/plugins/aquarius-clients
				fi
				npm install --save wp-content/plugins/aquarius-clients
				doWebpack clients
			fi
			shift
			;;
		aquarius-news|news)
			if [ "$remove" = true ]; then
				npm remove wp-content/plugins/aquarius-news
				git-delete-submodule "wp-content/plugins/aquarius-news"
			else
				if [ "$ssh" = true ]; then
					git submodule add git@bitbucket.org:fellowshipproductionsltd/aquarius-news.git wp-content/plugins/aquarius-news
				else
					git submodule add https://bitbucket.org/fellowshipproductionsltd/aquarius-news.git wp-content/plugins/aquarius-news
				fi
				npm install --save wp-content/plugins/aquarius-news
				doWebpack news
			fi
			shift
			;;
		aquarius-people|people)
			if [ "$remove" = true ]; then
				npm remove wp-content/plugins/aquarius-people
				git-delete-submodule "wp-content/plugins/aquarius-people"
			else
				if [ "$ssh" = true ]; then
					git submodule add git@bitbucket.org:fellowshipproductionsltd/aquarius-people.git wp-content/plugins/aquarius-people
				else
					git submodule add https://bitbucket.org/fellowshipproductionsltd/aquarius-people.git wp-content/plugins/aquarius-people
				fi
				npm install --save wp-content/plugins/aquarius-people
				doWebpack people
			fi
			shift
			;;
		aquarius-permissions|permissions)
			if [ "$remove" = true ]; then
				npm remove wp-content/mu-plugins/aquarius-permissions
				git-delete-submodule "wp-content/mu-plugins/aquarius-permissions"
			else
				if [ "$ssh" = true ]; then
					git submodule add git@bitbucket.org:fellowshipproductionsltd/aquarius-permissions.git wp-content/mu-plugins/aquarius-permissions
				else
					git submodule add https://bitbucket.org/fellowshipproductionsltd/aquarius-permissions.git wp-content/mu-plugins/aquarius-permissions
				fi
				npm install --save wp-content/mu-plugins/aquarius-permissions
			fi
			shift
			;;
		aquarius-projects|projects)
			if [ "$remove" = true ]; then
				npm remove wp-content/plugins/aquarius-slider
				git-delete-submodule "wp-content/plugins/aquarius-projects"
			else
				if [ "$ssh" = true ]; then
					git submodule add git@bitbucket.org:fellowshipproductionsltd/aquarius-projects.git wp-content/plugins/aquarius-projects
				else
					git submodule add https://bitbucket.org/fellowshipproductionsltd/aquarius-projects.git wp-content/plugins/aquarius-projects
				fi
				npm install --save wp-content/plugins/aquarius-projects
				doWebpack projects
			fi
			shift
			;;
		aquarius-slider|slider)
			if [ "$remove" = true ]; then
				npm remove wp-content/plugins/aquarius-slider
				git-delete-submodule "wp-content/plugins/aquarius-slider"
			else
				if [ "$ssh" = true ]; then
					git submodule add git@bitbucket.org:fellowshipproductionsltd/aquarius-slider.git wp-content/plugins/aquarius-slider
				else
					git submodule add https://bitbucket.org/fellowshipproductionsltd/aquarius-slider.git wp-content/plugins/aquarius-slider
				fi
				npm install --save wp-content/plugins/aquarius-slider
			fi
			shift
			;;
		aquarius-snippets|snippets)
			if [ "$remove" = true ]; then
				npm remove wp-content/plugins/aquarius-snippets
				git-delete-submodule "wp-content/plugins/aquarius-snippets"
			else
				if [ "$ssh" = true ]; then
					git submodule add git@bitbucket.org:fellowshipproductionsltd/aquarius-snippets.git wp-content/plugins/aquarius-snippets
				else
					git submodule add https://bitbucket.org/fellowshipproductionsltd/aquarius-snippets.git wp-content/plugins/aquarius-snippets
				fi
				npm install --save wp-content/plugins/aquarius-snippets
				doWebpack snippets
			fi
			shift
			;;
		aquarius-widgets|widgets)
			if [ "$remove" = true ]; then
				npm remove wp-content/plugins/aquarius-widgets
				git-delete-submodule "wp-content/plugins/aquarius-widgets"
			else
				if [ "$ssh" = true ]; then
					git submodule add git@bitbucket.org:fellowshipproductionsltd/aquarius-widgets.git wp-content/plugins/aquarius-widgets
				else
					git submodule add https://bitbucket.org/fellowshipproductionsltd/aquarius-widgets.git wp-content/plugins/aquarius-widgets
				fi
				npm install --save wp-content/plugins/aquarius-widgets
				doWebpack widgets
			fi
			shift
			;;
		*)
			echo "Unknown plugin $1"
			exit 0
			;;
	esac
	
done