#!/bin/bash
#
#####################################################
#
# !! Functions to use in bash command line tool !!
#
# Docker4Drupal shortcuts commands
#
####################################################


# Operating systems constants
OS_LINUX="linux-gnu"
OS_OSX="darwin"
OS_CYGWIN="cygwin"
OS_WINDOWS="msys"


# import .env file variables
source .env

# Docker up
function docker_up()
{
	docker-compose up -d
}

# Docker down
function docker_down()
{
	docker-compose down
}

# Enter a container
function docker_container_exec()
{
	if [ -n "$2" ]
	then
		docker-compose exec $1 bash -c "$2"
	elif [ -n "$1" ]
	then
		docker-compose exec $1 bash
	fi
}

# Execute commands for composer
function composer(){
	printf "\e[1;33m Launch composer command \e[0m"
	docker_container_exec "php" "composer ${@:1:${#}}"
}

# Execute comands for drush
function drush(){
	printf "\e[1;33m Launch Drush command \e[0m"
	docker_container_exec "php" "drush ${@:1:${#}}"
}

# Execute comands for drupal console
function drupal(){
	printf "\e[1;33m Launch Drupal console command \e[0m"
	docker_container_exec "php" "drupal ${@:1:${#}}"
}


# Enter the PHP container
function docker_cmd_php()
{
	docker_container_exec "php" "$1"
}

# Enter the PHP container
function docker_cmd_node()
{
	docker_container_exec "node" "$1"
}

# Clear Drupal cache
function clear_cache()
{
	printf "\e[1;33m Launch Drupal clear cache process \e[0m"
	docker_container_exec "php" "drupal cr all"
}

# Update Drupal Database
function update_db()
{
	printf "\e[1;33m Launch Drupal Database update process \e[0m"
	docker_container_exec "php" "drupal updb"
}

# Drop database
function drop_db()
{
	printf "\e[1;33m Drop database $DB_NAME \e[0m"
	docker_cmd_php "drupal dbd $DB_NAME"
}

# Install module
function module_install()
{
    docker_cmd_php  "drupal moi $1"
}

# Unnstall module
function module_uninstall()
{
    docker_cmd_php  "drupal mou $1"
}

# Maintenance on
function maintenance_on()
{
  docker_cmd_php "drupal site:maintenance  on"
}

# Maintenance on
function maintenance_off()
{
  docker_cmd_php "drupal site:maintenance  off"
}

# Clear Drupal cache
function drush_dump()
{
	echo -e "\e[1;33m Launch Drush dump process \e[0m"

	if [ -n "$1" ]
	then
		clear_cache
		docker_container_exec "php" "drush sql-dump > $1"
		printf "\e[1;25;42m Database dump generated at $1 \e[0m"
	else
		printf "\e[41m \n[ERROR] Missing the destination filename argument.\n \e[0m"
	fi
}

# CImport config
function drush_cim()
{
	docker_container_exec "php" "drush cim"
}


# Clear Drupal logs
function drush_cex()
{
	docker_container_exec "php" "drush cex"
}


# Shown Drupal logs
function show_drupal_logs()
{
	docker_container_exec "php" "drupal dblp"
}

# Clear Drupal logs
function clear_drupal_logs()
{
	docker_container_exec "php" "drupal dblc"
}

# ***************************************************
# Composer shortcuts
# ***************************************************

# composer install
function composer_install(){
	docker_container_exec "php" "composer install"
}

# composer update
function composer_update(){
	docker_container_exec "php" "composer update"
}

# composer require
function composer_require(){
	docker_container_exec "php" "composer require $1"
}

# composer command
function composer_command(){
	docker_container_exec "php" "composer $1"
}

# open the project URL in the selected browser
function show_site_in_browser(){
	case $1 in
		firefox|brave)
		    echo -e "\e[33m [NOTICE] Congratulations you are using a good browser \e[0m"
		    echo -e "\e[1;25;42m Launch the browser '$1' targeting URL : $PROJECT_BASE_URL \e[0m"
			start $1 $PROJECT_BASE_URL
		    ;;
		chrome)
		    echo -e "\e[33m [NOTICE] Congratulations you are using a good browser, but Google is watching you ! \e[0m"
			echo -e "\e[1;25;42m Launch the browser '$1' targeting URL : $PROJECT_BASE_URL \e[0m"
			start $1 $PROJECT_BASE_URL
			;;
		edge)
		    echo -e "\e[33m [NOTICE] Edge is better that Internet Explorer but still an evil browser.\e[0m"
		    echo -e "\e[1;25;42m Launch the browser '$1' targeting URL : $PROJECT_BASE_URL \e[0m"
		    start microsoft-edge:"http://$PROJECT_BASE_URL"
		    ;;
		ie|internet-explorer|ieexplorer)
			echo -e "\e[33m [NOTICE] Please do not use that evil browser.\e[0m"
			echo -e "\e[1;25;42m Launch the browser '$1' targeting URL : $PROJECT_BASE_URL \e[0m"
			start iexplore.exe "http://$PROJECT_BASE_URL"
			;;
		*)

        echo -e "\e[1;25;42m Launch the default browser targeting URL : $PROJECT_BASE_URL \e[0m"
		start "http://$PROJECT_BASE_URL"
		#echo -e "\e[41m [ERROR] Browser not found.\e[0m"
	esac

}

# Clear vendors
function clear_vendors()
{
	# TODO find a way to set core and contrib paths

	echo -e "\e[1;25;42m Removing vendors files \e[0m"
	docker_cmd_php "rm -R vendor"
	
	echo -e "\e[1;25;42m Removing Drupal core files \e[0m"
	docker_cmd_php "rm -R web/core"
	
	echo -e "\e[1;25;42m Removing Drupal contrib modules \e[0m"
	docker_cmd_php "rm -R web/modules/contrib"
	
	echo -e "\e[1;25;42m Removing Drupal contrib themes \e[0m"
	docker_cmd_php "rm -R web/themes/contrib"
}

# Launch project
function launch_project()
{
	composer_install
	show_site_in_browser
}


# Detect called function
case "$1" in

	up)
		docker_up
		;;
	down)
		docker_down
		;;
	enter)
		docker_container_exec $2
		;;
	composer)
		composer "${@:2:${#}}"
		;;
	drush)
		drush "${@:2:${#}}"
		;;
	drupal)
		drupal "${@:2:${#}}"
		;;
	comp-install)
		composer_install
		;;
	comp-update)
		composer_update
		;;
	comp-require)
		composer_require "$2"
		;;
	comp-cmd)
		composer_command "$2"
		;;
	php)
		docker_cmd_php "$2"
		;;
	node)
		docker_cmd_node
		;;
	cc)
		clear_cache
		;;
	updb)
		update_db
		;;
	dropdb)
		drop_db
		;;
	moi)
	    module_install $2
	    ;;
	mou)
	    module_uninstall $2
	    ;;
	mon)
	    maintenance_on
	    ;;
	 moff)
	    maintenance_off
	    ;;
	dump)
		drush_dump $2
		;;
	cim)
		drush_cim
		;;
	cex)
		drush_cex
		;;
	logs)
		show_drupal_logs
		;;
	logs-clear)
		clear_drupal_logs
		;;
	show)
		show_site_in_browser $2
		;;
	rmv)
		clear_vendors
		;;
	launch)
		launch_project
		;;
	*)


		echo -e "\e[36m"
		echo -e "---------------------------------------------------"
		echo  "   ___  ___  ____  ____  __   ____"
		echo  "  / _ \/ _ \/ __ \/ __ \/ /  / __/"
		echo  " / // / , _/ /_/ / /_/ / /___\ \\"
		echo  "/____/_/|_|\____/\____/____/___/"
		echo -e "\e[0m"
		echo ""
		echo -e "\e[32mdocker-compose shortcuts\e[0m - \e[1;33mversion 1.1\e[0m - 11-02-2020"
		echo -e "\e[36m---------------------------------------------------\e[0m"

		echo ""
		echo -e "\e[35mAvailable commands:\e[0m"

		echo ""
		echo -e " \e[1;33mDocker & docker-compose\e[0m"
		echo -e "  \e[32mup\e[0m 				Start containers"
		echo -e "  \e[32mdown\e[0m				Stop and remove containers"
		echo -e "  \e[32menter\e[0m				Enter the specified container"
		echo -e "  \e[32mphp\e[0m				Enter the PHP container"
		echo ""
		echo -e " \e[1;33mUsual tools aliases shortcuts\e[0m"
		echo -e "  \e[32mcomposer\e[0m			Execute composer commands"
		echo -e "  \e[32mdrush\e[0m				Execute Drush commands"
		echo -e "  \e[32mdrupal\e[0m			Execute Drupal console commands"

		echo ""
		echo -e " \e[1;33mComposer shortcuts\e[0m"
		echo -e "  \e[32mcomp-install\e[0m			Launch composer install in the PHP container"
		echo -e "  \e[32mcomp-update\e[0m			Launch composer update in the PHP container"
		echo -e "  \e[32mcomp-require\e[0m			Launch composer require in PHP container. Argument is a string containing the packages to add"
		echo -e "  \e[32mcomp-cmd\e[0m			Launch composer command in PHP container. Argument is the command string "

		echo ""
		echo -e " \e[1;33mDrupal console shortcuts\e[0m"
		echo -e "  \e[32mcc\e[0m				Drupal clear cache"
		echo -e "  \e[32mupdb\e[0m				Drupal update database"
		echo -e "  \e[32mmoi\e[0m				Drupal install module"
		echo -e "  \e[32mmou\e[0m				Drupal uninstall module"
		echo -e "  \e[32mmon\e[0m				Drupal switch maintenance mode on"
		echo -e "  \e[32mmoff\e[0m				Drupal switch maintenance mode off"
		echo -e "  \e[32mlogs\e[0m				Drupal logs"
		echo -e "  \e[32mlogs-clear\e[0m			Drupal logs clear"
		
		echo ""
		echo -e " \e[1;33mDrush shortcuts\e[0m"
		echo -e "  \e[32mdump\e[0m				Dump database to the specified file name"
		echo -e "  \e[32mcim\e[0m				Drush cim"
		echo -e "  \e[32mcex\e[0m				Drush cex"

		echo ""
		echo -e " \e[1;33mUtilities\e[0m"
		echo -e "  \e[32mshow\e[0m				Open the project's local URL in the specified browser (possible options : brave, firefox, chrome, ie, edge)"
		echo -e "  \e[32mrmv\e[0m				Remove vendors and core files including contrib modules and themes"
		echo -e "  \e[32mlaunch\e[0m			Launch project installing composer and showing URL in browser"
		echo ""
esac
