{
    use esmith::AccountsDB;
    my $adb = esmith::AccountsDB->open_ro();
    $OUT = "";
    foreach my $ibay ($adb->ibays)
    {
        my %properties = $ibay->props;
        my $key = $ibay->key;
        if ($properties{'ModDav'})
        {
            if ($properties{'ModDav'} eq 'enabled')
            {
                $OUT .= "\n<Directory /home/e-smith/files/ibays/$key/html>\n\n";
                $OUT .= "    # Enable DAV access for this directory tree\n";
                $OUT .= "    DAV On\n\n";

                if ($properties{'ModDav-FileETag'})
                {
                    $OUT .= "    FileETag ".$properties{'ModDav-FileETag'}."\n\n";
                }
                $OUT .= "    AllowOverride None\n";
                $OUT .= "    Options +Indexes \n\n";
                $OUT .= "    # Allow fancy indexing by columns and download by clicking icon\n";
                $OUT .= "    IndexOptions FancyIndexing IconsAreLinks\n\n";
                if ($properties{'Group'})
                {
                    $OUT .= "    AuthName \"$key\"\n";
                    $OUT .= "    AuthBasicProvider external\n";
                    $OUT .= "    AuthType Basic\n";
                    $OUT .= "    AuthExternal pwauth\n\n";
                    # Save groupname and find it in the group list
                    $iBayGroup = $properties{'Group'};
                    foreach my $group ($adb->groups)
                    {
                        my %groupprops = $group->props;
                        my $grpkey = $group->key;
                        if ($grpkey eq $iBayGroup)
                        {
                            # we have the group that owns the DAV iBay
                            # If there are members of the group validate on them,
                            # otherwise on the ibayname
                            if ($groupprops{'Members'})
                            {
                                # need to break user list on commas then output each one...
                                my @values = split(',',$groupprops{'Members'});
                                $OUT .= "    # Replace ibay name with any valid group member to validate\n";
                                $OUT .= "    Require user ";
                                foreach my $val (@values) {
                                    $OUT .= $val . " ";
                                }
                                $OUT .= "\n\n";
                            }
                            else
                            {
                                # No group members so use ibay name for validation
                                $OUT .= "    # use ibay name to validate\n";
                                $OUT .= "    Require user " . $key . "\n\n";
                            }
                        }
                    } 
                }
                # Ensure only valid users get to do stuff...
                $OUT .= "    <Limit GET PUT POST DELETE PROPFIND PROPPATCH MKCOL COPY MOVE LOCK UNLOCK>\n\n";
                $OUT .= "        Allow from all\n";
                $OUT .= "        Require valid-user\n\n";
                $OUT .= "    </Limit>\n\n";
                $OUT .= "</Directory>\n";
            }
        }
    }
}
