#!/bin/bash

###########
# Lo script utilizza tre possibili environment: Produzione, Test e Locale
# Permette cinque operazioni su database: Drop and rebuild all,  Drop and rebuild all content, Drop and rebuild config, Add missing tables e Skip
###########

source "buildLibs"

OPTION_HELP=false 			# -h

ENVIRONMENT_PRODUCTION=false 		# -p
ENVIRONMENT_TEST=false 			# -t
ENVIRONMENT_LOCAL=false 		# -l

DATABASE_DROP_ALL=false			# -D
DATABASE_DROP_CONTENT=false		# -C
DATABASE_DROP_CONFIG=false		# -c
DATABASE_FIX=false			# -f
DATABASE_SKIP=false			# -s

DEFAULT_CONFIG_FILE=".build.config"

DEFAULT_DATABASE_FILE=".build_db.config"

USE_CONFIG_FILE="$DEFAULT_CONFIG_FILE"
USE_DATABASE_FILE="$DEFAULT_DATABASE_FILE"

while getopts ":hptlDCcfs" optname
do
	case "$optname" in
		"h")
			OPTION_HELP=true
		;;
		"p")
			ENVIRONMENT_PRODUCTION=true
		;;
		"t")
			ENVIRONMENT_TEST=true
		;;
		"l")
			ENVIRONMENT_LOCAL=true
		;;
		"D")
			DATABASE_DROP_ALL=true
		;;
		"C")
			DATABASE_DROP_CONTENT=true
		;;
		"c")
			DATABASE_DROP_CONFIG=true
		;;
		"f")
			DATABASE_FIX=true
		;;
		"s")
			DATABASE_SKIP=true
		;;
		"?")
			echo "Invalid option -$OPTARG"
		;;
		":")
			echo "No argument value for option $OPTARG"
		;;
		*)
		# Should not occur
			echo "Unknown error while processing options"
		;;
	esac
done

if [ "$ENVIRONMENT_PRODUCTION" = false ] && [ "$ENVIRONMENT_TEST" = false ] && [ "$ENVIRONMENT_LOCAL" = false ]; then
	OPTION_HELP=true
fi

if [ $OPTION_HELP = true ]; then
	help
	exit 0
fi

shift $(($OPTIND - 1))

if [ "$1" != "" ]; then
	USE_CONFIG_FILE="$1"
fi

if [ -f "$USE_CONFIG_FILE" ]; then
	source "$USE_CONFIG_FILE"
else
	printf "Impossibile Leggere il file di configurazione '$USE_CONFIG_FILE')\n"
	exit 1
fi

if [ -f "$USE_DATABASE_FILE" ]; then
	source "$USE_DATABASE_FILE"
else
	printf "Impossibile Leggere il file di configurazione del database '$USE_DATABASE_FILE')\n"
	exit 1
fi

if [ ! -w "$APACHE2_DOCUMENTROOT" ] || [ ! -w "$SYSTEM_PRIVATE" ] || [ ! -w "$SYSTEM_STORAGE" ] || [ ! -w "$SYSTEM_LOG_PATH" ]; then
	printf "Permessi insufficienti per eseguire lo script (sudo?)\n"
	if [ ! -w "$APACHE2_DOCUMENTROOT" ]; then
		printf "Impossibile scrivere in '$APACHE2_DOCUMENTROOT'\n"
	fi
	if [ ! -w "$SYSTEM_PRIVATE" ]; then
		printf "Impossibile scrivere in '$SYSTEM_PRIVATE'\n"
	fi
	if [ ! -w "$SYSTEM_STORAGE" ]; then
		printf "Impossibile scrivere in '$SYSTEM_STORAGE'\n"
	fi
	if [ ! -w "$SYSTEM_LOG_PATH" ]; then
		printf "Impossibile scrivere in '$SYSTEM_LOG_PATH'\n"
	fi
	exit 1
fi


printf "Asdynamics Application Build/Deploy Script\n"
if [ -f .logo ]; then
	cat .logo
fi

if [ "$ENVIRONMENT_PRODUCTION" = true ]; then
	deploy_production_env
elif [ "$ENVIRONMENT_TEST" = true ]; then
	deploy_test_env
elif [ "$ENVIRONMENT_LOCAL" = true ]; then
	deploy_local_env
fi

if [ "$DATABASE_DROP_ALL" = true ]; then
	database_rebuild_content
	database_rebuild_config
elif [ "$DATABASE_DROP_CONTENT" = true ]; then
	database_rebuild_content
elif [ "$DATABASE_DROP_CONFIG" = true ]; then
	database_rebuild_config
elif [ "$DATABASE_FIX" = true ]; then
	database_add_missing
elif [ "$DATABASE_SKIP" = false ]; then
	database_add_missing
fi


