#!/bin/bash
shopt -s extglob
set -a

# if argument, do nothing

# else scans all Home pages in Main, and add a "   * Account: " field
# to home pages

ldap=/www/wiki/pub/ldap-wikinames
cd /www/wiki/data/Main

find . -name \*.txt| while read topic; do
  topic=${topic#./}
  if egrep -sqi '^	+[*] name:' $topic; then
    if ! egrep -sqi '^	+[*] account:' $topic; then
      email=`egrep -i '^	+[*] email:' $topic`
      email="${email##*[eE]mail:*( )}"
      email="${email%[ 	]*}"
      if test -z "$email"; then echo "***WARNING: no email in $topic"
      else
	if ! fgrep -sqi " $email " $ldap; then
	  echo "***WARNING: no email $email in ldap for $topic"
	else
	  account=`fgrep -i " $email " $ldap`
	  account="${account#* }"
	  account="${account%% *}"

          echo "adding $account to $topic"
	  if test -z "$1"; then
 	    mv $topic $topic.b
            echo "	* Account:  <a href='http://people.ilog.fr/employee-$account'>$account</a>" >$topic
	    cat $topic.b >>$topic
	    rm -f $topic.b; chmod a+rw $topic
          fi
	fi
      fi
    fi
  fi
done
