#!/bin/dash
## Script to apply git patches for submodules.
##
## Authors: 
##	- Miguel Jimenez Lopez (Seven Solutions, www.sevensols.com)
##
## GNU Lesser General Public License Usage
## This file may be used under the terms of the GNU Lesser
## General Public License version 2.1 as published by the Free Software
## Foundation and appearing in the file LICENSE.LGPL included in the
## packaging of this file.  Please review the following information to
## ensure the GNU Lesser General Public License version 2.1 requirements
## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
#######################################################################################################

### Show help of the function
help () 
{
cat << EOF
Usage: $(basename $0)  [Options] 
	
Options:	
	-h|--help)     Show this little help
EOF
exit 0
}

#################################################
### Main execution
GIT_REPOS="spec-sw wrpc-sw wr-nic fmc-dio coht-vic"
MAIN_DIR=".."
PATCHES_DIR="patches"

# setup script dir

sdir=$(dirname $0)
cd $sdir
sdir=$(pwd)

while [ $# -gt 0 ]; do    # Until you run out of parameters . . .
	case "$1" in
		-h|--help) help;;
		*) help;;
	esac
	shift       # Check next set of parameters.
done

for i in $GIT_REPOS
do
	if [ -e "$sdir/$MAIN_DIR/$PATCHES_DIR/$i" ] ; then
		echo "Patching $i submodule"
		cd "$sdir/$MAIN_DIR/$i"
		git am $sdir/$MAIN_DIR/$PATCHES_DIR/$i/*.patch
		cd "$sdir"
	fi
done
