#!/bin/sh

PATH=/usr/bin:/bin
IFS=" "

HOW='/usr/bin/curl -s'
URLBASE='https://www.google.com'
TMP='/tmp/.gcalproxy.tmp.'"$$"
trap 'rm -f "$TMP" ; exit' INT TERM EXIT

QUERY="`echo \"$QUERY_STRING\" \
       | perl -pe 's/\+/ /g; s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;'`"

if $HOW "$URLBASE""$QUERY" > "$TMP" 2>/dev/null; then
    echo "Content-type: text/calendar"
    echo ""
    cat "$TMP"
    rm -f "$TMP"
    exit 0
else
    rm -f "$TMP"
    echo 'HTTP/1.1 404 Not Found'
    echo ''
    echo '<html><body><p>my proxy script failed</p></body></html>'
    exit 1
fi
