const mix = require('laravel-mix');
const { exec } = require('child_process');

const resIntegrityFiles = [
    { "envKey": "HUMANS_TXT", "path": "public/humans.txt" },
    { "envKey": "ROBOTS_TXT", "path": "public/robots.txt" },
    { "envKey": "COPYRIGHT_HTML", "path": "public/copyright.html" },
    { "envKey": "COPYRIGHT_MD", "path": "public/copyright.md" },
    { "envKey": "RSS_FEED", "path": "public/rss.xml" },
    { "envKey": "ATOM_FEED", "path": "public/feed.atom" },
    { "envKey": "FAVICON_ICO", "path": "public/favicon.ico" },
    { "envKey": "FAVICON_SVG", "path": "public/favicon.svg" },
    { "envKey": "FAVICON_PNG", "path": "public/favicon.png" },
    { "envKey": "NUNITO_REGULAR_WOFF2_FONT", "path": "public/fonts/Nunito/Nunito-Regular.woff2" },
    { "envKey": "POIRETONE_REGULAR_WOFF2_FONT", "path": "public/fonts/PoiretOne/PoiretOne-Regular.woff2" },
    { "envKey": "WEBMANIFEST_JSON", "path": "public/site.webmanifest" },
    { "envKey": "MIX_MANIFEST_JSON", "path": "public/mix-manifest.json" },
    { "envKey": "APP_CSS", "path": "public/css/app.css" },
    { "envKey": "APP_JS", "path": "public/js/app.js" },
];

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel applications. By default, we are compiling the CSS
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/js')
    .vue()
    .sourceMaps()
    .postCss('resources/css/app.css', 'public/css', [
        require('postcss-import'),
        require('tailwindcss'),
    ])
    .sourceMaps()
    .alias({
        '@': 'resources/js',
    }).after(stats => {
        resIntegrityFiles.forEach(file => {
            exec(`cat ${file.path} | openssl dgst -sha512 -binary | openssl base64 -A`, (shaError, shaStdout, shaStderr) => {
                if (shaError) {
                    console.error(`error getting SHA hash for ${file.path}: ${shaError.message}`);
                    return;
                }
                if (shaStderr) {
                    console.error(`error getting SHA hash for ${file.path}: ${shaStderr}`);
                    return;
                }
                let sha512Hash = shaStdout;
                let sedCmd = `sed -i '/^INTEGRITY_HASH_${file.envKey}="*/c\INTEGRITY_HASH_${file.envKey}="sha512-${sha512Hash}"' .env`;

                exec(sedCmd, (sedError, sedStdout, sedStderr) => {
                    if (sedError) {
                        console.error(`error executing sed command for ${file.envKey}: ${sedError.message}`);
                        return;
                    }
                    if (sedStderr) {
                        console.error(`error executing sed command for ${file.envKey}`);
                        return;
                    }
                    console.log(`Replaced SHA512 hash for ${file.envKey}.`);
                });
            });
        });
    });

if (mix.inProduction()) {
    mix.version();
}
