#!/bin/bash
#                      /usr/local/bin/my_twin
# http://crystalfaeries.net/posix/bin/my_twin
# celeste:crystalfaery 2017-08-10 00:27:39+00:00
# my_twin is a helper for commands like rsync,
# to determine the source or destination of the rsync,
# from me to my twin, or vice versa.
# This facilitates a single script working between multiple hosts, e.g.:
# rsync -auvzH source_specification `my_twin`:/destination
# If called with the argument "-u" then the hostname only is returned, rather than user@hostname.
let hostname_only=1		# false by default
if [[ $# -ne 0 ]]
then				# we have an argument
    case "${1}" in
-u)
	let hostname_only=0	# true by argument
	shift			# consume the argument
	;;
*)
	head -n 10 ${0} 1>&2
	exit 0
	;;
    esac
fi

case `hostname` in
fey)
	if   $(ping -q -c 1 box6537.bluehost.com > /dev/null)
	then	# we twin with box6537
		if [[ $hostname_only -eq 0 ]]
		then
			echo	"box6537.bluehost.com"
		else
			echo	"divservi@box6537.bluehost.com"
		fi
	else	# we have no twin currently online so talk to self
		if [[ $hostname_only -eq 0 ]]
		then
			echo	"localhost"
		else
			echo	"$(whoami)@localhost"
		fi
	fi
	exit 0
	;;
*)
	if [[ $hostname_only -eq 0 ]]
	then
		echo	"localhost"
	else
		echo	"$(whoami)@localhost"
	fi
	echo "$0 does not cognize host `hostname`" 1>&2
	exit -3
	;;
esac


syntax highlighted by Code2HTML, v. 0.9.1