# General functions, used by other functions in this bashrc
is_feature() {
	if [[ ${FEATURES/$1} != $FEATURES ]] ; then
		return 0
	else
		return 1
	fi
}

is_ROOT() {
	if [[ "${ROOT}" == "" || "${ROOT}" == "/" ]] ; then
		return 0
	else
		return 1
	fi
}

rssh() {
	echo "Process started at: $( date +'%F %X')" 
	/usr/bin/rsync --times --links --progress --recursive --perms \
		--executability --human-readable --timeout=30 \
		-e /usr/bin/ssh $@
	echo "Process ended at: $( date +'%F %X')"
}

distclean() {
	# Clean distdir after finishing
	# From tinderbox.x86.gentoo.org thanks to Ned
	distdir="$( portageq distdir )"
	for x in ${SRC_URI}; do
		x=$(/bin/basename $x)
		dx=$(readlink -f ${distdir}/${x})
		if [[ -f $dx ]]; then
			size="$(/bin/ls -lh  ${dx} | /bin/awk '{print $5}')"
			ebegin "Removing ${x} to save ${size}"
			/bin/rm ${dx}
			eend $?
		fi
	done
}

syncpkg() {
	# Syncing the binary tbz2 to my webhost
	if is_feature "buildpkg" && \
		[[ -n $REPO_HOST && -n $REPO_BASE && -n $REPO_PATH ]] ; then
		REMOTE_TARGET="$REPO_HOST:$REPO_BASE/$REPO_PATH"
		REMOTE_ECACHE="$REPO_BASE/$REPO_PATH/settings/.ebuild.x"

		einfo "Publishing data to remote repository ($REPO_PATH) on ${REPO_HOST##*@}"

		if ! $( ssh $REPO_HOST "test -d $REPO_BASE/$REPO_PATH/settings" ) ; then
			ssh $REPO_HOST "mkdir -p $REPO_BASE/$REPO_PATH/settings" \
				>> /var/log/syncpkg.log 2>&1
		fi

		if [ -x /usr/bin/q ] ; then
			/usr/bin/qlist -IvCU > /etc/portage/package.list
		fi

		ebegin " [ SYNC: 1/3] /etc/make.conf, /etc/portage/package* to $REPO_PATH/settings"
		rssh /etc/{make.conf,portage/package*,portage/profile,portage/bin,portage/bashrc} \
			$REMOTE_TARGET/settings/ > /var/log/syncpkg.log 2>&1
		eend $?

		ebegin " [ SYNC: 2/3] $PKGDIR to $REPO_PATH"
		rssh $PKGDIR/ $REMOTE_TARGET/ >> /var/log/syncpkg.log 2>&1
		eend $?

		ebegin " [ SYNC: 3/3] Cleaning $PKGDIR"
		rm -rf $PKGDIR/* >> /var/log/syncpkg.log 2>&1
		eend $?

		# We sync our copy of .ebuild.x over to the webnode, as it might not be
		# synced as often / at the same time as the buildnode. Thus the 
		# `qpkg --eclean' would remove packages, which *are* in the tree, but
		# the webnode hasn't synced up yet.

		rssh $PORTDIR/.ebuild.x $REMOTE_TARGET/settings/.ebuild.x \
			>> /var/log/syncpkg.log 2>&1

		if $( ssh $REPO_HOST "test -f $REMOTE_ECACHE" ) ; then
			ebegin " [MAINT: 1/3] Removing stale packages in $REPO_PATH"
			ssh $REPO_HOST "CACHE_EBUILD_FILE=$REMOTE_ECACHE qpkg -Eq -P \
				$REPO_BASE/$REPO_PATH/" >> /var/log/syncpkg.log 2>&1
			eend $?
		fi

		# And now remove the .ebuild.x copy again.
		ssh $REPO_HOST "rm -f $REMOTE_ECACHE" >> /var/log/syncpkg.log 2>&1
	
		# The user ssh'ing, needs to be in the portage group on the remote
		# end. Otherwise, things *will* go wrong (like genpkgindex being
		# unable to write to /var/cache/edb/xpak). Also /var/cache/edb/xpak
		# needs to be owned by portage:portage, as well as group-writeable.

		if $( ssh $REPO_HOST "test -w /var/cache/edb/xpak" ) ; then
			ebegin " [MAINT: 2/3] Regenerating $REPO_PATH/Packages"

			# We need to clean out /var/cache/edb/xpak, otherwise it generates
			# stupid shit, like MD5sums not machting, due to old/tbz2's from
			# another run/host
			ssh $REPO_HOST "$REPO_BASE/$REPO_PATH/settings/bin/genpkgindex \
				$REPO_BASE/$REPO_PATH/All" >> /var/log/syncpkg.log 2>&1
			eend $?
		fi

		if ! $( ssh $REPO_HOST "test -L $REPO_BASE/$REPO_PATH/Packages" ) ; then
			ebegin " [MAINT: 3/3] Fixing $REPO_PATH/Packages symlink"
			ssh $REPO_HOST "cd $REPO_BASE/$REPO_PATH; rm -f Packages; ln -s \
				All/Packages" >> /var/log/syncpkg.log 2>&1
			eend $?
		fi
	fi
}

case "$EBUILD_PHASE"  in
	depend) : ;;
	prerm|postrm|clean) : ;;
	setup|unpack|compile) : ;;
	postinst)
		distclean
		syncpkg
	;;
esac

# vim: tw=80
