#!/bin/bash

# shellcheck disable=2162

function delete_local_branch() {
    git branch -D "$1"
}

function delete_remote_branch() {
    git push origin --delete "$1"
}

function list_of_the_branches() {
    echo
    echo "List of branches:"
    echo -e "$(git branch)"
    echo
}

function remove_branch() {
    BRANCH="$1"
    echo "$BRANCH"
    read -p "Remove locally / remote / both [L/R/B]: " CHOOSE
    case $CHOOSE in
        [Bb]*)
            delete_local_branch "$BRANCH"
            delete_remote_branch "$BRANCH"
            unset -f BRANCH
            ;;
        [Ll]*)
            delete_local_branch "$BRANCH"
            unset -f BRANCH
            ;;
        [Rr]*)
            delete_remote_branch "$BRANCH"
            unset -f BRANCH
            ;;
        *) echo "Please write good answer" ;;
    esac
}

BRANCH="$1"

if [ -z "$BRANCH" ]; then
    while true; do
        list_of_the_branches
        read -p "Select branch to remove: " BRANCH
        remove_branch "$BRANCH"
    done
else
    remove_branch "$BRANCH"
fi