# Convert Arel to Google
# Auteur: sb, 16/12/2011
# Parametres:
# $1 = nom du fichier AREL à convertir
# $2 = nom du fichier Google Agenda à produire
# Exemple d'appel: ./convert.sh arel.txt google.txt

OLDIFS=$IFS

declare -a tabHeureDebut
declare -a tabHeureFin
declare -a tabAnnee
declare -a tabMois
declare -a tabJourMois
declare -a tabSalle
declare -a tabLibelle

IFS=\<
for i in `cat $1`
do
	if [ "${i:0:7}" = "creneau" ] 
	then
		IFS=$OLDIFS
		for j in $i
		do
			IFS=\"
			declare -a tab=($j)
			if [ "${j:0:11}" = "heureDebut=" ] 
			then
				tabHeureDebut[${#tabHeureDebut[*]}]=`echo "${tab[1]}" | sed 's/h/:/'`
			fi
			if [ "${j:0:9}" = "heureFin=" ] 
			then
				tabHeureFin[${#tabHeureFin[*]}]=`echo "${tab[1]}" | sed 's/h/:/'`
			fi
			if [ "${j:0:6}" = "annee=" ] 
			then
				tabAnnee[${#tabAnnee[*]}]=${tab[1]}
			fi
			if [ "${j:0:5}" = "mois=" ] 
			then
				tabMois[${#tabMois[*]}]=${tab[1]}
			fi
			if [ "${j:0:9}" = "jourMois=" ] 
			then
				tabJourMois[${#tabJourMois[*]}]=${tab[1]}
			fi
			if [ "${j:0:6}" = "salle=" ] 
			then
				tabSalle[${#tabSalle[*]}]=${tab[1]}
			fi
		done
	fi
	if [ "${i:0:3}" = "rel" ] 
	then
		IFS=$OLDIFS
		for j in $i
		do
			IFS=\"
			declare -a tab=($j)
			if [ "${j:0:8}" = "libelle=" ] 
			then
				tabLibelle[${#tabLibelle[*]}]=${tab[1]}
			fi
		done
	fi

done

echo "Subject, Start Date, Start Time, End Time, End Date" > $2
n=0
while [ $n -lt ${#tabLibelle[*]} ]
do
	echo "${tabLibelle[$n]} (${tabSalle[$n]}), ${tabJourMois[$n]}/${tabMois[$n]}/${tabAnnee[$n]}, ${tabJourMois[$n]}/${tabMois[$n]}/${tabAnnee[$n]}, ${tabHeureFin[$n]}, ${tabHeureDebut[$n]}" >> $2
	n=$((n+1))
done

IFS=$OLDIFS
