#ident	"@(#)rfs.admin:resrcmgmt/advmgmt/advnow	1.11"
#menu# advertise local resources immediately (if RFS is running)

#help# 	
#help#	You can share your local resources with other machines
#help#	by offering (advertising) them.  You advertise directories
#help#	containing files, subdirectories, devices, and /or 
#help#	named pipes.
#help#
#help#	The subcommand "advnow" allows you to immediately advertise 
#help#	local resources to other machines.    You may only use this 
#help#	subcommand when RFS is running on your machine.
#help#
#help#	NOTE: This subcommand does NOT put resources on the Automatic 
#help#	Advertise List.  Use the subcommand "advauto" to add resources
#help#	to this list.

#set trap
trap '/bin/rm -f /usr/tmp/advnow$$; trap 0; exit' 0 1 2 3 15

# Print heading
echo '\n\nIf you are not sure how to answer any prompt, type "?" for HELP.'
echo 'If you want to quit the subcommand at any time, type "q".'
echo "\n\n              ADVERTISE LOCAL RESOURCES IMMEDIATELY"

# define messages and questions
QUEST2="
RFS is NOT running on your machine. 
You cannot immediately advertise local resources."

QUEST3="
Enter the full pathname of the local directory to
advertise [?, q]:"

QUEST4="
Enter the resource identifier [?, q]:"

QUEST5="
Select 
    r) if the resource is to be advertised as read-only.
    w) if the resource is to be advertised as read-write.
[r, w, ?, q (default w)]: "

QUEST6="
Enter a 0 to 32 character description of the resource
	start|********************************|end
      [?, q]:"

QUEST7="
Enter the name of a client machine [?, q]:"

QUEST8="
Enter the name of another client machine [?, q]:"

DEF_PATH="
Enter the full pathname of the directory you want to advertise 
starting with \"/\".  All files and subdirectories below the 
directory you specify will be included in the advertisement and 
will be available to client machines."

DEF_SYM_RES="
The resource identifier is a short, descriptive, name by which 
administrators of client machines will know and access your resource.
The resource identifier you choose must be unique in your domain.
Resource identifiers must be 1 to 14 printable characters."

DEF_PERM="
If you advertise this resource as read-only, client machines may only
mount it as read-only.  No users on the client machines may make any 
changes to your resource.

If you advertise this resource as read-write, client machines may choose 
to mount it as read-write or as read-only.  When client machines mount
the resource as read-write, the user and group ID mappings that you have 
defined will govern which remote users are able to make changes to it."

DEF_CLIENTS="
Unrestricted access means that all machines will be allowed to mount and
access this resource that you are advertising.  To choose the option of
allowing all machines to use the resource you enter <RETURN> to this 
client machine prompt.

If you only want a specific set of machines to be allowed to use your
resource you set up a client list that contains the names of the machines
that are allowed to use it.  To do this, enter one client machine name at
each prompt.  When you are done putting machines in the list, you enter
<RETURN> at the prompt to signal that your client list is complete.

You may have different client lists for each resource you advertise."

DEF_DESC="
This field allows you to give a short description of the resourse,
i.e., \"laser printer 1\", or \"the /usr directory\".
Do not include double quotes in your description."

VQ="
Is this information correct?"

REDOQ="
Do you want to advertise another resource?"

INTRO="
You share your local resources with other machines by advertising them.  
The machines that can use the resources are called clients.

This subcommand lets you immediately advertise resources.  As soon
as a resource is advertised it is available to client machines.
You will be asked for all the information needed.

NOTE:  This subcommand does NOT add resources to the Automatic
Advertise List.  Use the subcommand \"advauto\" to add resources
to this list."

# Print description
echo "$INTRO\n"

#initialiation
cflags="-qq -k$$"

# Determine if RFS is running.  If not, then print appropriate error message
if  /usr/lbin/rfsrunning
then
	: OK !!!
else
	echo "\nRFS is NOT running on your machine. "
	echo "You can not immediately advertise local resources.\n"
	exit
fi

while /bin/true
do
	# Get resource's full path-name.  If the path does not exist, print message
	# and ask user to re-enter
	path=`/usr/lbin/checkre $cflags -fe				\
	-H"$DEF_PATH" \
	"$QUEST3"						\
	'^[^ ]*$' 'The pathname must not contain a space.'		\
	'^/.*$' 'You must specify a full pathname starting with /.'`

	#remove "." special meaning (just in case it is used)
	path2=`echo $path | /bin/sed 's/\./\\\./g'`

	if [ ! -d $path ]
	then
		echo "$path is not a directory in your file system.\n"
		continue
	else
		/usr/bin/adv | /bin/grep " $path2 " >/dev/null 2>&1
		if [ "$?" = "0" ]
		then
			echo "$path is already advertised."
			continue
		fi
	fi

	while /bin/true
	do
		# Get the resource's symbolic name
		res=`/usr/lbin/checkre $cflags -fe				\
		-H"$DEF_SYM_RES" \
		"$QUEST4"						\
		'^.\{1,14\}$' 'The resource identifier must be 1 to 14 printable characters.' \
		'^[^ \.\/:]*$'  'The resource identifier must not contain a space, ":", "." or a "/".'`

		/usr/bin/nsquery -h 2>/dev/null | /bin/grep "^$res " >/dev/null 2>&1
		if [ "$?" = "0" ]
		then
			echo "\n\"$res\" is already being used as a resource identifier."
			continue
		fi
		break
	done

	# Get the permissions of the resource
	rw=`/usr/lbin/checkre $cflags -fe	-D "w"				\
	-H"$DEF_PERM" \
	"$QUEST5"						\
	'^[rw]$' 'You must type r, w, or RETURN' `

	if [ "$rw" = "w" ]
	then
		rw=
		perm="read-write"
	else
		rw="-r"
		perm="read-only"
	fi

	# Get the description of the resource
	desc=`/usr/lbin/checkre $cflags -fe				\
	-H "$DEF_DESC"						\
	"$QUEST6"						\
	'^[^\"]*$'  'The description must not contain double quotes.'	\
	'^.\{0,32\}$' 'The description must be 0 to 32 printable characters.'`

	# Get the client list of the resource
        echo "\nA remote machine that uses your resource is called a CLIENT MACHINE."
	echo "You may allow unrestricted access to \"$res\", which means that ALL"
	echo "machines may access it, or, you can allow only certain machines to access"
	echo "\"$res\" by putting those machines on a CLIENT LIST.\n"
	echo "To allow ALL machines to access \"$res\" enter <RETURN> at this "
	echo "client machine prompt.  To limit access to a list of client "
	echo "machines, enter one client machine name each time you are prompted.  "
	echo "When you have no more machines to put on the client machine list, "
	echo "enter <RETURN> at the prompt.\n"


	clist=
	quest="$QUEST7"
	while /bin/true
	do
		client=`/usr/lbin/checkre $cflags -fe  \
		-H"$DEF_CLIENTS" \
		"$quest"						\
		'^[^\.\/ :]*$'  'The  machine name must not contain a ".", space, ":", or "/".' \
		'^.\{0,8\}$' 'The machine name must be 1 to 8 printable characters.'`

		if [ "$client" = "" ]
		then
			break
		else
			clist="$client $clist"
			quest="$QUEST8"
		fi
	done

	# Verify information.  If incorrect then re-collect information
	echo "\nDirectory pathname: $path"
	echo "Resource identifier: $res"
	echo "Client access permissions: $perm"
	echo "Description: $desc"
	echo "Client list: \c"
	if [ "$clist" = "" ]
	then
		echo "*UNRESTRICTED*"
	else
		echo $clist
	fi

	if /usr/lbin/checkyn $cflags -f "$VQ"
	then
		: OK!
	else
		echo "\n\"$res\" will NOT be advertised."
		continue
	fi

	# Advertise the resource and report whether successful or failure.
	/usr/bin/adv $rw -d "$desc" $res $path $clist 2>/usr/tmp/advnow$$
	if [ "$?" != "0" ]
	then
		echo "\nWARNING: \c"
		/bin/sed "s/^.*adv://" /usr/tmp/advnow$$
		continue;
	else
		echo "\nResource \"$res\" is advertised and is available "
		echo "to client machines.\n"
	fi


	# ask the user whether to rpeat the cycle
	if /usr/lbin/checkyn $cflags -f "$REDOQ"
	then
		continue
	else
		break
	fi
done
