#!/bin/sh

# Thanks to Dave Taylor at www.askdavetaylor.com for the great tutorial on accessing and parsing RSS feeds with a shell script

echo "EMail" # Puts the word Email at the top for a header to the module

# Provide your log in credentials
# NOTE - you MUST replace the @ in your username email with %40 or the scrip will fail
username='user%40gmail.com' # username is your email address with @ replaced by %40
password='password' # your password

# Set the label that you want use
label='inbox' # the GMail label that contains the email you wnat to display

#Set the Wdith of the output
width='45' # The number of columns wide that you want the output

# Required for Google Apps users
domain='company.com' # Your company domain - only needed for Google Apps users

# On the next line remove the # at the beginning of the line for the service that you are using. The top line is for GMail, the next line is for Google Apps

#url='https://'"$username"':'"$password"'@mail.google.com/mail/feed/atom/'"$label" # GMail users 
url='https://'"$username"':'"$password"'@mail.google.com/a/'"$domain"'/feed/atom/'"$label" # Google Apps users - remember to add your organization's domain in domain above

# Do not modify below this line
if [ $# -eq 1 ] ; then
  headarg=$(( $1 * 2 ))  # $(( )) specifies that you're using an equation
else
  headarg="-8"  # default is four email messages 
fi

curl --silent "$url" | grep -E '(title>|summary>)' | \
	sed -n '2,$p' | \
	sed -e 's/<title>//' -e 's/<\/title>//' -e 's/<summary>/   /' \
	      -e 's/<\/summary>//' | \
	head $headarg | fmt -w "$width"

