#!/bin/bash 

set -e

DESTDIR=${1:-/xdt/meta/meta-agl}

pushd $DESTDIR

meta=$(basename $(pwd -P))

for odir in $(ls -d templates/{feature,machine}/*); do
	name=$(basename $odir)
	type=$(basename $(dirname $odir))

	readme=$odir/README_${type}_${name}.md

	echo "generating $readme"

    # get authors from git
	authors=$(git log --pretty=format:"%an <%ae>" $odir | sed 's/Stéphane Desneux/Stephane Desneux/g' | sed 's/Ronan <ronan.lemartret/Ronan Le Martret <ronan.lemartret/g' | sed 's/repo sync//g' | sort | uniq | awk '{printf("%s, ",$0);}' | sed 's/, $//')
	cat <<EOF >$readme
---
description: ${type^} $name
authors: $authors
---

### ${type^} $name

*Description is missing - please complete file $meta/$readme*

EOF

	deps=
	for x in $(ls $odir/*.dep 2>/dev/null); do
		deps="$deps $(cat $x)"
	done
	[[ -n "$deps" ]] && cat <<EOF >>$readme
#### Dependent features pulled by $name

The following features are pulled:

$(for x in $deps; do echo "* $x"; done)

EOF

done

