#!/bin/bash
#FileName: git_tarball_install.sh
#Description: Script for git
#Author: shuhui

# Define Variables
GLOBAL_NAME="admin"
GLOBAL_EMAIL="admin@vqiu.cn"

# gitosis repository address
gitOSIS_REPO="git://github.com/res0nat0r/gitosis.git"

# Define IP Address
IP="127.0.0.1"


function Remove_Git()
{
	# Remove git 
	if [[ -f /usr/bin/git ]]; then
		echo "git is already installed,Removing"
		yum -y remove git
	fi
}

function Dependent_Install()
{
	for i in zlib-devel openssl-devel perl-devel cpio expat-devel gettext-devel unzip
	do
		yum -y install ${i}
	done
}

function Add_User
{
    id git >/dev/null 2>&1
    if [[ $? -eq 1 ]]; then
        useradd -c "git user" git
    fi 
}

function Generate_keys()
{
	# Generate the Keys
	if [[ ! -e ~/.ssh/id_rsa ]]; then
		ssh-keygen -t rsa -b 2048 -P "" -f ~/.ssh/id_rsa -C ${GLOBAL_EMAIL}
	fi
}


function Git_Install()
{
	if [[ ! -e git-master.zip ]]
	then
		wget -c https://codeload.github.com/git/git/zip/master -O git-master.zip
	fi
	unzip git-master.zip;cd git-master
	make configure
	./configure --prefix=/usr
	make all doc info;
	make install install-doc install-html install-infoinstall

	# Set Global Info.
	git config --global user.name ${GLOBAL_NAME}   
	git config --global user.email ${GLOBAL_EMAIL}

}

function gitosis
{
	# Install Dependent Package
	yum -y install python python-setuptools

	# Clone gitosis repositories
	git clone ${gitOSIS_REPO}
	cd gitosis/

	# Install gitosis
	python setup.py install

	# Import Public key
	sudo -H -u git gitosis-init < ~/.ssh/id_rsa.pub

	# Set privileges[Allow update]
	chmod 755 /home/git/repositories/gitosis-admin.git/hooks/post-update
}

function client {
	cd ~
	# Clone the Gitosis-admin Repositories
	git clone git@${IP}:gitosis-admin.git
}

#Remove_Git
#Dependent_Install
Add_User
Generate_keys
#Git_Install
gitosis
client
