#!/usr/bin/perl -w

# Copyright (c) 2006, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# ---
# Author: Andrew Fikes
#
# Tool to convert a template file (.tpl) into a C++ header
# file with a const string defining the same template.  This string
# can then be used to create/retrieve a template using the
# TemplateFromString class (see template_from_string.h).
#
# Usage: template-converter <template_name> < infile > outfile
#
# template_name is the name of the variable we export.  A good choice
# is to pass in the outfile name.  To make that easier, we treat
# <template_name> as a pathname, and take the basename, strip the
# suffix if it's .h, and sanitize the rest of the name to be a legal
# C variable name.


# Open template file
(my $template_name = shift) || usage ("Need to specify template variable name.");

# Get base name of template file 
$base_name = $template_name;
$base_name =~ s|^.*/([^/]*)$|$1|;  # Strip out directory name
$base_name =~ s|\.h$||;            # Strip out suffix, if it's .h
$base_name =~ tr|A-Za-z0-9_|_|c;   # Sanitize name to remove non-letters/nums

# Print header 
print "// This file automatically generated by template-converter\n";
print "// DO NOT EDIT!\n\n";
print "#ifndef _" . uc($base_name) . "_H\n";
print "#define _" . uc($base_name) . "_H\n\n";

# Read in template file and print template as a string
print "const string ${base_name} (\n";
while (<>) {
  chomp;
  my $escaped_line = escape_line($_);
  print "\"$escaped_line\\n\"\n";  
}
print ");\n\n";

# Print footer and exit
print "#endif /* _" . uc($base_name) . "_H */\n";
exit(0);

# Prints usage message
sub usage {
  my $msg = shift;
  print STDERR "\n$msg\n";
  print STDERR "Usage: template-converter <template-varname>\n\n"  ;
  exit(1);
}

# Escapes line (adds a '\' to quotes and possible control characters)
sub escape_line {
  (my $line) = (@_);
  $line =~ s|\\|\\\\|g;
  $line =~ s|\"|\\"|g;
  return $line;
}