#!/usr/bin/perl -wn

# Simple INI format demo by _Vi; L=MIT; 2012

use strict;

our $section_name;

INIT {
    $section_name = "general";
}

# "345-sdf sdfs$f" -> "345_sdf_sdfs_f"
sub sanitize_name($) {
    $_ = shift;
    $_ =~ s/[^a-zA-Z0-9_]/_/g;
    return $_;
}

sub process_assignment($$) {
    my $nam = shift;
    my $val = shift;
    
    print uc($section_name . "_" . sanitize_name($nam)) . "=" . $val . "\n"
}

sub process_section($) {
    my $nam = shift;
    $section_name = sanitize_name($nam);
}

/^\s*[#;]/ and next; # ignore comments
/^\s*$/ and next; # ignore empty lines

/^\s* ([^=]+?) \s* \= \s* (.*?) \s*$/x and process_assignment($1, $2) and next;

/^\s* \[ \s* ([^\]]+?) \s* \]   \s*$/x and process_section($1) and next;

print STDERR "Malformed config line: $_";

