#!/bin/bash

#
#    gotmail4evolution - gets hotmail and makes ready for evolution.
#
#    Copyright (C) 2005 Jon Phillips <jon@rejon.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#
#
# This is an ugly script to get hotmail email and put them in the proper 
# place by force to be used inside Evolution
#
#
# INSTALL NOTE: Put this into you local scripts directory like:
#
#    ~/bin
# 
# After that, then run on your own evolution folder.
#
#
# TODO: Make this work for multiple non-hotmail.com domains
# TODO: Make this work for more than Inbox and Junk Mail folders.
# TODO: Do file tests.
#
#


# for debugging
VERBOSE=

#MAILPATH=""
MAILPATH="$HOME/.evolution/mail/local"

USERNAME=
PASSWORD=

GOTMAIL=gotmail

test_ret_val ()
{
    # arg 1 is error code and optional arg 2 is message indicating error

    if [ $1 != 0 ]
    then :
        if [ "$2" ]
	then :
	    echo $2
	else :
            echo "There seemed to be a problem with the script (error code $1)."
        fi
	echo ""
        exit 1
    fi
    
    #fi
}

# proc_mbox ()
#
# This sub processes a passed old mbox and moves to the proper location.
#
proc_mbox ()
{
    # $1 is tmp box and $2 is new box
    if [ -z "$1" ] && [ -z "$2" ]
    then :
        # echo "$1 $2"
        #echo "You must provide username and password for accounts"
        return 1
    else :
        # Process vars

        if [ -e "$1" ] # if the mbox exists
	then :
	    cat "$1" >> "$2"
	    chmod 600 "$1"
	    chmod 600 "$2"
	fi

    fi

    return 0
}

#
# getMyHotmail ()
#
# This sub uses the gotmail script to get my two main hotmail accounts
#
get_my_hotmail ()
{
    # $1 is username and $2 is password
    if [ -z "$1" ] && [ -z "$2" ]
#    if [ $1 ]
    then :
        echo "$1 $2"
        #echo "You must provide username and password for accounts"
        #return 1
    else :
        #OLD:  MAILPATH="$HOME/evolution/local/$1@hotmail.com"
        TMP_PATH="/tmp/$1@hotmail.com"
        INBOX="$MAILPATH/$1@hotmail.com"
        JUNKMAIL="$INBOX.sbd/Junk"


        # If it doesn't exist, then create, otherwise clear out the stuff in it
        if [ ! -e "$TMP_PATH" ] # if it doesn't exist
        then :
            mkdir $TMP_PATH
	else :
 	    rm -Rf $TMP_PATH/*
        fi

        RETURN=$?
        test_ret_val "$RETURN"

        if [ ! -z $VERBOSE ]; then
	   VERBOSECMD="-v"
        fi
        $GOTMAIL -u $1 -p $2 --folder-dir $TMP_PATH --mark-messages-as-read \
	    --delete $VERBOSECMD
        RETURN=$?
	test_ret_val "$RETURN"
       

	# deal with 
        proc_mbox "$TMP_PATH/Inbox" "$INBOX"
        RETURN=$?
	test_ret_val "$RETURN"
		
	proc_mbox "$TMP_PATH/Junk E-Mail" "$JUNKMAIL"
        RETURN=$?
	test_ret_val "$RETURN"

    fi

    return 0
}


print_help ()
{
    echo -e "\nGets hotmail and puts it somewhere evolution can see it."
    echo -e "\nUSAGE:\n`basename $0` [-hqv] [-u USERNAME ] [-p PASSWORD]" \
        " [-m PATH_TO_EVO_MAIL]\n"
    echo -e "\t-h help"
    echo -e "\t-v verbose output"
    echo -e "\t-q quiet output"
    echo -e "\t-u username"
    echo -e "\t-p password"
    echo -e "\t-m path to evolution mail\n"
    exit 1
}


# This is the main driver code
# First need to check if we are online and getting hotmail.com
#

# ping -c 1 hotmail.com
# RETURN=$?
# test_ret_val "$RETURN" "Your Internet connection or hotmail.com is offline."

###############################################################################
# MAIN DRIVER CODE
###############################################################################

# deal with commandline args (a : at the beginning suppresses some errors)
# put the : after a flag to grab next parameter as option
while getopts  "hvqu:p:m" flag
do
    # echo "$flag" $OPTIND $OPTARG
    case "$flag" in
	v) VERBOSE=1;;
	q) VERBOSE=;;
	u) USERNAME="$OPTARG";;
	p) PASSWORD="$OPTARG";;
	m) MAILPATH="$OPTARG";;
        h) print_help;;
        \? ) print_help;;
	* ) print_help;;
    esac
done
# if no arguments, then do this (don't need currently)
#if [ "$#" == 0 ]
#then :
#    print_help

if [ -z $USERNAME ] && [ -z $PASSWORD ]
then :
    echo $USERNAME
    echo $PASSWORD
    print_help
fi

get_my_hotmail $USERNAME $PASSWORD

exit 0

