#!/usr/bin/php -qC
<?php
#
# Combine html/css files into skin configuration card .txt file.
#
# Understands *.header.*, *.footer.* and *.css.* filenames, and
# assumes the template name from file basenames or parent dirnames.
#
#
# To assemble template parts:
#
#      mkskin skin/tmplname/*.* > skin.txt
#
#
# Alternatively craft REPLACE INTO config SQL statments,
# or combined into a "skin:name" config card import file even:
#
#      mkskin --sql skin/tmpl/*        > skin.sql
#      mkskin --sql --reg skin/tmpl/*  > skin-reg.txt
#


# cmd args as input files to merge
$files = array_slice($_SERVER["argv"], 1);
$opts = preg_grep("/^-/", $files);
$files = array_values(array_diff($files, $opts));
if (empty($files)) {
   exit("Usage:\n  mkskin [--sql] [--reg] parts/name/* > output.txt\n");
}
$o_sql = !!preg_grep("/sql|raw|insert|replace/i", $opts);
$o_reg = !!preg_grep("/reg|skin|name|blob|combine/i", $opts);


# some defaults
$skin_name = skin_name($files[0]);
$time = time();
$date = gmstrftime("%Y-%m-%dT%H-%M-%SZ", $time);
$q = new PDO("sqlite::memory:");
$output = "";



#-- loop over files
foreach ($files as $fn) {

    # figure out class
    $config = config_name($fn);
    if (!$config) {
       continue;
    }
    
    # read
    $value = file_get_contents($fn);
    $value = preg_replace("/\R/", "\n", $value);

    # append
    if (!$o_sql) {
       $output .= config_name_value_line($config, $value);
    }
    else {
       $output .= config_sql_replace_line($config, $value);
    }
}

#-- header, and wrapping
if (!$o_sql) {
  $output = "# Fossil skin configuration \"$skin_name\" for simple `fossil config import skin.txt`\n"
          . "# $date\n#\n"
          . $output;
}
elseif (!$o_reg) {
  $output = "-- Fossil skin \"$skin_name\" as raw SQL statements ($date)\n\n"
          . $output;
}
else {
  $output = "# Config \"skin:$skin_name\" import card\n\n"
          . config_name_value_line("skin:$skin_name", $output);
}

#-- output combined SQL config file
print $output;



/**
 * Wraps value into "config /config nnn" line.
 *
 */
function config_name_value_line ($name, $value) {
    global $time, $q;

    # quote for SQL context
    $value = $q->quote($value);
    $length = strlen($value) + strlen("$time '$name' value ");

    # and append to sql/config output
    return "config /config $length\n"
         . "$time '$name' value $value\n";
}

/**
 * Wraps value into "config /config nnn" line.
 *
 */
function config_sql_replace_line ($name, $value) {
    global $time, $q;
    $value = $q->quote($value);
    return "REPLACE INTO config (name,mtime,value) VALUES ('$name', $time, $value);\n\n";
}



#-- use directory or basename as skin name
function skin_name($fn) {
    if (preg_match("~(?:^|/)((?!header|footer|css|js)[\w-]+)(?:\.(?:\w{2,4}|header|footer|js|th1-setup))*$~", $fn, $uu)) {
        return $uu[1];
    }
    return basename(dirname($fn));
}

#-- extract header/footer/css from filename
function config_name($fn) {
    if (preg_match("~(header|footer|css|js|th1-setup).*?$~", $fn, $uu)) {
        return $uu[1];
    }
    return NULL;
}
