#!/bin/bash
#
# (C) Copyright 1996-2017 ECMWF.
# 
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. 
# In applying this licence, ECMWF does not waive the privileges and immunities 
# granted to it by virtue of its status as an intergovernmental organisation nor
# does it submit to any jurisdiction.

#
# git-proj: status, branch, checkout
#

txtbld=$(tput bold)             #  bold
bldblu=${txtbld}$(tput setaf 6) #  bold cyan
txtrst=$(tput sgr0)             #  reset

dashless=$(basename "$0" | sed -e 's/-/ /')
USAGE="$dashless <command> [options...]"

SUBDIRECTORY_OK="yes"

PATH=$(git --exec-path):$PATH
. git-sh-setup

require_work_tree_exists

# go to top

cd_to_toplevel
cd ..

# check option for number of levels
nlevels=$(git config --global --int --get mproj.levels)
if [ "$nlevels" != "" ]
then
    nlevels="-maxdepth $nlevels"
fi

# do it

for d in $( find $PWD $nlevels -type d -iname ".git" )
do
    gdir=$(dirname $d)
    proj=$(basename $gdir)
    pushd $gdir > /dev/null
    branch=$(git symbolic-ref --short HEAD)
    echo -e "${bldblu}---> $proj ($branch)${txtrst}"
    git "$@"
    popd > /dev/null
done


