#!/bin/rc

# getreg REGNAME
# finds and mounts the named registry based on reg info cached in /tmp/grid
# if the registry is not found there, calls huntservice REGNAME to query mounted registries

# set variable from arguments or prompt user 
switch($1){
case ''
	echo Enter a registry name name
	REGNAME=`{read}
	echo use getreg REGNAME to skip this prompt
case *
	REGNAME=$1
}

rfork ns
ATTACH=null
# clear old work files if present
if (test -e /tmp/grid/work/svcfind) rm /tmp/grid/work/svcfind
if (test -e /tmp/grid/work/svctarget) rm /tmp/grid/work/svctarget

echo `{date} getreg $REGNAME >>/tmp/grid/log/log

# find all lines with info matching the requested service 
# then pick the most recently added 

grep -i $REGNAME /tmp/grid/info/regbucket >/tmp/grid/work/svcfind
SVCINFO=`{tail -1l /tmp/grid/work/svcfind}

# if we didnt find any SVCINFO from grepping the regbucket
# call huntservice to query currently mounted registries

if (! ~ $SVCINFO ?*) {
	echo registry not found in index, try running regupdate and regrepair if needed. exiting
	exit
}

echo found registry info $SVCINFO
echo $SVCINFO >/tmp/grid/work/svctarget
DIALSTRING=`{awk '{print $1}' /tmp/grid/work/svctarget}
echo attempting mount at /g/r/$REGNAME/r using $DIALSTRING as dialstring
if (recover $DIALSTRING $REGNAME) ATTACH=success
if (! ~ $ATTACH success){
	echo registry mount failed
	exit
}
if (~ $ATTACH success){
	g/local mntgen /g/r/$REGNAME
	g/local mount /srv/$REGNAME /g/r/$REGNAME/r
	echo adding registry info to /tmp/grid
	echo `{date} gridregadd $DIALSTRING $REGNAME >>/tmp/grid/log/log
#	echo $REGNAME >/tmp/grid/registries/$DIALSTRING
	echo $DIALSTRING > /tmp/grid/registries/$REGNAME
	echo $REGNAME $DIALSTRING >> /tmp/grid/info/reglist
	echo harvesting information
	sleep 1
	cat /g/r/$REGNAME/r/index > /tmp/grid/info/reg$REGNAME
	cat /tmp/grid/info/reg$REGNAME >>/tmp/grid/info/regbucket
	echo registry mounted at /g/r/$REGNAME/r
	echo registry contents:
	cat /tmp/grid/info/reg$REGNAME
	exit
}

echo bad dialstring guess, trying to find a better one
PORT=`{echo $DIALSTRING |awk -F! '{print $3}'}

# look at how many strings there are in the svctarget info and count through them
# look for a string that seems like it could be used in a dialstring and try dialing it

for (i in `{seq 2  `{cat /tmp/grid/work/svctarget |wc -w}}){
	IPADDR=`{awk '{print $'$i'}' /tmp/grid/work/svctarget |sed s/at//}
	if (! ~ $IPADDR ?*) IPADDR=null
	TESTADDR=`{echo $IPADDR |awk -F. '{print $2}'}
	DIALSTRING=tcp!$IPADDR!$PORT
	if (~ $TESTADDR ?*){
		echo attempting mount at /g/r/$REGNAME/r using $DIALSTRING as dialstring
		if (recover $DIALSTRING $REGNAME) ATTACH=success
		if (~ $ATTACH success){
			g/local mntgen /g/r/$REGNAME
			g/local mount /srv/$REGNAME /g/r/$REGNAME/r
			echo adding registry info to /tmp/grid
			echo `{date} gridregadd $DIALSTRING $REGNAME >>/tmp/grid/log/log
#			echo $REGNAME >/tmp/grid/registries/$DIALSTRING
			echo $DIALSTRING > /tmp/grid/registries/$REGNAME
			echo $REGNAME $DIALSTRING >> /tmp/grid/info/reglist
			echo harvesting information
			sleep 1
			cat /g/r/$REGNAME/r/index > /tmp/grid/info/reg$REGNAME
			cat /tmp/grid/info/reg$REGNAME >>/tmp/grid/info/regbucket
			echo registry mounted at /g/r/$REGNAME/r
			echo registry contents:
			cat /tmp/grid/info/reg$REGNAME
		}
	}
	echo dialstring still failed, looking for more possibilities
}

echo failed to deduce correct dialsting. try manually mounting based on
echo $SVCINFO


# 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
