#!/bin/bash
###############################################################################
# But: automatiser au maximum la création d'une liste ezmlm à partir d'une liste Yahoo
# Author :  Jean-Pascal Milcent <jpm@tela-botanica.org>
# Contributor : David Delon <david@tela-botanica.org>
# License: GPL v2
# Created: Mai 23, 2008
# Version: 0.1
# $Id$
###############################################################################
# Incrément pour ezmlm-archive
INCREMENT=1000

E_OPTERR=65
if [ "$#" -eq 0 ]
then   # Script needs at least one command-line argument.
	echo "Usage $0 -[parametres -l,-n-[options -i]"
	echo "Parametres : "
	echo "	-l: le nom de la liste de discussion"
	echo "	-n: le nombre de message maximum de la liste"
	echo "Options : "
	echo "	-i: Incrément pour ezmlm-archive"
	exit $E_OPTERR
fi  

# Get parameters and options
TEMP=`getopt -o l:n:i: -- "$@"`
if [ $? != 0 ] ; then echo "Arrêt en cours..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
while [ ! -z "$1" ] ; do
	echo $1" # "$2
	case "$1" in
		-l) LISTE_ORIGINE=$2;;
		-n) NBRE_MESSAGE=$2;;
		-i) INCREMENT=$2;;
		--) shift ; break ;;
		*) echo "Erreur interne!" ; exit 1 ;;
	esac
	shift 2 
done
# Passage en minuscue du nom de la liste d'origine
LISTE=`expr "xxx$LISTE_ORIGINE" : 'xxx\(.*\)' | tr '[A-Z]' '[a-z]'`
echo "Nous nous rendons dans /home/mon_compte/forums/..."
cd /home/mon_compte/forums/
echo "Supression des archives..."
rm -fR /home/vpopmail/domains/mon_domaine/$LISTE/archive/
echo "Supression du fichier num..."
rm /home/vpopmail/domains/mon_domaine/$LISTE/num
echo "Création des archives a partir du fichier mbox.."
nice -n 15 perl mbox2ezmlm.pl --min annee_du_premier_message_de_la_liste --max annee_du_dernier_message_de_la_liste -i ./archives/$LISTE.mbox /home/vpopmail/domains/mon_domaine/$LISTE//
echo "Modification des droits sur le contenu du dossier archive..."
chmod -R 744 /home/vpopmail/domains/mon_domaine/$LISTE/archive
echo "Recréaton de l'index pour ezmlm..."
nice -n 19 /usr/local/bin/ezmlm/ezmlm-idx -d /home/vpopmail/domains/mon_domaine/$LISTE
echo "Recréaton des archives pour ezmlm..."
# L'indexation de plus de 2000 messages d'un seul coup peut ne pas créer tous les index....
i=0;
while (( i < NBRE_MESSAGE ));
do
	j=$((i+INCREMENT));
	nice -n 15 /usr/local/bin/ezmlm/ezmlm-archive -f $i -t $j /home/vpopmail/domains/mon_domaine/$LISTE;
	i=$j;
done
