#!/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: Clean up the code significantly.
# TODO: Do file tests.
#
#


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


testRetVal ()
{
    # 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
}

# procMbox ()
#
# This sub processes a passed old mbox and moves to the proper location.
#
procMbox ()
{
    # $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
#
getMyHotmail ()
{
    # $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=$?
        testRetVal "$RETURN"

        gotmail -u $1 -p $2 --folder-dir $TMP_PATH --mark-messages-as-read \
	    --delete
        RETURN=$?
	testRetVal "$RETURN"
       

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

    fi

    return 0
}



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

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

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

if [ -z "$1" ] || [ -z "$2" ] || [ "$1" = '-h' ] || [ "$1" = '--help' ]
then :
    echo ""
    echo "Usage: gethotmail <username> <password>"
    echo ""

elif [ ! -z "$1" ] && [ ! -z "$2" ]
then :
    getMyHotmail "$1" "$2"
fi






