#!/bin/bash
###############################################################################
# Purpose: run the yahoo2mbox perl script to work around the Yahoo download limits
# Author :  Jean-Pascal Milcent <jpm@tela-botanica.org> (translate bat file to shell script) 
# Contributor : David Delon <david@tela-botanica.org>
# Author of bat file :  Chris Gamlin
# License: GPL v2
# Created: Mai 23, 2008
# Version: 0.1
# $Id$
###############################################################################
# Set Number of messages to download each time (Recommend 75)...
add=75
#Set delay time in seconds (Recommend 2)...
delay=2
#Set sleep time in seconds (Recommend 90)...
sleep=90
#Country
country="fr"
#Set message download start number (usually 0)...
startat=0

# Help...
E_OPTERR=65
if [ "$#" -eq 0 ]
then   # Script needs at least one command-line argument.
	echo "Usage $0 -[parameters -g,-m, --user1,--pass1,--user2,--pass2] -[options -c,-s,--add,--delay,--sleep]"
	echo "Parameters : "
	echo "	-g: Set Yahoo Group Name"
	echo "	-m: Path to mbox file"
	echo "	--user1: set name of Yahoo user 1"
	echo "	--pass1: set password of Yahoo user 1"
	echo "	--user2: set name of Yahoo user 2"
	echo "	--pass2: set password of Yahoo user 2"
	echo "Options : "
	echo "	-s: Set message download start number (default is 0)"
	echo "	-c: Yahoo web site country [fr, de...] (default is fr)"
	echo "	--add: set Number of messages to download each time (default is 75)"
	echo "	--delay: set delay time in seconds (default is 2)"
	echo "	--sleep: set sleep time in seconds (default is 90)"
	exit $E_OPTERR
fi

# Get parameters and options
TEMP=`getopt -o s:g:m:c: -l user1:,pass1:,user2:,pass2:,add::,delay::,sleep:: -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
while [ ! -z "$1" ] ; do
	echo $1" # "$2
	case "$1" in
		-g) groupname=$2;;#Set Yahoo Group Name ...
		-m) mbox=$2;;#Path to mbox file
		-s) startat=$2;;#Set message download start number (usually 0)...
		-c) country=$2;;#Country
		--user1) username1=$2;;#Set Username 1 ...
		--pass1) password1=$2;;#Set Username 1 ...
		--user2) username2=$2;;#Set Username 2 ...
		--pass2) password2=$2;;#Set Username 2 ...
		--add) add=$2;;#Set Number of messages to download each time (Recommend 75)...
		--delay) delay=$2;;#Set delay time in seconds (Recommend 2)...
		--sleep) sleep=$2;;#Set sleep time in seconds (Recommend 90)...
		--) shift ; break ;;
		*) echo "Internal error!" ; exit 1 ;;
	esac
	shift 2 
done

retry2() {
	echo "In retry2()..."
	echo $error >log.txt
	perl yahoo2mbox-0.24.pl -o $mbox --user="$username1" --pass="$password1" --verbose --resume --country=$country --delay=$delay --end=$endat2 $groupname
	error=$?
	endat=$endat2
	if [ $error -eq 0 ]; then
		suite
	fi
	sleep 900
	retry2
}

suite2() {
	echo "In suite2()..."
	endat2=$((endat+add))
	echo $endat2
	perl yahoo2mbox-0.24.pl -o $mbox --user="$username2" --pass="$password2" --verbose --resume --country=$country --delay=$delay --end=$endat2 $groupname
	error=$?
	if [ $error -ne 0 ]; then
		retry2
	fi
	endat=endat2
	sleep $sleep
	suite2
}

retry() {
	echo "In retry()..."
	echo $error > log.txt
	perl yahoo2mbox-0.24.pl -o $mbox --user="$username2" --pass="$password2" --verbose --resume --country=$country --delay=$delay --end=$endat2 $groupname
	error=$?
	endat=$endat2
	if [ $error -eq 0 ]; then
		suite2
	fi
	sleep 900
	retry2
}

suite() {
	echo "In suite()..."
	endat2=$((endat+add))
	echo $endat2
	perl yahoo2mbox-0.24.pl -o $mbox --user="$username1" --pass="$password1" --verbose --resume --country=$country --delay=$delay -end=$endat2 $groupname
	error=$?
	echo $error
	if [ $error -ne 0 ]; then
		retry
	fi
	endat=$endat2
	sleep $sleep
	suite
}

# Main
endat=$((startat+add))
echo "Last message "$endat;
perl yahoo2mbox-0.24.pl -o $mbox --user="$username1" --pass="$password1" --verbose --resume --country=$country --delay=$delay --end=$endat $groupname
sleep $sleep
suite
