#! /bin/sh

cd `dirname $0`;
main=`pwd`;
rm -f .IND1 .IND2 .IND4 .IND2a .IND4a 2>/dev/null
clist=`ls -d [0-9]* 2>/dev/null`
for cls in $clist; do
 if [ $cls -gt 9999 ]; then
  supv=`awk -F'=' '$1=="!set class_supervisor" {print $2; exit}' $cls/.def`
  type=`awk -F'=' '$1=="!set class_type" {print $2; exit}' $cls/.def`
  email=`awk -F'=' '$1=="!set class_email" {print $2; exit}' $cls/.def`
  [ -z "$type" ] && type=0
  if [ $type == 0 ]; then
     echo ":$cls,$supv,$email" >>.IND1
  else
    if [ $type == 2 ]; then
     echo ":$cls,$supv,$email" >>.IND2
     cd $cls/.users/;
     lt=`grep user_supervisable=yes * | cut -d":" -f1`;
     for t in $lt; do
       lname=`awk -F'=' '$1=="!set user_lastname" {print $2; exit}' ./$t`
       fname=`awk -F'=' '$1=="!set user_firstname" {print $2; exit}' ./$t`
       email=`awk -F'=' '$1=="!set user_email" {print $2; exit}' ./$t`
       if [ -z "$email" ]; then
        echo  "$cls,$supv,$email" >> $main/.IND2a
       fi
     done
     cd $main
    else
     echo ":$cls,$supv,$email" >>.IND4
     cd $cls/.users/;
     lt=`grep user_supervisable=yes * | cut -d":" -f1`;
     for t in $lt; do
       lname=`awk -F'=' '$1=="!set user_lastname" {print $2; exit}' ./$t`
       fname=`awk -F'=' '$1=="!set user_firstname" {print $2; exit}' ./$t`
       email=`awk -F'=' '$1=="!set user_email" {print $2; exit}' ./$t`
       if [ -z "$email" ]; then
        echo  "$cls,$supv,$email" >> $main/.IND4a
       fi
     done
     cd $main
    fi;
  fi;
fi;
done;
touch .IND1 .IND2 .IND4 .IND2a .IND4a;
env LC_COLLATE=POSIX LANG=POSIX LC_CTYPE=fr_FR.UTF-8 sort -t ',' -f -k 2 <.IND1 >.teacher-cls 2>/dev/null
env LC_COLLATE=POSIX LANG=POSIX LC_CTYPE=fr_FR.UTF-8 sort -t ',' -f -k 2 <.IND2 >.supervisor-supercls 2>/dev/null
env LC_COLLATE=POSIX LANG=POSIX LC_CTYPE=fr_FR.UTF-8 sort -t ',' -f -k 2 <.IND4 >.supervisor-portal 2>/dev/null
env LC_COLLATE=POSIX LANG=POSIX LC_CTYPE=fr_FR.UTF-8 sort -t ',' -f -k 2 <.IND2a >.teacher-supercls 2>/dev/null
env LC_COLLATE=POSIX LANG=POSIX LC_CTYPE=fr_FR.UTF-8 sort -t ',' -f -k 2 <.IND4a >.teacher-portal 2>/dev/null
rm -f .IND1 .IND2 .IND4 .IND2a .IND4a 2>/dev/null
