#!/bin/sh

## Copyright (C) 2006-2013 Daniel Baumann <mail@daniel-baumann.ch>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


set -e

if [ -e /etc/default/git-stuff ]
then
	. /etc/default/git-stuff
fi

if [ "${GIT_REPACK_REPOSITORIES_ENABLE}" != "true" ]
then
	# git-repack-repositories is disabled in /etc/default/git-stuff
	exit 0
fi

if [ -z "${GIT_REPACK_REPOSITORIES_DIRECTORIES}" ]
then
	echo "E: git-repack-repositories has no directories in /etc/default/git-stuff."
	exit 1
fi

for _USER_DIRECTORY in ${GIT_REPACK_REPOSITORIES_DIRECTORIES}
do
	if [ ! -e "${_USER_DIRECTORY}" ]
	then
		echo "W: ${_USER_DIRECTORY} - no such file or directory, skipping."
		continue
	fi

	cd "${_USER_DIRECTORY}"

	_SUB_DIRECTORIES="$(for _DIRECTORY in $(find . -type d -name "*.git"); do echo $(dirname $_DIRECTORY); done | sort -u)"

	for _SUB_DIRECTORY in ${_SUB_DIRECTORIES}
	do
		cd "${_SUB_DIRECTORY}"
		git-repack-repositories ${@}
		cd "${OLDPWD}"
	done
done
