#!/bin/bash

email=$1
password=$2
if [ -z "$email" -o -z "$password" ]; then
  echo """Usage: $(basename $0) email password
  email - google email name
  password - google mail password
"""
  exit 1
fi


# Auth=DQAAAHIAAAAxUi5NvC3E2B5HPUYhF4ObdUPVSGxaHNsAscnXydkheXkBw-wjzb6QCARpr2hKe1qAqIBDug4xh_rQhw6IGo4Bj87rjbdXA9SjBJySgVDcHXJkPwaY3Gh1jR-jRNGaTqv-jW2WPS6nYgZtKUh_8lEkKDdEBvTsK_3TFn2GQlIUNg
# 

if [[ "$(wget -q -O- --post-data "Email=$email&Passwd=$password&service=cl&source=mcyster-test-1" "https://www.google.com/accounts/ClientLogin")" =~ "Auth=(.*)" ]]; then
  auth="${BASH_REMATCH[1]}"
else
  echo "Auth failed"
  exit 1
fi

if [[ "$(wget -q -O - --header="Authorization: GoogleLogin auth=$auth" "http://www.google.com/calendar/feeds/default/private/full?start-min=2000-01-01T00:00:00&start-max=2040-01-01T23:59:59&max-results=1000")" =~ 'link[[:space:]]+rel=.edit.[[:space:]]+type=.application/atom\+xml.[[:space:]]+href=.(.*).></link>' ]]; then
  url="${BASH_REMATCH[1]}"
else
  echo "Hack to work around wget's redirect issue and get session id failed"
  exit 1
fi

wget -v    -O - --header="Authorization: GoogleLogin auth=$auth" --post-file=event1.xml "$url"


