#!/bin/sh
#
# Jani Salonen <salojan@goto10.co>
#

if [ "${1}" = "" ]; then
	echo "Usage: ${0} PROJECT"
	echo ""
	echo " * for example ${0} gui_vesmir"
	echo ""

	exit 0
fi

_d=$(echo "${1}" | sed -e 's|/$||');

if [ ! -d "${_d}" ]; then
	echo "Failed to access example directory ${_d}. Abort."

	exit 1
fi

# Remove files copied in previous run
#
app_remove() {
	(cd ../ || exit 1

	if [ -f "./Makefile" ]; then
		gmake erase >/dev/null 2>&1

		if [ "$?" != 0 ]; then
			if [ -f "./Makefile.no_gnu" ]; then
				make -f Makefile.no_gnu erase >/dev/null 2>&1
			fi
		fi
	fi

	_n=$(cat ./PROJECT 2>/dev/null | grep -v '^#' | head -2 | tail -1)

	if [ "${_n}" != "" ]; then
		if [ -d "examples/${_n}" ]; then
			echo " * get rid of ${_n} program files"

			(cd "examples/${_n}" ; ls \
			cmdline_app.c conf_app.c dynload_bind_app.c \
			dynload_ret_app.c main_app.c \
			cmdline_app.h conf_app.h dynload_bind_app.h \
			dynload_ret_app.h main_app.h \
			declarations_app.h defines_app.h structures_app.h \
			symbols.dyn 2>/dev/null) | while read a; do
				if [ -f "work/${a}" ]; then
					cmp "examples/${_n}/${a}" "work/${a}" >/dev/null 2>&1

					if [ "$?" = 0 ]; then
						rm -f "work/${a}"
					fi
				fi
			done

			(cd work ; ls *.c *.h 2>/dev/null) | while read a; do
				if [ -f "examples/${_n}/${a}" ]; then
					cmp "examples/${_n}/${a}" "work/${a}" >/dev/null 2>&1

					if [ "$?" = 0 ]; then
						rm -f "work/${a}"
					fi
				fi
			done

			rm -rf ./Makefile.in ./Makefile.no_gnu.in ./po/*

			touch po/KEEPME
		fi
	fi)
}

# Restore files from vault and copy program files in place
#
app_restore() {
	cd ../ || exit 1

	echo " * restoring files from vault"

	(cd vault ; ls *.c *.h *.dyn 2>/dev/null) | while read a; do
		if [ -f "work/${a}" ]; then
			cmp "work/${a}" "vault/${a}" >/dev/null 2>&1

			if [ "$?" != 0 ]; then
				echo "   moving work/${a} as work/${a}.old and restoring original"

				mv -f "work/${a}" "work/${a}.old"
				cp -f "vault/${a}" work/
			fi
		fi
	done

	#
	cp -f vault/PROJECT .
	cp -f vault/*.c vault/*.h vault/*.dyn work/

	#
	for i in Makefile.in Makefile.no_gnu.in; do
		if [ -f "examples/${1}/${i}" ]; then
			cp -f "examples/${1}/${i}" .
		else
			cp -f "vault/${i}" .
		fi
	done

	ls "examples/${1}"/*.c "examples/${1}"/*.h "examples/${1}"/*.dyn \
		2>/dev/null | while read a; do

		cp -f "${a}" work/
	done

	if [ -d "examples/${1}/share" ]; then
		cp -Rp "examples/${1}/share/"* share/
	fi
}

# Compile ano, widget and menu files
#
app_compile() {
	echo " * compiling scripts for ${1}"

	./build/ano "examples/${1}/${1}.ano" > \
		engine/dsl_ano.h || exit 1

	./build/ano -dt "examples/${1}/${1}.ano" > /dev/null 2> \
		work/symbols.dyn || exit 1

	./build/menu "examples/${1}/${1}.m2c" > \
		engine/menu_defs.h || exit 1

	./build/widget "examples/${1}/${1}.w2c" > \
		engine/widget_defs.h || exit 1
}

# Generate PROJECT file from Ano script header
#
app_project() {
	echo " * generating project file for ${1}"

	_n=$(cat "examples/${1}/${1}.ano" | grep "@ANO_SCRIPT_NAME" | \
		awk '{print $NF}')

	_v=$(cat "examples/${1}/${1}.ano" | grep "@ANO_SCRIPT_VERSION" | \
		awk '{print $NF}')

	_c=$(date +%Y)
	_x=$(echo "${_n}" | tr '[A-Z]' '[a-z]')

	rm -f ./Makefile ./Makefile.no_gnu ./PROJECT

	echo "${_n}" >> ./PROJECT
	echo "${_x}" >> ./PROJECT
	echo "${_c}" >> ./PROJECT
	echo "${_v}" >> ./PROJECT
}

# Generate configs for project
#
app_config() {
	echo " * generating configuration files for ${1}"

	_n=$(cat "examples/${1}/${1}.ano" | grep "@ANO_SCRIPT_NAME" | \
		awk '{print $NF}')

	_x=$(echo "${_n}" | tr '[A-Z]' '[a-z]')

	(cd etc ; ls detroit.*.in | while read a; do
		_e=$(echo "${a}" | sed -e "s|[a-z]*|${_x}|")

		cp -f "${a}" "${_e}" 2>/dev/null
	done)
}

app_done() {
	echo ""
	echo "   engine/dsl_ano.h, engine/menu_defs.h and engine/widget_defs.h"
	echo "   has been built, and configuration and project files generated."
	echo ""
	echo "   To try example without installing it, go back to pkg root"
	echo "   directory (that's where the configure script is) and run:"
	echo ""
	echo "    $ ./configure --prefix=\`pwd\` \\"
	echo "                  --with-libiconv=/where/is/your/libiconv"
	echo "      # or 'make reconfig' if configure has been already run for"
	echo "      # this example"
	echo "    $ gmake"
	echo ""
	echo "   Start the example program from there because example projects uses"
	echo "   files relative to this, examples, directory."
	echo ""
}

#
echo ""

app_remove "${_d}"
app_restore "${_d}"
app_compile "${_d}"
app_project "${_d}"
app_config "${_d}"

app_done "${_d}"

exit 0
