<?php


$aFiles = array_diff(scandir('models_orig'), array('..', '.'));

//var_dump($aFiles);


foreach ($aFiles as $sFile) {
    $oldfile = file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/models_orig/' . $sFile);

    $oldfile = substr($oldfile, 0, strrpos($oldfile, '#') + 1) . ' ' . PHP_EOL . 'mtllib Savana_a_Liverpool' . rand() . '.mtl' . substr($oldfile, strrpos($oldfile, '#') + 3);
    $i = 1;
    $newfile = preg_replace_callback('/vn(.*?)\nf/', function($m)use(&$i){return 'vn' . $m[1] .PHP_EOL .'usemtl ' . ($i++) . '_Definition1' . PHP_EOL . 'f';}, $oldfile);
    $newfile = preg_replace('/g.*/', 'o', $newfile);

//file_put_contents('G:\bakalarka\Modeler\models\\'  . pathinfo($path, PATHINFO_FILENAME) . '1.obj', $newfile);
    file_put_contents('models_converted/'  . $sFile, $newfile);
}