
$c->{set_eprint_automatic_fields} = sub
{
	my( $eprint ) = @_;

	my $type = $eprint->get_value( "type" );
	if( $type eq "monograph" || $type eq "thesis" )
	{
		unless( $eprint->is_set( "institution" ) )
		{
 			# This is a handy place to make monographs and thesis default to
			# your insitution
			#
			$eprint->set_value( "institution", "University of Otago" );
		}
	}

	if( $type eq "patent" )
	{
		$eprint->set_value( "ispublished", "pub" );
		# patents are always published!
	}

	if( $type eq "thesis" )
	{
		$eprint->set_value( "ispublished", "unpub" );
		# thesis are always unpublished.
	}


	my @docs = $eprint->get_all_documents();
	my $textstatus = "none";
	## NJS 2008-07-24
	# We want documents with staffonly security level to be invisible to normal
	# users, as they are effectively "internal use only". Visibility with
	# restricted access is now provided by the new embargo security level.
	#
	# The rules for setting text status are:
	#   * "none" if there are no documents at all (count = 0), or if there are
	#      no _visible_ documents (count > 0 but all have security level
	#      staffonly)
	#   * "restricted" if there is at least one document with security level
	#     embargo or user (these are visible but inaccessible to some users)
	#   * "public" otherwise
	#     
	my $numdocs = scalar @docs;
	foreach my $doc ( @docs )
	{
		my $security = $doc->get_value( "security" );
		
		$numdocs-- if ( $security eq "staffonly" );
		
		# Public and staffonly will always exist, but we don't know how many
		# other intermediate values there might be in between (in addition
		# to user and embargo), so we check for not equal to public/staffonly
		# rather than equal to everything else. We can drop out of the loop
		# at this point. We also set $numdocs to zero to ensure that the
		# text status isn't changed to public after the loop.
		#
		# Note we include the eprint status check from the is_public method
		# to ensure behaviour consistent with the original code.
		if ( ( ( $security ne "public" ) && ( $security ne "staffonly" ) ) ||
		     ( $eprint->get_value( "eprint_status" ) ne "archive" ) )
		{
			$textstatus = "restricted";
			$numdocs = 0;
			last;
		}
	}
	$textstatus = "public" if ( $numdocs > 0 );

# Original code follows:
# 	if( scalar @docs > 0 )
# 	{
# 		$textstatus = "public";
# 		foreach my $doc ( @docs )
# 		{
# 			if( !$doc->is_public )
# 			{
# 				$textstatus = "restricted";
# 				last;
# 			}
# 		}
# 	}
## END NJS 2008-07-24
	$eprint->set_value( "full_text_status", $textstatus );


};

