#!/bin/bash

dirtree=dir.tree
dirdesc=dir.desc
template=index.template.html

function update_dirtree() {
	local tmp
	tmp=$(find ./ -type d | sed -s s/./\/)
   echo "$tmp" > $dirtree
}

function gen_html() { 


	update_dirtree

	for line in $(cat $dirtree) ; do
		current=$PWD$line
		sedline=$(echo $line | sed -s 's/\//\\\//g')
		echo "Analisando $current"
		if [ "$line" == "/index.html_files" ] || 
			[ "$line" == "/index.html_files/icons" ]
 		then
			continue
		fi


		if [ -f "$current/$dirdesc" ] ; then
			tmpdesc=$(cat $current/$dirdesc)
			desc=$(cat $current/$dirdesc | sed -s 's/\//\\\//g')
			echo "Gerando descrição para diretório $line"
			cat $template | sed -s s/%title/$sedline/g | sed -e s/%description/"\\$desc"/ > $current/index.html
		fi

	done

}

gen_html
