setup_submodules 2.57 KB
#!/bin/bash -x
# =================
# = Do not chenge.
# =================
GIT_URL_SUBS="http://gitlab.osdev.nl/open_source"
FUNC_RESULT=""

function print_usage_exit() 
{
    echo "Usage $0 -i|--install|-u|--update"
    echo "	-i or --install  Install the submodules mentioned in the submodules.list" 
    echo "	-u or --update   Update the submodules mentioned in the submodules.list"
    echo " "
    exit 1
}

function check_top_or_sub() 
{
    # This function checks if we're the top-repository.
    # In that case we need the submodules.. If we're already a submodule, 
    # we simply exit this script with a message
    if [ -e ./.git ]; then
	    FUNC_RESULT="1"
        return
    elif [ -e ../.git ]; then
        if [ -e ../.submodules ]; then
            echo "Seems like we're already a submodule. Nothing to do here."
            FUNC_RESULT="0"
            return
        fi
    fi
    FUNC_RESULT="0"
    return
}

function check_working_dir() 
{
    # Check if we're in the top-level directory of our repository.
    if [ -f ./scripts/submodules.list ]; then
        # We're good to go
        FUNC_RESULT="1"
        return
    fi
    FUNC_RESULT="0"
    return
}

function read_submodules() 
{
    if [ -e ./scripts/submodules.list ]; then
        source ./scripts/submodules.list
    fi
}

function add_submodules() 
{
    for SUB_MODULE in ${SUB_MODULES}
    do
        git submodule add -f ${GIT_URL_SUBS}/${SUB_MODULE}.git ${SUB_MODULE}
        git config submodule.${SUB_MODULE}.url ${GIT_URL_SUBS}/${SUB_MODULE}.git
    done
}

function get_submodules() 
{
    git submodule update --init --recursive
}

function update_submodules() {
    git submodule update --recursive
}

# =============================================================================
# ==        T H E   M A I N   E N T R Y   O F   T H I S   S C R I P T        ==
# =============================================================================
check_top_or_sub
if [ "${FUNC_RESULT}" == "0" ]; then
    echo "Seems like we're a submodule already or not part of a repository."
    exit 0
fi

check_working_dir
if [ "${FUNC_RESULT}" == "0" ]; then
    echo "Go to the top of this repository and type : scripts/setup_submodules [-i|--install]"
    exit 0
fi

read_submodules

case "$1" in
    -i*|--install*)
        echo "Installing submodules for this repository ( ${PWD} )"
        add_submodules
        get_submodules
        ;;
    -u*|--update*)
        echo "Update submodules : ${SUB_MODULES}"
        update_submodules
        ;;
    *)
        echo "No parameters found..."
        print_usage_exit
        ;;
esac