#!/bin/bash
# Script: svn2host
# Script that creates a dist tar (via svn2tar), copies it to the specified hosts(s), builds it there and installs it there. Basically a very very simple continuous integration.
# Author: Schlomo Schapiro
# License: see COPYING file
# Last Update: $Id: svn2host 736 2011-11-22 09:21:35Z gdha $

##################### M A I N ####################

case "$*" in
	--help|"")
		echo "Usage: $0 <host> ..."
		exit 1
esac


echo "
******************************************
**	svn2host script for rear      	**
******************************************
" >&2

# find the full path to this script
ME="$(cd $(dirname "$0") ; pwd)/$(basename "$0")"
# Find the svnroot path for the initial tar-ball
rear_root="${ME%/usr/share/rear/contrib/svn2host}"
### echo The root of rear SVN directory is $rear_root

# building dist tar
distarchive=$($rear_root/usr/share/rear/contrib/svn2tar)

RESULTS=()
FAILED=()

for h in "$@" ; do

	# check remote host access
	if ! err=$(ssh $h /bin/true 2>&1) ; then
		echo "ERROR Cannot access host '$h' via SSH:"
		echo "$err"
		FAILED=( "${FAILED[@]}" "$h -> Could not access via SSH: $err")
		continue
	else
		echo "BUILDING ON $h"
	fi

	# copy dist archive to host
	if ! err=$(scp "$distarchive" $h:$distarchive 2>&1) ; then
		echo "ERROR Could not scp rear dist archive to '$h':"
		echo "$err"
		FAILED=( "${FAILED[@]}" "$h -> Could not scp dist archive: $err")
		continue
	fi

	if ssh $h type -p rpmbuild >/dev/null ; then
	# this is an RPM type host, build it

		# call rpmbuild
		RPMFILE=
		while read -r ; do
			echo "$h: $REPLY"
			case "$REPLY" in
				*noarch.rpm*) RPMFILE="${REPLY##* }" # assume that RPM file name is last word on this line!!!
			esac
		done < <(
			ssh $h rpmbuild -tb $distarchive 2>&1
			)
		# verify existance of built RPM
		if ! ssh $h test -s "$RPMFILE" ; then
			echo "ERROR Could not build RPM on '$h'"
			FAILED=( "${FAILED[@]}" "$h -> Could not build RPM")
			continue
		fi

		# install RPM
		if ! ssh $h rpm -U --force --nodeps "$RPMFILE" ; then
			echo "ERROR Could not install RPM on '$h'"
			FAILED=( "${FAILED[@]}" "$h -> Could not install RPM")
			continue
		else
			echo "INSTALLED RPM '$RPMFILE' on '$h'"
			RESULTS=( "${RESULTS[@]}" "$h -> $RPMFILE" )
		fi

	elif ssh $h type -p dpkg >/dev/null ; then
	# this is an DEB type host, just throw ReaR on top
	# TODO: built proper DEB
		distdir=$(basename $distarchive)
		distdir=${distdir%.tar.gz}
		if ! ssh $h -- rm -Rf $distdir ; then
			FAILED=( "${FAILED[@]}" "$h -> Could not remove $distdir")
			continue
		fi
		if ! ssh $h -- tar -xzf $distarchive ; then
			FAILED=( "${FAILED[@]}" "$h -> Could not unpack $distarchive")
			continue
		fi
		if ! ssh $h -- rm -Rf /usr/sbin/rear /usr/share/rear ; then
			FAILED=( "${FAILED[@]}" "$h -> Could not remove /usr/sbin/rear /usr/share/rear")
			continue
		fi
		if ! ssh $h -- tar -C $distdir -c usr \| tar -C / -x --totals ; then
			FAILED=( "${FAILED[@]}" "$h -> Could not copy $distdir/usr to / with tar")
			continue
		else
			echo "COPIED FILES TO '$h'"
			RESULTS=( "${RESULTS[@]}" "$h -> manual copy" )
		fi


	else
		echo "ERROR Unsupported host type '$h'. Maybe you need to install rpmbuild?"
		FAILED=( "${FAILED[@]}" "$h -> Could not determine host type. Maybe you need to install rpmbuild?")
		continue
	fi

done

if test "$RESULTS" ; then
	echo -e "\n\n * * *   I N S T A L L E D  H O S T S   * * *\n"
	for l in "${RESULTS[@]}" ; do echo "$l" ; done
fi

if test "$FAILED" ; then
	echo -e "\n\n * * *   F A I L E D  H O S T S   * * *\n"
	for l in "${FAILED[@]}" ; do echo "$l" ; done
fi
