#!/bin/dash
## Script to apply git patches for submodules.
##
## Authors:
##	- Miguel Jimenez Lopez (Seven Solutions, www.sevensols.com)
##
## Copyright 2019 CERN (www.cern.ch)
##
## This file is part of WR Starting Kit project.
## (see https://www.ohwr.org/project/wr-starting-kit).
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>
#######################################################################################################

### 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
