#!/usr/bin/env bash

TEMPLATE_DIR="swh-py-template"

usage () {
    echo "Usage: cherry-py-repo-from-template GIT_REVISION [REPO_NAME]"
    echo
    echo "Apply the GIT_REVISION change of the swh-py-template repository to either"
    echo "REPO_NAME, or all Python repositories"
    exit 1
}

if [ -z "$1" ] ; then
    usage
fi
gitrev="$1"
shift 1

apply_to_repo () {
    repo="$1"
    echo "* Applying to repo $1..."
    (cd "$TEMPLATE_DIR" && git show "$gitrev") | (cd "$repo" ; git apply)
}

if [ -n "$1" ] ; then
    repo="$1"
    apply_to_repo "$repo"
else
    for repo in `bin/ls-py-modules` ; do
	apply_to_repo "$repo"
    done
    echo "All done."
fi
