#! /bin/bash

# usage:	create <PROJECT> - create a git bare repository named PROJECT.git
#		this command will setup the repo and send a mail for confirmation.

GITDIR="/var/git"
BASEDIR=".gitbase"
MULTIMAIL="/usr/doc/git-*/contrib/hooks/multimail/git_multimail.py"
GITUSER="git"
GITGRP="git"
VENVSDIR="/usr/local/venvs"
USEVENV=false

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

function git_init() {
	PROJECT=$1
	echo "creating project \"${PROJECT}.git\""
	echo -n "Describe your project in one line: "
	read DESCRIPTION
	if [ ! -d ${GITDIR}/${PROJECT}.git ]; then
		mkdir ${GITDIR}/${PROJECT}.git
	fi
	cd ${GITDIR}/${PROJECT}.git
	git init --bare
	echo "Remember to insert a README.md file to explain what your project is about."
	mkdir custom-hooks
	ln -s $MULTIMAIL custom-hooks/git_multimail.py

	# check if we are creating a python project
	if [ $USEVENV == true ]; then
		# if yes
		cp ${GITDIR}/${BASEDIR}/post-receive-py hooks/post-receive
		sed -i "s|{{GITDIR}}|${GITDIR}|g" hooks/post-receive
		sed -i "s|{{PROJECT}}|${PROJECT}|g" hooks/post-receive
		cp ${GITDIR}/${BASEDIR}/pip_install custom-hooks/pip_install
		sed -i "s|{{VENVSDIR}}|${VENVSDIR}|g" custom-hooks/pip_install
		sed -i "s|{{PROJECT}}|${PROJECT}|g" custom-hooks/pip_install
		chmod 0755 custom-hooks/pip_install
	else
		# otherwise
		cp ${GITDIR}/${BASEDIR}/post-receive hooks/post-receive
		sed -i "s|{{GITDIR}}|${GITDIR}|g" hooks/post-receive
		sed -i "s|{{PROJECT}}|${PROJECT}|g" hooks/post-receive
	fi
	# in any case we setup so that we can deploy
	cp ${GITDIR}/${BASEDIR}/deploy.sh custom-hooks/deploy.sh
	sed -i "s|{{GITDIR}}|${GITDIR}|g" custom-hooks/deploy.sh
	sed -i "s|{{PROJECT}}|${PROJECT}|g" custom-hooks/deploy.sh
	# add multimail settings to config
	cat >> config <<EOT

[multimailhook]
	mailer = "sendmail"
	refchangeShowGraph = true
	mailingList = "danixland@gmail.com"
	commitEmailFormat = "html"
	htmlInIntro = true
	htmlInFooter = true
	from = "danix@danix.xyz"
	administrator = "danix@danix.xyz"
	quiet = true
	logFile = "/var/log/multimail.log"
	errorLogFile = "/var/log/multimail_err.log"
EOT

	# fix permissions to post-receive and deploy.sh scripts
	chmod 0755 hooks/post-receive custom-hooks/deploy.sh
	echo $DESCRIPTION > description
	cd ${GITDIR}/
	chown -R ${GITUSER}:${GITGRP} ${GITDIR}/${PROJECT}.git
	echo "All done, you can now work on \"${PROJECT}.git\""
        echo "You can now clone this repository at ssh://danix_git:/${GITDIR}/${PROJECT}.git"
	exit 0
}

if [ ! -z $1 ]; then
	if [ "python" == $1 ]; then
		USEVENV=true
		# this is a python project. Let's create a virtualenv
		if [ ! -z $2 ]; then
			PROJECT=$2
		else
			read -p 'Python project name: ' PROJECT
		fi
		virtualenv ${VENVSDIR}/${PROJECT}
		echo "virtual environment created inside ${VENVSDIR}/${PROJECT}"
	else
		PROJECT=$1
	fi
else
	read -p 'Project name: ' PROJECT
fi

if [ ! -d ${GITDIR}/${PROJECT}.git ]; then
	git_init $PROJECT
else
	echo "Project directory ${PROJECT}.git already exists."
	if [ $(ls -A ${GITDIR}/${PROJECT}.git) ]; then
		if is_bare ${GITDIR}/${PROJECT}.git
		then
			echo "looks like \"${PROJECT}.git\" is an existing git project directory, choose another name."
			exit 171
		else
			echo "\"${PROJECT}.git\" is not empty, I can't create a Git Project in it. Choose another name."
			exit 172
		fi
	else
		echo "\"${PROJECT}.git\" is an empty directory. Do you want to initialize a Git Project here? [y/N]"
		read answer
		case $answer in
			Y|y)
				git_init $PROJECT	
				;;
			N|n)
				echo "Aborting due to user request."
				exit 173
				;;
			*)
				# we assume no as default answer.
				echo "you said \"$answer\" which I don't understand, so to me is no. Aborting."
				exit 177
				;;
		esac
	fi
fi


