<?php

if ($_SERVER['argc'] < 1) {
    die("Usage: php -f xcompile.php /full/path/to/templatefile.htm\n");
}
$filename = realpath($_SERVER['argv'][1]);
if (!file_exists($filename)) {
    die("file {$filename} does not exist");
}
echo "converting template: {$filename}\n";
$contents = file_get_contents($filename);
$original = $contents;
$contents = migrate_templates($contents);
if ($original === $contents) {
    echo "...No changes made to {$filename} \n";
    exit;
}
file_put_contents($filename, $contents);
echo "...Changes written to {$filename} \n";
function migrate_templates($content)
{
    $content = preg_replace_callback('`(<(script|style)[^>]*>)(.*?)(</\\2>)`s', 'z_prefilter_add_literal_callback', $content);
    $content = str_replace('<!--[', '{', $content);
    $content = str_replace(']-->', '}', $content);
    $content = str_replace('{pn', '{', $content);
    $content = str_replace('{/pn', '{/', $content);
    $content = str_replace('|pn', '|', $content);
    $content = str_replace('|date_format', '|dateformat', $content);
    $content = str_replace('|varprepfordisplay', '|safetext', $content);
    $content = str_replace('|varprephtmldisplay', '|safehtml', $content);
    return $content;
     }
     continue;
 }
 if (stripos($fn, '/data/emoticons') !== false) {
     $dest = SERVDIR . str_replace('/data/emoticons/', '/skins/emoticons/', $fn);
     if (!copy(OLDDIR . $fn, $dest)) {
         $fail[] = array('Cannot copy the file', OLDDIR . $fn, $dest);
     }
     continue;
 }
 if (stripos($fn, '/data/config.php') !== false) {
     migrate_config();
     continue;
 }
 if (pathinfo($fn, PATHINFO_EXTENSION) === 'tpl') {
     migrate_templates($fn);
     continue;
 }
 if (stripos($fn, '.htaccess') !== false) {
     continue;
 }
 $path = SERVDIR . '/cdata';
 foreach (explode('/', str_ireplace('/data/', '', $fn)) as $dc) {
     $path .= '/' . $dc;
     if (strpos($dc, '.') === false) {
         if (!is_dir($path) && !mkdir($path, 0777)) {
             $fail[] = array('Cannot create the folder', $path);
         } else {
             chmod($path, 0777);
         }
     } else {