#!/bin/bash
#
#
# 	Script d'initialisation d'un dépot vMap versionné sous git
#
#   Split repo
#   git subtree split -P module/ -b module_split_branch
#
#   Add subtree
#   git subtree add --prefix vmap "git@gitlab.veremes.net:Development/vitis_apps/sources/Vitis.git" master
#
#

echo -n "Existing app path (path/empty)? "
read APP_EXISTING_PATH

# Récupère la liste des dépendances
. ./get_deps.sh

cd ..

# Vitis
echo "---- Link vitis ----"
if ! [ -f "$(pwd)/client" ]; then
	ln -rsf src/vitis/client client
fi
if ! [ -f "$(pwd)/vas" ]; then
	ln -rsf src/vitis/vas vas
fi

# Conf
echo "---- Link conf ----"
if ! [ -f "src/vitis/client/conf" ]; then
	ln -rsf conf src/vitis/client/conf
fi
if ! [ -f "conf/closure" ]; then
	ln -rsf src/closure/conf conf/closure
fi

# Modules
if [[ $depsCount > 0 ]]; then
	for (( i=1; i <= $depsCount; i++ )); do
		if [ ${aDeps[$i, nature]} == "modules" ]; then
			if [ ${aDeps[$i, name]:0:7} == "module_" ]; then
				module=${aDeps[$i, name]:7}
				echo "---- Link module_${module} ----"
				if [ -d "$(pwd)/src/module_${module}/module" ]; then
					if ! [ -f "src/vitis/client/modules/${module}" ]; then
						ln -rsf src/module_${module}/module src/vitis/client/modules/${module}
					fi
				fi
				if [ -d "$(pwd)/src/module_${module}/web_service/ws" ]; then
					if ! [ -f "src/vitis/vas/rest/ws/${module}" ]; then
						ln -rsf src/module_${module}/web_service/ws src/vitis/vas/rest/ws/${module}
					fi
				fi
				if [ -d "$(pwd)/src/module_${module}/web_service/conf" ]; then
					if ! [ -f "src/vitis/vas/rest/conf/${module}" ]; then
						ln -rsf src/module_${module}/web_service/conf src/vitis/vas/rest/conf/${module}
					fi
				fi
				if [ -d "$(pwd)/src/module_${module}/web_service/sql" ]; then
					if ! [ -f "src/vitis/vas/sql/${module}" ]; then
						ln -rsf src/module_${module}/web_service/sql src/vitis/vas/sql/${module}
					fi
				fi
				if [ -d "$(pwd)/src/module_${module}/web_service/class" ]; then
					for class_dir in $( ls "$(pwd)/src/module_${module}/web_service/class"); do
						if [ -d "$(pwd)/src/module_${module}/web_service/class/${class_dir}" ]; then
							echo "---- Link module_${module}/${class_dir} ----"
							if ! [ -f "src/vitis/vas/rest/class/${class_dir}" ]; then
								ln -rsf src/module_${module}/web_service/class/${class_dir} src/vitis/vas/rest/class/${class_dir}
							fi
						fi
					done
				fi
			fi
		fi
	done

	# Utilisation d'une install déjà existante
	if [ -d "${APP_EXISTING_PATH}" ]; then
		echo "---- Link exixting app ${APP_EXISTING_PATH} ----"

		existing_folders_path[0]="vas/server"
		existing_folders_path[1]="vas/log"
		existing_folders_path[2]="vas/public"
		existing_folders_path[3]="vas/shared"
		existing_folders_path[4]="vas/tmp"
		existing_folders_path[5]="vas/upload"
		existing_folders_path[6]="vas/ws_data"

		existing_folders_path[8]="vas/rest/conf"
		file_existing_path[0]="client/conf/properties.json"
		file_existing_path[1]="vas/rest/.htaccess"

		# for i in "${arrayName[@]}" do
		#
		# done
	fi
fi
