#!/bin/bash
# gitdit, a dialog interface to manage git repositories
# Author: JKA Network - contacto@jkanetwork.com

VERSION="1.0.4"
#First check if the user is in a git repository
preOption=$(git status 2>/dev/stdout | sed -n 1p | cut -f2 -d" ")
if [ $preOption = "Not" ];then #When it isn't only it can do git clone
gitopt=`dialog --stdout --scrollbar --menu "gitdit - Git DIalog inTerface $VERSION" 0 0 6 c "Download new repository (Do clone)" i "Make a new empty repo"`
else #Else, all other options
	gitopt=`dialog --stdout --scrollbar --menu "gitdit - Git DIalog inTerface" 0 0 6 p "Download repo updates (git pull)" t "Make commit without upload" s "Make commit if necc. and upload changes (git push)" d "Clean branch (Go to last commit)" n "Create new branch" b "Change branch" m "Merge branch (Do in branch you want to merge)"`
	actual="origin $(git branch --list | grep "^*" | cut -f2 -d" ")" #Current branch
	actual2=$(git branch --list | grep "^*" | cut -f2 -d" ") #Current branch name
fi
if [ ! -z $gitopt ];then #No canceled
	case $gitopt in
		#Options not in a git repo
		"i")
			gitdir=`dialog --stdout --inputbox  "Repo directory" 10 60 "."`
			if [ $(echo $gitdir | cut -f1 -d"/") = "~" ];then #~ don't understand like $HOME
			gitdirdef="$HOME"
			for (( i=2; i<=$cant; i++ ))
			do
				gitdirdef="$gitdirdef/$(echo $gitdir | cut -f$i -d"/")"
			done
			elif [ $(echo $gitdit | cut -f1 -d"/") = "."  ];then
				gitdirdef=$(pwd)
				for (( i=1; i<=$cant; i++ ))
				do
					gitdirdef="$gitdirdef/$(echo $gitdir | cut -f$i -d"/")"
				done
			fi
			if [ ! -d $gitdir ];then
				mkdir -p $gitdirdef
			fi
			if [ $? -eq 1 ];then
				echo "You dont have permission to write in this directory" >> /dev/stderr
			else
				cd $gitdirdef
				git init
			fi
		;;
		"c")
			giturl=`dialog --stdout --inputbox  "git URL: " 10 60`
			if [ -z $giturl ];then
				echo "No URL specified."
				exit
			fi
			git clone $giturl
			;;
			

		#Options of a git repo
		"p")
			git pull $actual;;
		"s")
			estado=$(git status | grep -E '(^Changes|^Untracked)' ) #It need to commit something?
			if [ "$estado" ];then #When is necesary commit
			commit=`dialog --stdout --inputbox  "Changes text: " 10 50 "changes"`
			if [[ -z $commit ]];then
				commit="changes"
			fi
			git add -A && git commit -m "$commit" && git push $actual
			else #Nothing to commit, only push
				git push $actual
			fi
			;;
		"t")
			commit=`dialog --stdout --inputbox  "Changes text: " 10 50 "changes"`
			if [[ -z $commit ]];then
				commit="changes"
			fi
			git add -A && git commit -m "$commit"
			;;
		"b")
			howopt=`dialog --stdout --scrollbar --menu "Change branch" 0 0 6 c "Only change branch" u "Change branch and update it"`
			if [ -z $howopt ];then
				exit
			fi
			j=0
			rama=""
			#Loop to create dialog whith all branches (except the current)
			for i in $(git branch -a | grep -v "^*" | grep -v "HEAD \->")
			do
				aux2=0
				if [ -z $(echo $i | grep -o "remotes") ];then
					aux=$i
					aux2=1
				else
					aux=$(echo $i | cut -f3 -d"/")
					error=0 #If 1 aux is yet in lista array
					for (( i=0;i<${#lista[@]} && error==0;i++ ))
					do
						if [ $aux == ${lista[$i]} -o $aux = $actual2 ];then
							error=1
						fi
					done
					if [ $error -eq 0 ];then
						aux2=1
					fi
				fi
				if [ $aux2 -eq 1 ];then
					rama="$rama $j $aux"
					lista[$j]=$aux #Array with the names of branches
					let j++
				fi
			done
			ramificacion=`dialog --stdout --scrollbar --menu "Select branch to goto" 0 0 6 $rama`
			if [ ! -z $ramificacion ];then
				git checkout ${lista[$ramificacion]}
				if [ $howopt = "u" ];then
					git pull origin ${lista[$ramificacion]}
				fi
			fi
			;;
		"n")
			NewBranch=`dialog --stdout --inputbox  "Name of the new branch" 10 50 "New branch"`
			if [ ! -z $NewBranch ];then
				git checkout -b $NewBranch
			fi
			;;
		"m")
			j=0
			rama=""
			for i in $(git branch --list | grep "^*" -v )
			do
				if [ $i != "*" ];then
					rama="$rama $j $i"
					lista[$j]=$i
					let j++
				fi
			done
			merges=`dialog --stdout --scrollbar --menu "Select branch for merge to $(git branch --list | grep "^*" | cut -f2 -d" ")" 0 0 6 $rama`
			if [ ! -z $merges ];then
				git merge ${lista[$merges]}
				git push $actual
			fi
			;;
		"d")
			var=1
			while [[ $var -eq 1 ]]; do
				if [[ $(ls -a | grep .git) ]]; then
					var=0
				else
					cd ..
				fi
			done
			git checkout .;
			;;
	esac
fi
