#! /usr/bin/perl
#
#  Copyright (c) 2000
# 	Konstantin Chuguev.  All rights reserved.
# 
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. 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.
#  3. All advertising materials mentioning features or use of this software
#     must display the following acknowledgement:
# 	This product includes software developed by Konstantin Chuguev
# 	and its contributors.
# 
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
# 
# 	iconv (Charset Conversion Library) v2.0
#

require 'getopts.pl';

&Getopts('mno:s:');

if ($opt_o) {
	open(STDOUT, ">$opt_o");

	print "#define ICONV_INTERNAL\n#include <iconv.h>\n\n";
	$cat = $opt_m ? "ces" : "ccs";
	foreach $file (@ARGV) {
		if ($file eq 'PIC') {
			print "#ifndef PIC\n";
			$pic = 1;
			next;
		}
		$var = $file;
		$var =~ s/.[co]$//;
		$var =~ tr/-/_/;
		if ($opt_m) {
			print "extern const struct iconv_ces_desc iconv_ces_$var;\n";
		} else {
			print "extern const unsigned char iconv_ccs_table_$var" . "[];\n";
		}
	}
	print "#endif\n" if $pic;
	$pic = 0;
	print "\nconst iconv_builtin_table iconv_builtin_" . $cat . "[] = {\n";
	foreach $file (@ARGV) {
		if ($file eq 'PIC') {
			print "#ifndef PIC\n";
			$pic = 1;
			next;
		}
		$file =~ s/.[co]$//;
		$var = $file;
		$var =~ tr/-/_/;
		if ($opt_m) {
			print qq/\t{ "$file", &iconv_ces_$var },\n/;
		} else {
			print qq/\t{ "$file", &iconv_ccs_table_$var },\n/;
		}
	}
	print "#endif\n" if $pic;
	print "\t{ NULL, NULL }\n};\n";
	exit 0;
}

$pic = 0;

%builtins = map { $pic = 1 if $_ eq 'PIC'; $_ => $pic } @ARGV;

while (<STDIN>) {
	unless (/^([^[:space:]#]+)/) {
		# line continuation or comment
		print $_ unless $opt_s || !$opt_n || /^#WARNING/;
		next;
	}
	# a real name is at the first position of the line
	if ($opt_s) {
		$_ = "$1$opt_s\n";
	} elsif (!$opt_n) {
		chomp;
		$_ = "\t\"$_\\n\"\n";
		if (exists $builtins{"$1.o"}) {
			if ($builtins{"$1.o"}) {
				$pic_lines .= $_;
			} else {
				$lines .= $_;
			}
		}
		next;
	}
	if ($opt_n) {
		print $_ unless exists $builtins{$1};
	} else {
		print $_ if exists $builtins{$1};
	}
}

if (!$opt_s && !$opt_n) {
	print $lines;
	print "#ifndef PIC\n$pic_lines" . "#endif\n" if $pic_lines;
}
