#!/bin/sh

source /etc/conf.d/eworm-repo
source /usr/lib/eworm/noroot
source /usr/lib/eworm/colors

function getversion() {
	if GITTAG="$(git --git-dir="${1}" describe --abbrev=0 --tags 2>/dev/null)"; then
		printf '%s.r%s.g%s' \
			"${GITTAG}" \
			"$(git --git-dir="${1}" rev-list --count refs/tags/${GITTAG}.. --)" \
			"$(git --git-dir="${1}" log -1 --format='%h' --abbrev=7)"
	else
		printf '0.r%s.g%s' \
			"$(git --git-dir="${1}" rev-list --count HEAD --)" \
			"$(git --git-dir="${1}" log -1 --format='%h' --abbrev=7)"
	fi
}

function work_dir() {
	DIR="${1}"
	if [ "$(git --git-dir="${DIR}" rev-parse --is-bare-repository 2>/dev/null)" = "true" ]; then
		DESCRIBE="$(getversion "${DIR}")"
		echo -e "git repo ${bldwht}$(basename "${DIR}")${default} (${DESCRIBE})"

		if [ "$(git --git-dir="${DIR}" remote | wc -l)" -gt 1 -a "${GITGC}" -le 1 ]; then
			git --git-dir="${DIR}" fetch --all
		else
			git --git-dir="${DIR}" fetch --all --prune
		fi

		if [ "${GITGC}" -ge 1 ]; then
			git --git-dir="${DIR}" gc --prune=now
		fi

		if [ ! -s "${DIR}/description" ]; then
			git --git-dir="${DIR}" ls-remote --get-url > "${DIR}/description"
		fi

		[ ! -d "${DIR}/info/web" ] && mkdir -p "${DIR}/info/web"
		LASTMODIFIEDOLD="$(cat "${DIR}/info/web/last-modified" 2>/dev/null)"
		LASTMODIFIEDNEW="$(git --git-dir="${DIR}" for-each-ref --sort='-authordate' --format='%(authordate:iso8601)' --count=1 refs/heads)"
		if [ "${LASTMODIFIEDOLD}" != "${LASTMODIFIEDNEW}" ]; then
			echo "${LASTMODIFIEDNEW}" > "${DIR}/info/web/last-modified"
		fi

		if NEWDESCRIBE="$(getversion "${DIR}")"; then
			if [ "${NEWDESCRIBE}" != "${DESCRIBE}" ]; then
				echo -e "git describe for repo ${bldcyn}$(basename "${DIR}")${default} now is ${bldwht}${NEWDESCRIBE}${default}."
			fi
		fi
	elif [ "$(git --git-dir="${DIR}/.git/" --work-dir="${DIR}" rev-parse --is-bare-repository 2>/dev/null)" = "false" ]; then
		echo "${bldylw}Warning${default}: The git repo ${bldwht}$(basename "${DIR}")${default} is a working dir! Ignoring..." >&2
	else
		for NEXTDIR in $(find ${1} -mindepth 1 -maxdepth 1 -type d -executable | sort); do
			work_dir ${NEXTDIR}
		done
	fi
}

function help() {
	echo "usage: ${0} [OPTIONS]"
	echo
	echo "where OPTIONS are:"
	echo " -c URL   clone from URL"
	echo " -d DEST  clone to destination"
	echo " -g       run 'git gc' to clean up"
	echo " -h       show this help"
	echo
	echo "Default is to update all git dirs."
}

GITURL=""
GITDEST=""
GITGC=0

while getopts "c:d:gh" opt; do
	case ${opt} in
		c)
			GITURL="${OPTARG}"
			;;
		d)
			GITDEST="${OPTARG}"
			;;
		g)
			let GITGC++
			;;
		h)
			help
			exit 0
			;;
	esac
done

if [ -n "${GITURL}" ]; then
	if [ -z "${GITDEST}" ]; then
		GITDEST=$(basename "${GITURL}")
		GITDEST="${GITDEST%.git}"
	fi
	git clone --mirror "${GITURL}" "${GITDIR}/${GITDEST}"
	git --git-dir="${GITDIR}/${GITDEST}" config gc.auto 0
	echo "${GITURL}" > "${GITDIR}/${GITDEST}"/description
	mkdir -p "${GITDIR}/${GITDEST}/info/web"
	git --git-dir="${GITDIR}/${GITDEST}" for-each-ref --sort='-authordate' --format='%(authordate:iso8601)' --count=1 refs/heads > "${GITDIR}/${GITDEST}/info/web/last-modified"
	if [ -n "${SRCDEST}" ]; then
		rm -f "${SRCDEST}/$(basename ${GITDEST})"
		ln -srf "${GITDIR}/${GITDEST}" "${SRCDEST}/$(basename ${GITDEST})"
	fi
elif [ ! -x ${GITDIR}/${GITDEST} ]; then
	echo "We are not allowed to enter ${GITDIR}/${GITDEST}!" >&2
else
	work_dir ${GITDIR}/${GITDEST}
fi
