#!/bin/rc

# gridstart SESSIONNAME EXTERNALIP
# prepares for grid connection by setting up dir for tmp information
# and binding it to /tmp/grid

# set variables from args or prompt user if missing
switch($1){
case ''
	echo Pick a name for this grid session
	GRIDSESS=`{read}
	echo Enter your external IP or a routable domain name
	NODEADDR=`{read}
	echo use gridstart SESSIONNAME EXTERNALIP to skip this prompt
case *
	GRIDSESS=$1
	switch($2){
	case ''
		echo remember to choose an external IP or routable domain name
		NODEADDR=`{read}
	case *
		NODEADDR=$2
	}
}

rfork ns
echo Making temporary session folder at /tmp/gridfiles/$GRIDSESS
if (! test -d /tmp/gridfiles) mkdir /tmp/gridfiles
if (test -e /tmp/gridfiles/$GRIDSESS) {
	echo old $GRIDSESS exists, removing
	rm -rf /tmp/gridfiles/$GRIDSESS
}
mkdir /tmp/gridfiles/$GRIDSESS
if (! test -d /tmp/grid) mkdir /tmp/grid
echo Binding session folder to /tmp/grid
bind -c /tmp/gridfiles/$GRIDSESS /tmp/grid
echo making /tmp/grid subdirectories
mkdir /tmp/grid/registries
mkdir /tmp/grid/miareg
mkdir /tmp/grid/dedreg
mkdir /tmp/grid/svcout
mkdir /tmp/grid/svcin
mkdir /tmp/grid/miasvc
mkdir /tmp/grid/dedsvc
mkdir /tmp/grid/info
mkdir /tmp/grid/log
mkdir /tmp/grid/work
mkdir /tmp/grid/g
echo $GRIDSESS > /tmp/grid/info/gridsession
echo writing external IP/name to /tmp/grid/info/nodeaddr
echo $NODEADDR >/tmp/grid/info/nodeaddr
echo starting log at /tmp/grid/log/log
echo `{date} gridstart $GRIDSESS $NODEADDR >/tmp/grid/log/log

echo starting mntgen for /, /g, and /g/r
aux/stub -d /g
bind /tmp/grid/g /g
mntgen /g
mntgen /g/r
bind -c /tmp/grid /g/grid
echo starting plumber for shared namespace
srvfs g$GRIDSESS /g
gplumber
ns |grep plumb |sed 's/#s/\/srv/' >/tmp/grid/info/plumber
rfork ns
echo grid session begun, use g/gridattach $GRIDSESS to connect to it

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