#!/bin/rc

# dialsvc DIALSTRING SVCNAME
# dials a service and mounts it and writes information to relevant /tmp/grid areas

# set variables from args or prompt user if missing
switch($1){
case ''
	echo Enter an IP address for this service
	SVCIP=`{read}
	echo Enter service service port
	SVCPORT=`{read}
	echo choose a name for this service
	SVCNAME=`{read}
	SVCADDR=tcp!$SVCIP!$SVCPORT
	echo use dialsvc tcp!IPADDRESS!PORT SVCNAME to skip these prompts
case *
	SVCADDR=$1
	switch($2){
	case ''
		SVCNAME=`{echo $ADDRESS |awk -F! '{print (NF>1) ? $2:$1}'}
	case *
		SVCNAME=$2
	}
}

rfork ns
ATTACH=null
echo attaching service
if (recover $SVCADDR $SVCNAME) ATTACH=success
if (! ~ $ATTACH success) {
	echo service mount failed
	exit
}

g/local mntgen /g/$SVCNAME
sleep 1
g/local mount /srv/$SVCNAME /g/$SVCNAME/s
sleep 1
echo adding service info to /tmp/grid
g/svcadd $SVCNAME $SVCADDR
echo `{date} dialsvc $SVCADDR $SVCNAME >>/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
