#!/bin/rc

# sharefiles SVCNAME INFORMATION PORT EXPORTDIR
# start a directory export service and create info about it in your session files

# set variables from args or prompt user if missing
switch($1){
case ''
	echo one word descriptive name of this fileshare export service?
	SVCNAME=`{read}
	echo description of this service? (include ip address if you didnt provide a routable one for your session)
	LOC=`{read}
	echo port to provide this export on?
	PORT=`{read}
	echo directory to export? (remember to check your permissions)
	DIR=`{read}
	echo use g/sharefiles SERVICENAME LOCATION PORT EXPORTDIR to skip these prompts
case *
	switch($4){
	case ''
		echo usage gridsharefiles SVCNAME DESCRIPTION PORT EXPORTDIR
		echo or start with no arguments to be prompted
		exit
	case *
		SVCNAME=$1
		LOC=$2
		PORT=$3
		DIR=$4
	}
}

if (test -d $DIR) POSSIBLE=yes
if (! ~ $POSSIBLE yes) {
	echo cant find $DIR to export
	exit
}

rfork ns
echo writing service info to /tmp/grid
echo $SVCNAME:$LOC:$PORT:$DIR >> /tmp/grid/info/exports
echo $LOC:$PORT:$DIR > /tmp/grid/svcout/$SVCNAME
echo starting listener on port $PORT using exportfs to share $DIR
aux/listen1 -tv tcp!*!$PORT /bin/exportfs -R -r $DIR >/tmp/grid/log/exp$SVCNAME &
echo `{date} sharefiles $SVCNAME $LOC $PORT $DIR >>/tmp/grid/log/log
echo listener started, log at /tmp/grid/log/exp$SVCNAME
echo dont forget to announce your service with tellreg $SVCNAME REGISTRY


# 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
