#!/usr/bin/env bash

set -e

cmd=$(basename $0)

sub_help(){
    echo "Usage: $cmd <sub-command> [options]"
    echo "Sub-commands:"
    echo "    help              Show help"
    echo "    checkout          Checkout code from git"
    echo "    compile           Compile java code"
    echo "    sonar-scan        Run sonar scanner"
    echo "    send-email        Send email after stage/pipeline"
    echo ""
    echo "For help with each sub-command run:"
    echo "$cmd <sub-command> -h|--help"
    echo ""
}

sub_checkout(){
    echo "--------------CHECKING OUT GIT CODE--------------------"
    ls # test command
    cd /root/GitlabRunner/repo/
    ls
    rm -rf ./spring-ci-advanced
    git config --global user.name 'truongbb96'
    git config --global user.email 'truongbb@itsol.vn'
    git clone https://truongbb96:hoanghuynh0801@git.myitsol.com/truongbb96/spring-ci-advanced.git
    cd ./spring-ci-advanced
    echo "--------------DONE CHECKING OUT GIT CODE--------------------"
}

subcommand=$1

case $subcommand in
    "" | "-h" | "--help")
        sub_help
        ;;
    *)
        shift
        sub_${subcommand} $@
        if [[ $? = 127 ]]; then
            echo "Error: '$subcommand' is not a known sub-command." >&2
            echo "Run '$cmd --help' for a list of known sub-commands." >&2
            exit 1
        fi
        ;;
esac