const fs = require('fs');

module.exports = function(app, checkApiKey) {
    app.get('/action/getusers', checkApiKey, (req, res) => {
        const passwdFilePath = '/etc/apache2/dav_svn.passwd';
        
        try {
            const fileContent = fs.readFileSync(passwdFilePath, 'utf8');
            const users = fileContent
                .split('\n')
                .filter(line => line.trim() !== '')
                .map(line => {
                    const username = line.split(':')[0];
                    return username;
                });

            res.json({
                users: users,
                count: users.length
            });
            
        } catch (error) {
            res.status(500).json({
                error: "Error reading users file",
                details: error.message
            });
        }
    });
};
