#Search on Google
google()  {
	emulate -L zsh
	${=BROWSER} "http://www.google.com/search?&num=100&q=$*"
}

#Search French Wiktionary
wikdic_fr()  {
    emulate -L zsh
    ${=BROWSER} http://fr.wiktionary.org/wiki/${(C)1// /_}
}
#Search English  Wiktionary
wikdic_en()  {
    emulate -L zsh
    ${=BROWSER} http://en.wiktionary.org/wiki/${(C)1// /_}
}

#Search French Wikipedia
wiki_fr()  {
    emulate -L zsh
    ${=BROWSER} "http://fr.wikipedia.org/w/index.php?title=Special%3ASearch&search=$*"
}
#Search English Wikipedia
wiki_en()  {
    emulate -L zsh
    ${=BROWSER} "http://en.wikipedia.org/w/index.php?title=Special%3ASearch&search=$*"
}

#French conjugaison
verbe() {
    emulate -L zsh
    ${=BROWSER} "http://www.la-conjugaison.fr/du/verbe/${*}.php"
}

weather() {
    emulate -L zsh
    [[ -n "$1" ]] || {
        print 'Usage: weather <station_id>' >&2
        print 'List of stations: http://en.wikipedia.org/wiki/List_of_airports_by_ICAO_code'>&2
        return 1
    }

    local VERBOSE="yes"    # TODO: Make this a command line switch

    local ODIR=`pwd`
    local PLACE="${1:u}"
    local DIR="${HOME}/.weather"
    local LOG="${DIR}/log"

    [[ -d ${DIR} ]] || {
        print -n "Creating ${DIR}: "
        mkdir ${DIR}
        print 'done'
    }

    print "Retrieving information for ${PLACE}:"
    print
    cd ${DIR} && wget -T 10 --no-verbose --output-file=$LOG --timestamping http://weather.noaa.gov/pub/data/observations/metar/decoded/$PLACE.TXT

    if [[ $? -eq 0 ]] ; then
        if [[ -n "$VERBOSE" ]] ; then
            cat ${PLACE}.TXT
        else
            DATE=$(grep 'UTC' ${PLACE}.TXT | sed 's#.* /##')
            TEMPERATURE=$(awk '/Temperature/ { print $4" degree Celcius / " $2" degree Fahrenheit" }' ${PLACE}.TXT | tr -d '(')
            echo "date: $DATE"
            echo "temp:  $TEMPERATURE"
        fi
    else
        print "There was an error retrieving the weather information for $PLACE" >&2
        cat $LOG
        cd $ODIR
        return 1
    fi
    cd $ODIR
}

battery() {
if [[ $BATTERY -gt 0 ]] ; then
    PERCENT="${${"$(acpi 2>/dev/null)"}/(#b)[[:space:]]#Battery <->: [^0-9]##, (<->)%*/${match[1]}}"
    if [[ -z "$PERCENT" ]] ; then
        PERCENT='acpi not present'
    else
        if [[ "$PERCENT" -lt 20 ]] ; then
            PERCENT="warning: ${PERCENT}%%"
        else
            PERCENT="${PERCENT}%%"
        fi
    fi
fi
}

mgrep () {
    find . -name .repo -prune -o -name .git -prune -o -path ./out -prune -o -regextype posix-egrep -iregex '(.*\/Makefile|.*\/Makefile\..*|.*\.make|.*\.mak|.*\.mk)' -type f -print0 | xargs -0 grep --color -n "$@"
}

cgrep () {
    find . -name .repo -prune -o -name .git -prune -o -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.h' \) -print0 | xargs -0 grep --color -n "$@"
}

jgrep () {
    find . -name .repo -prune -o -name .git -prune -o -type f -name "*\.java" -print0 | xargs -0 grep --color -n "$@"
}

resgrep () {
    for dir in `find . -name .repo -prune -o -name .git -prune -o -name res -type d`
        do
            find $dir -type f -name '*\.xml' -print0 | xargs -0 grep --color -n "$@"
                done
}

gettop () 
{ 
    local TOPFILE=build/core/envsetup.mk;
    if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ]; then
        echo $TOP;
    else
        if [ -f $TOPFILE ]; then
            PWD= /bin/pwd;
        else
            local HERE=$PWD;
    T=;
    while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
        \cd ..;
    T=`PWD= /bin/pwd`;
    done;
    \cd $HERE;
    if [ -f "$T/$TOPFILE" ]; then
        echo $T;
    fi;
    fi;
    fi
}

croot ()
{
    T=$(gettop);
    if [ "$T" ]; then
        \cd $(gettop);
    else
        echo "Couldn't locate the top of the tree.  Try setting TOP.";
    fi
}

