#!/bin/bash

set -e

generate() {

	ENTITIES=$(php app/console doctrine:mapping:info | sed -n "s/\[OK\]\s*\(.*\)/\1/p")
	
	echo "easy_admin:
    site_name: TVn7 DevAdmin
    design:
        menu:
            - { label: 'Retour au site', route: '_site_homepage' }
            - { label: 'Retour au TDB', route: '_tdb_homepage' }
            - { label: 'Entités :' }"
	
	curbundle="h"
	for entitie_ns in $ENTITIES; do
		read ns bundle entitie <<< `echo $entitie_ns | awk -F'\' '{ print $1 " " substr($2, 0, length($2) - 6) " " $4}'`
		if [ "$ns" == "TVn7" ]; then
			if [ "$bundle" != "$curbundle" ]; then
				echo "            - label: '$bundle'"
				echo "              children:"
				echo "                  - $entitie"
				curbundle=$bundle
			else
				echo "                  - $entitie"
			fi
		fi
	done
	
	echo "    entities:"
	for entitie_ns in $ENTITIES; do
		if echo $entitie_ns | grep -q "^TVn7"; then
			echo "        - $entitie_ns"
		fi
	done
}
echo "Sauvegarde de admin.yml en admin.yml~"
mv app/config/admin.yml app/config/admin.yml~

generate > app/config/admin.yml
echo "Génération effectuée dans app/config/admin.yml"
