#!/bin/bash

. /etc/flenv

gituninstall=$flpath/git_uninstall
gitrecovery=$gituninstall/recovery
gitunused=$gituninstall/unused

########################################
### utilities

# [in] from
# [in] to
mvdir(){
	pushd $1
	find . -type f -name \* | \
		while read filepath ; do \
			echo "mv: $filepath" ; \
			mkdir -p `dirname $2/$filepath` ; \
			mv -f $filepath $2/$filepath ; \
		done
	popd
}

########################################
### git adjustment and install

gitfiles=/etc/gitfiles

if [ ! -d "$gituninstall" ] ; then
	echo "ERROR: $gituninstall is not found."
	exit 1
fi

echo "INFO: removing msysgit own files..."

if [ -f "$gitfiles.use" ] ; then
	for target in `cat $gitfiles.use` ; do
		rm -f /$target || exit 1
	done
fi

if [ -d "$gitrecovery" ] ; then
	mvdir $gitrecovery ""
fi

rm -fr $gituninstall
rm -f $gitfiles.*

echo "INFO: empty directories are not removed by this script."
echo "INFO: finished."
