#!/bin/bash

# gt script for git by andy.isd-group.com
#
# INSTALLATION:
# 1) copy to /usr/local/bin
# 2) sudo chmod +x /usr/local/bin/gt
#
# v1.0: 2018-xx-xx: initial version (test one)
# v1.1.5: 2019-01-29: current branch detecting, new|fe|cl|clone|close|br|selfupdate commands added, st default commang, bugfixed, spaces in quotes fix
# v1.1.6: 2019-01-29: added keys -v, -h
# v1.1.7: 2019-02-27: bugfixes
# v1.1.8: 2019-04-28: merge command added
# v1.1.9: 2019-05-14: --no-edit added when merge
# v1.1.10: 2019-07-12: --no-edit added when pu, pull
# v1.1.11: 2019-08-23: remotes added
# v1.2: 2020-01-14: git tag release added on deploy command
# v1.2.1: 2020-01-22: git tag force and push added to deploy
# v1.2.2: 2020-01-22: gt deploy v1.1 (optional tag param added to gt deploy)
# v1.2.3: 2020-02-03: gt close and gt finish implemented

VERSION="1.2.3"

if [[ "$1" == "selfupdate" ]]; then
#    wget -O /usr/local/bin/gt http://andy.isd-group.com/git-gt/gt
    curl https://andy.isd-group.com/git-gt/gt -o /usr/local/bin/gt
    chmod +x /usr/local/bin/gt
    exit 0;
fi

if [[ "$1" == "-v" ]]; then
    echo "gt version $VERSION";
    exit 0;
fi

if [[ "$1" == "-h" ]]; then
    echo "gt version $VERSION";
    echo "Available commands: st|br|cm|pu|push|pull|add|new|fe|cl|clone|close|test|deploy|selfupdate";
    exit 0;
fi

if [[ "$1" == "cl" ]]; then
    git clone git@gitlab.com:isd-team/$2.git .
    exit 0;
fi

if [[ "$1" == "clone" ]]; then
    git clone "$2" .
    exit 0;
fi

BRANCH=$(git branch | grep \* | cut -d ' ' -f2)

if [[ "$BRANCH" == "" ]]; then
    echo "git repo not found here. Please check that you are in the right folder.";
    exit 0;
fi

if [[ "$1" == "st" ]]; then
    git status;
    exit 0;
fi

if [[ "$1" == "" ]]; then
    git status;
    exit 0;
fi

if [[ "$1" == "br" ]]; then
    git branch;
    exit 0;
fi

if [[ "$1" == "remotes" ]]; then
    git branch -a
    exit 0;
fi

if [[ "$1" == "fe" ]]; then
    git fetch;
    exit 0;
fi

if [[ "$1" == "new" ]]; then
    if [[ "$BRANCH" != "dev" ]]; then
        echo "You are on a wrong branch $BRANCH. Please checkout to dev.";
        exit 0;
    fi
    git checkout -b "$2";
    exit 0;
fi

if [[ "$1" == "close" ]]; then
    git checkout dev
    git branch -D "$BRANCH"
    exit 0;
fi

if [[ "$1" == "add" ]]; then
    git add .;
    exit 0;
fi

if [[ "$1" == "cm" ]]; then
    git commit -a -m "$2";
    exit;
fi

if [[ "$1" == "pu" ]]; then
    git pull --no-edit origin $BRANCH;
    git push origin $BRANCH;
    exit;
fi

if [[ "$1" == "push" ]]; then
    git push origin $BRANCH;
    exit;
fi

if [[ "$1" == "pull" ]]; then
    git pull --no-edit origin $BRANCH;
    exit;
fi

if [[ "$1" == "reset" ]]; then
    git reset --hard HEAD;
    exit;
fi

if [[ "$1" == "co" ]]; then
    git checkout "$2";
    exit;
fi

if [[ "$1" == "merge" ]]; then
    git merge --no-edit --no-ff "$2";
    exit;
fi

if [[ "$1" == "close" ]]; then
    git checkout master
    git branch -d $BRANCH
fi

if [[ "$1" == "finish" ]]; then
    git checkout master
    git branch -D $BRANCH
    git push origin $BRANCH
fi

if [[ "$1" == "test" ]]; then
    git pull origin $BRANCH;
    git push origin $BRANCH;
    git checkout test;
    git pull origin test;
    git merge --no-edit --no-ff $BRANCH;
    git push origin test;
    git checkout $BRANCH;
    exit;
fi

if [[ "$1" == "deploy" ]]; then
    git pull origin $BRANCH;
    git push origin $BRANCH;
    git checkout master;
    git pull origin master;
    git merge --no-edit --no-ff $BRANCH;
    if [[ "$2" != "" ]]; then
        git tag "$2" -f;
        git push origin master --tags -f;
    else
        git push origin master;
    fi
    git checkout $BRANCH;
    exit;
fi
