#!/usr/bin/perl

# This perl script read stdin and write on stdout. It shall be an XML language file.
#
# * If the name of the language is 'HTML', then it creates the language 'PHP (HTML)'
#   which shall be used for PHP hl.
#
# * If the name of the language is something else (say '*'), it creates the language '*/PHP'.
#   This new language is the same as the old one, but is able to detect PHP everywhere.
#
# This script will correctly set extensions & mimetype, and will replace
# <IncludeRules context="##*"> by <IncludeRules context="##*/PHP">
#
# Generated languages need a language named 'PHP/PHP', which shall take care of PHP hl itself
# and which will be called every time something like <?php is encountred.
#
# SPDX-FileCopyrightText: Jan Villat <jan.villat@net2000.ch>
# License: LGPL

my $file = "";

open(my $input, '<:encoding(UTF-8)', $ARGV[0])
  or die "Could not open file '$ARGV[0]': $!";

open(my $output, '>:encoding(UTF-8)', $ARGV[1])
  or die "Could not open file '$ARGV[1]': $!";

while (<$input>)
{
  $file .= $_;
}

$warning = "\n\n<!-- ***** THIS FILE WAS GENERATED BY A SCRIPT - DO NOT EDIT ***** -->\n";

$file =~ s/(?=<language)/$warning\n\n\n/;

if ($file =~ /<language[^>]+name="HTML"/)
{
  $root = 1;
}

if ($root == 1)
{
  $file =~ s/<language([^>]+)name="[^"]*"/<language$1name="PHP (HTML)"/s;
  $file =~ s/<language([^>]+)section="[^"]*"/<language$1section="Scripts"/s;
  $file =~ s/<language([^>]+)extensions="[^"]*"/<language$1extensions="*.php;*.php3;*.wml;*.phtml;*.phtm;*.inc;*.ctp"/s;
  $file =~ s/<language([^>]+)mimetype="[^"]*"/<language$1mimetype="text\/x-php4-src;text\/x-php3-src;text\/vnd.wap.wml;application\/x-php"/s;
  $file =~ s/<language([^>]+)*/<language$1 indenter="cstyle"/s;
}
else
{
  if ($file =~ /<language[^>]+hidden="[^"]*"/) {
    $file =~ s/<language([^>]+)name="([^"]*)"/<language$1name="$2\/PHP"/s;
    $file =~ s/<language([^>]+)hidden="[^"]*"/<language$1hidden="true"/s;
  }
  else
  {
    $file =~ s/<language([^>]+)name="([^"]*)"/<language$1name="$2\/PHP" hidden="true"/s;
  }
  $file =~ s/<language([^>]+)section="[^"]*"/<language$1section="Other"/s;
  $file =~ s/<language([^>]+)extensions="[^"]*"/<language$1extensions=""/s;
  $file =~ s/<language([^>]+)mimetype="[^"]*"/<language$1mimetype=""/s;
}

if ($root == 1 || $ARGV[0] =~ /mustache.xml$/)
{
  $file =~ s/<(?:RegExpr (attribute="Processing Instruction" context="PI"|context="PI" attribute="Processing Instruction")|itemData name="Processing Instruction")[^\/]+\/>|<context name="PI".*?<\/context>//gs;
}

$findphp = "<context name=\"FindPHP\" attribute=\"Normal Text\" lineEndContext=\"#stay\">\n<Detect2Chars context=\"##PHP/PHP\" char=\"&lt;\" char1=\"?\" lookAhead=\"true\" />\n</context>\n";

$file =~ s/<IncludeRules\s([^>]*)context="([^"#]*)##(?!Alerts|Comments|Doxygen|Modelines)([^"]+)"/<IncludeRules $1context="$2##$3\/PHP"/g;
$file =~ s/(<context\s[^>]*[^>\/]>)/$1\n<IncludeRules context="FindPHP" \/>/g;
$file =~ s/(<context\s[^>]*[^>\/])\s*\/>/$1>\n<IncludeRules context="FindPHP" \/>\n<\/context>/g;
$file =~ s/(?=<\/contexts\s*>)/$findphp/;

print $output $file;
print $output $warning;
