#!/bin/bash
#< Send a command to all child zones

AWK="/usr/bin/awk"
BASENAME="/usr/bin/basename"
ECHO="/usr/bin/echo"
TPUT="/usr/bin/tput"
ZLOGIN="/usr/sbin/zlogin"
ZONEADM="/usr/sbin/zoneadm"

TPUT_RMSO=$( ${TPUT} rmso )
TPUT_SMSO=$( ${TPUT} smso )
THIS_PROG=$( ${BASENAME} $0 )

function print_error {
	${ECHO} "Error: $@" >&2
}

function print_usage {
	{
		${ECHO} "Usage: ${THIS_PROG} 'command'"
	} >&2	
}

function get_zone_list {
	ZONES=( $( ${ZONEADM} list -cp | ${AWK} 'BEGIN { FS=":" } $3 == "running" && $2 != "global" { print $2 }' ) )
	if [ -z "${ZONES[0]}" ]; then
		print_error "No zones present"
		exit 1
	fi
}

function send_command {
	for ZONE in ${ZONES[@]}; do
		${ECHO} "${TPUT_SMSO}${ZONE}${TPUT_RMSO}"
		${ZLOGIN} ${ZONE} ${COMMAND}
	done
}

if [ "$#" -ne "1" ]; then
	print_usage
	exit 1
fi
if [ "$1" = "-h" ]; then
	print_usage
	exit 0
fi
COMMAND=$1

get_zone_list
send_command

exit 0