#------------------------------------------------------------
# Information bay directories
#------------------------------------------------------------

{
    use esmith::AccountsDB;
    my $adb = esmith::AccountsDB->open_ro();
    $OUT = "";
    foreach my $ibay ($adb->ibays)
    {
	my %properties = $ibay->props;
	my $key = $ibay->key;
	#------------------------------------------------------------
	# Figure out which combination of parameters to use. If
	# PublicAccess parameter is present, this is e-smith 4.0.
	# Otherwise, it's e-smith 3.0.
	#------------------------------------------------------------

	my $allow;
	my $pass;
	my $satisfy;
	
	if ($properties{'PublicAccess'})
	{
	    if ($properties{'PublicAccess'} eq 'none')
	    {
		next;
	    }
	    elsif ($properties{'PublicAccess'} eq 'local')
	    {
		$allow   = $localAccess;
		$pass    = 0;
		$satisfy = 'all';
	    }
	    elsif ($properties{'PublicAccess'} eq 'local-pw')
	    {
		$allow   = $localAccess;
		$pass    = 1;
		$satisfy = 'all';
	    }
	    elsif ($properties{'PublicAccess'} eq 'global')
	    {
		$allow   = 'all';
		$pass    = 0;
		$satisfy = 'all';
	    }
	    elsif ($properties{'PublicAccess'} eq 'global-pw')
	    {
		$allow   = 'all';
		$pass    = 1;
		$satisfy = 'all';
	    }
	    elsif ($properties{'PublicAccess'} eq 'global-pw-remote')
	    {
		$allow   = $localAccess;
		$pass    = 1;
		$satisfy = 'any';
	    }
	}
	elsif ($properties {'ReadAccess'} eq 'global')
	{
	    if ($properties {'UsePassword'} eq 'yes')
	    {
		$allow   = 'all';
		$pass    = 1;
		$satisfy = 'all';
	    }
	    else
	    {
		$allow   = 'all';
		$pass    = 0;
		$satisfy = 'all';
	    }
	}
	else
	{
	    if ($properties {'UsePassword'} eq 'yes')
	    {
		$allow   = $localAccess;
		$pass    = 1;
		$satisfy = 'all';
	    }
	    else
	    {
		$allow   = $localAccess;
		$pass    = 0;
		$satisfy = 'all';
	    }
	}


	my $dynamicContent = $properties{'CgiBin'} || "disabled";
	$OUT .= "\n";
	$OUT .= "#------------------------------------------------------------\n";
	$OUT .= "# $key ibay directories ($properties{'Name'})\n";
	$OUT .= "#------------------------------------------------------------\n";

	$OUT .= "\n";
	$OUT .= "<Directory /home/e-smith/files/ibays/$key/html>\n";
	$OUT .= "    Options Indexes\n";
	if ($dynamicContent eq 'enabled')
	{
	    $OUT .= "    Options +Includes\n";
	}
	else
	{
	    $OUT .= "    <FilesMatch \"\\.(php|php3|phtml)\$\">\n";
	    $OUT .= "        order deny,allow\n";
	    $OUT .= "        Deny from all\n";
	    $OUT .= "    </FilesMatch>\n";
	    $OUT .= "    Options +IncludesNOEXEC\n";
	}
	$OUT .= "    AllowOverride None\n";
	$OUT .= "    order deny,allow\n";
	$OUT .= "    deny from all\n";
	$OUT .= "    allow from $allow\n";
	if ($pass)
	{
	    $OUT .= "    AuthName \"$properties{'Name'}\"\n";
	    $OUT .= "    AuthType Basic\n";
	    $OUT .= "    AuthExternal pwauth\n";
	    $OUT .= "    require user $key\n";
	    $OUT .= "    Satisfy $satisfy\n";
	}
	$OUT .= "</Directory>\n";

	$OUT .= "\n";
	$OUT .= "<Directory /home/e-smith/files/ibays/$key/cgi-bin>\n";
	if ($dynamicContent eq 'enabled')
	{
	    $OUT .= "    Options ExecCGI\n";
	}
	$OUT .= "    AllowOverride None\n";
	$OUT .= "    order deny,allow\n";
	$OUT .= "    deny from all\n";
	$OUT .= "    allow from $allow\n";
	if ($pass)
	{
	    $OUT .= "    AuthName \"$properties{'Name'}\"\n";
	    $OUT .= "    AuthType Basic\n";
	    $OUT .= "    AuthExternal pwauth\n";
	    $OUT .= "    require user $key\n";
	    $OUT .= "    Satisfy $satisfy\n";
	}
	$OUT .= "</Directory>\n";

	$OUT .= "\n";
	$OUT .= "<Directory /home/e-smith/files/ibays/$key/files>\n";
	$OUT .= "    AllowOverride None\n";
	$OUT .= "    order deny,allow\n";
	$OUT .= "    deny from all\n";
	$OUT .= "    allow from $allow\n";
	if ($pass)
	{
	    $OUT .= "    AuthName \"$properties{'Name'}\"\n";
	    $OUT .= "    AuthType Basic\n";
	    $OUT .= "    AuthExternal pwauth\n";
	    $OUT .= "    require user $key\n";
	    $OUT .= "    Satisfy $satisfy\n";
	}
	$OUT .= "</Directory>\n";
    }
}
