<?php

$mode = 'production';

if ($argv[1] == 'dev') {
    $mode = 'dev';
}


$currendDir = getcwd();

$base_root = $currendDir . '/../../www/';
$tpl_root =  $currendDir . '/../../engine/www/tpls/include/';

require_once($base_root. '../vendor/minificator/Utils.php');
require_once($base_root. '../vendor/minificator/Colors.php');
require_once($base_root. '../vendor/minificator/Minifier.php');
require_once($base_root. '../vendor/minificator/JSmin.php');

$css_template_file = $tpl_root . '_static_css_headers.htm';
$js_template_file  = $tpl_root . '_static_js_headers.htm';

$static_root = '/';

$css_files = [
    'bootstrap.css',
    'custom.css',
    'custom_xxs.css',
    'custom_sm.css',
    'custom_md.css',
    'custom_lg.css',
    'custom_xl.css',
    'custom_xxl.css',
    'animate.css',
    'jquery.fancybox.min.css',
    'jquery.scrollbar.css',
    'slick.css',
    'slick-theme.css',
    'slide-menu.css',
    'fonts_full.css',
    'mmenu.css'
];

$js_files = [
    'jquery-3.4.1.min.js',
    'bootstrap.bundle.min.js',
    'slick.js',
    'jquery.fancybox.min.js',
    'slide-menu.js',
    'lazy.js',
    'autocomplete.js',
    'jquery.scrollbar.min.js',
    'jquery.mask.js',
    'jquery.ajaxform.js',
    'core-engine.js',
    'mmenu/mmenu.js'
];



$ts = time();

$out_css = '';
$out_js = '';
switch($mode) {
    case 'production':
        // Минифицируем статику
        $cssmin = new Minifier();
        $input_css = "";
        foreach($css_files as $filename) {
            $input_css .= file_get_contents('css/'.$filename);
        }
        $output_css = $cssmin->run($input_css);
        file_put_contents('css/_pack.css', $output_css);

        $out_css .= '<link rel="preload" href="'.$static_root.'assets/fonts/fa-brands-400.woff2" as="font" type="font/woff2" crossorigin/>';
        $out_css .= '<link rel="preload" href="'.$static_root.'assets/fonts/fa-light-300.woff2" as="font" type="font/woff2" crossorigin/>';
        $out_css .= '<link rel="preload" href="'.$static_root.'assets/fonts/fa-regular-400.woff2" as="font" type="font/woff2" crossorigin/>';
        $out_css .= '<link rel="preload" href="'.$static_root.'assets/fonts/fa-solid-900.woff2" as="font" type="font/woff2" crossorigin/>';

        $out_css .= '<link rel="stylesheet" href="'.$static_root.'assets/css/_pack.css?'.$ts.'">'.PHP_EOL;

        $input_js = "";
        foreach($js_files as $filename) {
            $input_js .= file_get_contents('js/'.$filename);
        }
        $output_js = JSMin::minify($input_js);
        file_put_contents('js/_pack.js', $output_js);
        $out_js .= '<script src="'.$static_root.'assets/js/_pack.js?'.$ts.'"></script>'.PHP_EOL;
        break;
    default:
        // Просто сохраняем заголовки
        foreach($css_files as $filename) {
            $out_css .= '<link rel="stylesheet" href="assets/css/'.$filename.'?'.$ts.'">'.PHP_EOL;
        }

        foreach($js_files as $filename) {
            $out_js .= '<script src="assets/js/'.$filename.'?'.$ts.'"></script>'.PHP_EOL;
        }
        break;
}

// save headers to templates
file_put_contents($css_template_file, $out_css);
file_put_contents($js_template_file, $out_js);
echo $mode . ' mode - done' . PHP_EOL;
