#! /bin/bash

# usage:	delete <REPOSITORY> - PERMANENTLY delete a repository if existing.
#		CAREFUL, this action cannot be undone. This command will ask for confirmation.

GITDIR="/var/git"

function is_bare() {
	repodir=$1
	if "$(git --git-dir="$repodir" rev-parse --is-bare-repository)" = true
	then
		true
	else
		false
	fi
}

if [ ! -z $1 ]; then
	PROJECT=$1
else
	read -p 'Project to delete: ' PROJECT
fi

if [ -d ${GITDIR}/${PROJECT}.git ]; then
	if [[ $(ls -A ${GITDIR}/${PROJECT}.git) ]]; then
		if is_bare ${GITDIR}/${PROJECT}.git
		then
			echo "You are going to delete the git repository \"${PROJECT}.git\" Do you really want to continue? Note, this action cannot be reverted. [y/N]"
			read delAnswer
			case $delAnswer in
				Y|y)
					rm -rfv ${PROJECT}.git
					if [ $? == 0 ]; then
						echo "Successfully deleted ${PROJECT}.git"
					else
						echo "An error occurred while deleting ${PROJECT}.git"
					fi
					;;
				N|n)
					echo "Aborting due to user request."
					exit 173
					;;
				*)
					echo "you said \"$delAnswer\" which I don't understand. Assuming No. Aborting."
					exit 177
					;;
			esac
		else
			echo "\"${PROJECT}.git\" doesn't look like a git repository. Check with your System Administrator."
			exit 177
		fi
	else
		echo "\"${PROJECT}.git\" is an empty directory, Skipping. Check with your System Administrator."
		exit 177
	fi
fi


