#!/usr/bin/perl -w
# NAME: gen-defines.pl
# AIM: VERY SPECIAL
use strict;
use warnings;
use File::Basename;  # split path ($name,$dir,$ext) = fileparse($file [, qr/\.[^.]*/] )
use Cwd;
my $perl_dir = 'C:\GTools\perl';
unshift(@INC, $perl_dir);
require 'lib_utils.pl' or die "Unable to load 'lib_utils.pl'...\n";
# log file stuff
our ($LF);
my $pgmname = $0;
if ($pgmname =~ /(\\|\/)/) {
    my @tmpsp = split(/(\\|\/)/,$pgmname);
    $pgmname = $tmpsp[-1];
}
my $outfile = $perl_dir."\\temp.$pgmname.txt";
open_log($outfile);

# user variables
my $load_log = 1;
my $in_file = '';

my $debug_on = 0;
my $def_file = 'C:\Projects\nsis\svn-trunk\nsis-sconf.h';

### program variables
my @warnings = ();
my $cwd = cwd();
my $os = $^O;

sub show_warnings($) {
    my ($val) = @_;
    if (@warnings) {
        prt( "\nGot ".scalar @warnings." WARNINGS...\n" );
        foreach my $itm (@warnings) {
           prt("$itm\n");
        }
        prt("\n");
    } else {
        ###prt( "\nNo warnings issued.\n\n" );
    }
}

sub pgm_exit($$) {
    my ($val,$msg) = @_;
    if (length($msg)) {
        $msg .= "\n" if (!($msg =~ /\n$/));
        prt($msg);
    }
    show_warnings($val);
    close_log($outfile,$load_log);
    exit($val);
}


sub prtw($) {
   my ($tx) = shift;
   $tx =~ s/\n$//;
   prt("$tx\n");
   push(@warnings,$tx);
}

sub process_in_file($) {
    my ($inf) = @_;
    if (! open INF, "<$inf") {
        pgm_exit(1,"ERROR: Unable to open file [$inf]\n"); 
    }
    my @lines = <INF>;
    close INF;
    my $lncnt = scalar @lines;
    prt("Processing $lncnt lines, from [$inf]...\n");
    my ($line,$inc,$lnn,$ch,$i,$len,$nc,$nline,$i2,$tline,$incomm);
    my ($def,@arr,$cnt,$val);
    $lnn = 0;
    $incomm = 0;
    $nline = '';
    foreach $line (@lines) {
        chomp $line;
        $lnn++;
        $tline = trim_all($line);
        $len = length($tline);
        next if ($len == 0);
        for ($i = 0; $i < $len; $i++) {
            $i2 = $i + 1;
            $ch = substr($tline,$i,1);
            $nc = ($i2 < $len) ? substr($tline,$i2,1) : "";
            if ($incomm) {
                if (($ch eq '*')&&($nc eq '/')) {
                    $incomm = 0;
                    prt("$i2: Exit comment...\n");
                    $i++;
                }
            } else {
                if (($ch eq '/')&&($nc eq '*')) {
                    prt("$i2: Enter comment...\n");
                    $incomm = 1;
                    $i++;
                    next;
                }
                if (($ch eq '/')&&($nc eq '/')) {
                    last; # end of this line
                }
                $nline .= $ch;;
            }
        }
        $len = length($nline);
        next if ($len == 0);
        $cnt = 0;
        if ($nline =~ /^\#\s*define\s+(.+)$/) {
            $def = $1;
            #prt("DEF $def\n");
            @arr = space_split($def);
            $cnt = scalar @arr;
        } elsif ($nline =~ /^\#\s*undef\s+(.+)$/) {
            $def = $1;
            #prt("UND $def\n");
            @arr = space_split($def);
            $cnt = scalar @arr;
        } else {
            prt("OTHER: $nline\n");
        }
        if ($cnt) {
            $def = $arr[0];
            prt("#ifdef $def\n");   # NSIS_CONFIG_COMPONENTPAGE
            if ($cnt == 1) {
                prt("    definedlist.add(_T(\"$def\"));\n");
            } elsif ($cnt == 2) {
                $val = $arr[1];
                if (($val =~ /^\d$/)&&($val == 1)) {
                    prt("    definedlist.add(_T(\"$def\"));\n");
                } else {
                    $val =~ s/^\"//;
                    $val =~ s/\"$//;
                    prt("    definedlist.add(_T(\"$def\"),_T(\"$val\"));\n");
                }
            } else {
                pgm_exit(1,"CHK COUNT $cnt [$nline]\n");
            }
            prt("#endif\n");
        }
        $nline = '';
    }
}

#########################################
### MAIN ###

$in_file = $def_file;
#parse_args(@ARGV);
#prt( "$pgmname: in [$cwd]: Hello, World...\n" );
process_in_file($in_file);
pgm_exit(0,"");

########################################
sub give_help {
    prt("$pgmname: version 0.0.1 2010-09-11\n");
    prt("Usage: $pgmname [options] in-file\n");
    prt("Options:\n");
    prt(" --help (-h or -?) = This help, and exit 0.\n");
}
sub need_arg {
    my ($arg,@av) = @_;
    pgm_exit(1,"ERROR: [$arg] must have following argument!\n") if (!@av);
}

sub parse_args {
    my (@av) = @_;
    my ($arg,$sarg);
    while (@av) {
        $arg = $av[0];
        if ($arg =~ /^-/) {
            $sarg = substr($arg,1);
            $sarg = substr($sarg,1) while ($sarg =~ /^-/);
            if (($sarg =~ /^h/i)||($sarg eq '?')) {
                give_help();
                pgm_exit(0,"Help exit(0)");
            } else {
                pgm_exit(1,"ERROR: Invalid argument [$arg]! Try -?\n");
            }
        } else {
            $in_file = $arg;
            prt("Set input to [$in_file]\n");
        }
        shift @av;
    }

    if ((length($in_file) ==  0) && $debug_on) {
        $in_file = $def_file;
    }
    if (length($in_file) ==  0) {
        pgm_exit(1,"ERROR: No input files found in command!\n");
    }
    if (! -f $in_file) {
        pgm_exit(1,"ERROR: Unable to find in file [$in_file]! Check name, location...\n");
    }
}

# eof - template.pl
