const fs = require('fs');
const apacheMd5 = require('apache-md5');

module.exports = function(app, checkApiKey) {
    app.post('/action/newuser', checkApiKey, (req, res) => {
        const { username, password } = req.body;
        if (!username || !password) {
            return res.status(400).send('Username and password are required');
        }
        const passwdFilePath = '/etc/apache2/dav_svn.passwd';
        const hashedPassword = apacheMd5(password);  // Hacher le mot de passe au format APR1-MD5
        const userLine = `${username}:${hashedPassword}`;
        fs.appendFile(passwdFilePath, `${userLine}\n`, (err) => {
            if (err) {
                return res.status(500).send(`Error updating passwd file: ${err}`);
            }
            res.send(`User ${username} created successfully`);
        });
    });
};
