#!/bin/rc

# tellreg SVCDESCRIPTION REGNAME
# announces a previously created service to a previously mounted registry

# set variables to args or prompt user if missing
switch($1){
case ''
	echo enter the saved name/description of a currently running service
	SVCNAME=`{read}
	echo enter the name of a mounted dynamic registry
	REGNAME=`{read}
	echo use gridannounce SVCNAME REGNAME to skip these prompts
case *
	SVCNAME=$1
	switch($2){
	case ''
		echo dont forget to pick a registry to announce to
		REGNAME=`{read}
	case *
		REGNAME=$2
	}
}

# make sure the service and registry exist in /tmp/grid
SERVICEINFO=`{cat /tmp/grid/svcout/$SVCNAME}
REGINFO=`{cat /tmp/grid/registries/$REGNAME}
if (! ~ $SERVICEINFO ?*){
	echo no service found, exiting
	exit
}
if (! ~ $REGINFO ?*){
	echo no registry found, exiting
	exit
}

rfork ns
NODEADDR=`{cat /tmp/grid/info/nodeaddr}
LOC=`{awk -F: '{print $1}' /tmp/grid/svcout/$SVCNAME}
PORT=`{awk -F: '{print $2}' /tmp/grid/svcout/$SVCNAME}
DIR=`{awk -F: '{print $3}' /tmp/grid/svcout/$SVCNAME}
MYDIALSTRING=tcp!$NODEADDR!$PORT
echo announcing to registry
>/g/r/$REGNAME/r/new {echo $MYDIALSTRING $SVCNAME $LOC; while (sleep 3600);}&
echo `{date} tellreg $SVCNAME $REGNAME >>/tmp/grid/log/log


# this script part of the /g/toolkit licensed under the MIT license but be cool
# keep the code open, share your scripts and config files
