#!/bin/sh
# gitupdate -- update all my Git projects (or the current directory)
#
# SUMMARY: If you give this no parameters,
#          it looks first in the current directory, if that's a Git directory,
#          then it acts like git update
#          If there are no arguments,
#          and if the current directory isn't a Git directory, then
#          it updates all of the immediate subdirectories of . that are Git
#          directories.
#          If you give it arguments, it updates all of the given directories.
#
# AUTHOR:	Gary T. Leavens

[ -n "$echo" ] && set -x

USAGE='gitupdate [dir] ...'

# gitdirs -- Print all immediate subdirectories that are GIT directories
# This only prints relative directory names.
gitdirs()
{
# no parameters
if test $# -ne 0
then
	echo "$USAGE" >&2
	exit 2
fi

if test -d .git
then
	echo .
else
    SUBDIRS=`echo */.git`
    if test "$SUBDIRS" \!= '*/.git'
    then
	echo "$SUBDIRS" | sed -e 's@/.git@@g'
    fi
fi
}

# no options allowed
case "$1" in
-*)
	echo "$USAGE" >&2
	exit 2
	;;
esac

if test $# -eq 0
then
    GITDIRS=`gitdirs`
else
    GITDIRS="$@"
fi

ret=0

case "$GITDIRS" in 
'*')
	exit $ret
	;;
esac

for d in $GITDIRS
do
	cd $d && echo $d
	if git fetch 
	then git pull || ret=1
	else ret=1
	fi
	cd ..
done

exit $ret

