setup_submodules 2.45 KB
#!/bin/bash 
# =================
# = Do not chenge.
# =================
GIT_URL_SUBS="http://gitlab.osdev.nl/open_source"
TOP_REPO="-1"

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 [ -d ./.git ]; then
        return 1
    elif [ -d ../.git ]; then
        if [ -f ../.submodules ]; then
            echo "Seems like we're already a submodule. Nothing to do here."
            return 0
        fi
    fi
    return 0
}

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
        return 1
    fi
    return 0
}

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} ${SUB_MODULE}
        git config submodule.${SUB_MODULE}.url ${GIT_URL_SUBS}/${SUB_MODULE}
    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        ==
# =============================================================================
RESULT=check_top_or_sub()
if [ $RESULT -eq 0 ]; then
    echo "Seems like we're a submodule already or not part of a repository."
    exit 0
fi

RESULT=check_working_dir()
if [ $RESULT -eq 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