#!/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@vm09.veremes.net:Development/vitis_apps/sources/Vitis.git" master
#
#


APP_NAME="vmap"
APP_MODULES=( vmap vm4ms anc cadastre cadastreV2 )


# Vitis
echo "---- Link Vitis ----"
ln -rsf deps/vitis/client client
ln -rsf deps/vitis/vas vas
echo "client" >> .gitignore
echo "vas" >> .gitignore

# Conf
echo "---- Link ${APP_NAME}/conf ----"
ln -rsf deps/app_${APP_NAME}/conf deps/vitis/client/conf
echo "deps/vitis/client/conf" >> .gitignore

# Modules
for module in "${APP_MODULES[@]}"; do
	echo "---- Link ${module} ----"
	if [ -d "$(pwd)/deps/module_${module}/module" ]; then
		ln -rsf deps/module_${module}/module deps/vitis/client/modules/${module}
		echo "deps/vitis/client/modules/${module}" >> .gitignore
	fi
	if [ -d "$(pwd)/deps/module_${module}/web_service/ws" ]; then
		ln -rsf deps/module_${module}/web_service/ws deps/vitis/vas/rest/ws/${module}
		echo "deps/vitis/vas/rest/ws/${module}" >> .gitignore
	fi
	if [ -d "$(pwd)/deps/module_${module}/web_service/conf" ]; then
		ln -rsf deps/module_${module}/web_service/conf deps/vitis/vas/rest/conf/${module}
		echo "deps/vitis/vas/rest/conf/${module}" >> .gitignore
	fi
	if [ -d "$(pwd)/deps/module_${module}/web_service/sql" ]; then
		ln -rsf deps/module_${module}/web_service/conf deps/vitis/vas/sql/${module}
		echo "deps/vitis/vas/sql/${module}" >> .gitignore
	fi
	if [ -d "$(pwd)/deps/module_${module}/web_service/class" ]; then
		for class_dir in $( ls "$(pwd)/deps/module_${module}/web_service/class"); do
			if [ -d "$(pwd)/deps/module_${module}/web_service/class/${class_dir}" ]; then
				echo "---- Link ${module}/${class_dir} ----"
				ln -rsf deps/module_${module}/web_service/class/${class_dir} deps/vitis/vas/rest/class/${class_dir}
				echo "deps/vitis/vas/rest/class/${class_dir}" >> .gitignore
			fi
		done
	fi
done
