#!/usr/bin/env bash

# 2017-01-01 HB9ocq - removed WA (escape commas)


# automated build only by DF8OE
if [ "$1" == "auto" ] && [ ! -f ../DF8OE ]; then
  exit 0
fi

if [ "$1" == "test" ]; then
  mkdir ../handbook-test 2>/dev/null
	cp -v uhsdr-logo.png support/ui/menu > /dev/zero
	(cd support/ui/menu  \
		&&  python2 ./ui_menu_structure_c2py.py | python -  \
		&&  python2 ./ui_menu_structure_graph.py > ../../../../handbook-test/ui_menu_structure_graph.gv  \
		&&  dot -Tsvg -o../../../../handbook-test/ui_menu_structure_graph.svg  ../../../../handbook-test/ui_menu_structure_graph.gv  \
		&&  dot -Tpng -o../../../../handbook-test/ui_menu_structure_graph.png  ../../../../handbook-test/ui_menu_structure_graph.gv \
	)
	(cd support/ui/menu  \
		&&  python2 ./ui_menu_structure_mdtable.py > ../../../../handbook-test/ui_menu_structure_mdtable.md \
	)
	echo "menu-handbook-test build completed."
	exit 0
fi

if [ "$(cat support/ui/menu/menu-handbook-build.timestamp 2>/dev/null)" == "$(stat -c%y drivers/ui/menu/ui_menu_structure.c)" ]; then
	echo "menu-handbook already up2date"
else
	# extract UI Menu Descriptor data from source code and generate graph + table for handbook
	echo "building menu-handbook..."
	cp -v uhsdr-logo.png support/ui/menu > /dev/zero
	(cd support/ui/menu  \
		&&  python2 ./ui_menu_structure_c2py.py | python -  \
		&&  python2 ./ui_menu_structure_graph.py > ui_menu_structure_graph.gv  \
		&&  dot -Tsvg -oui_menu_structure_graph.svg  ui_menu_structure_graph.gv  \
		&&  dot -Tpng -oui_menu_structure_graph.png  ui_menu_structure_graph.gv \
	)
	(cd support/ui/menu  \
		&&  python2 ./ui_menu_structure_mdtable.py > ui_menu_structure_mdtable.md \
	)
	(echo $(stat -c%y drivers/ui/menu/ui_menu_structure.c) > support/ui/menu/menu-handbook-build.timestamp)
	echo "menu-handbook build completed."
fi
