#!/usr/bin/perl
#
# Script to convert fw-data.ini file to shella
#
# Copyright (C) 2009, Niall Walsh <niallwalsh@users.berlios.de>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

use strict;
my $file=$1;
if (length($file)==0)
{
	$file='fw-data.ini';
}
unless (-f $file)
{
	die "you must supply a valid filename to load from if fw-data.ini is not in the working directory";
}
# Using Config::Any instead would allow to switch to anything it supports
#use Config::Any;
#my $cfg = Config::Any->load_files({files => [ $file ] });
#my $cards = $$cfg[0]{$file};
use Config::Tiny;
my $cards = Config::Tiny->read($file);
my $name;
my $num=0;
my (@names, @fw, @url, @file, @dev, @map, @ok);
foreach $name (sort(keys(%$cards)))
{
	push(@names,$name);
	my $props = $$cards{$name};
	my $field;
	# can't use string as ref with strict so map name to array
	my %fmap = ('fw'=>\@fw, 'url'=>\@url, 'ok'=> \@ok);
	foreach $field ('fw','url','ok')
	{
		my $a = $fmap{$field};
		if (length($$props{$field}))
		{
			push(@$a,"$$props{$field}");
		}
		else
		{
			push(@$a,'""');
		}
	}
	my $prop;
	my $drivers=$$props{'drivers'};
	$drivers=~s/^\s*\"(.*)\"\s*$/$1/;
	foreach $prop (split(/\s+/,$drivers))
	{
		push(@dev,'"'.$prop.'"');
		push(@map,@names-1);
	}
}
print 'FW_CARDS=( "'.join('" "',@names).'")'."\n";
print 'FW_CARDS_FW=( '.join(' ',@fw).')'."\n";
print 'FW_CARDS_FW_URL=( '.join(' ',@url).')'."\n";
print 'FW_CARDS_FW_OK=( '.join(' ',@ok).')'."\n";
print 'FW_CARDS_FW_DEV_STR=( '.join(' ',@dev).')'."\n";
print 'FW_CARDS_FW_DEV_NUM=( '.join(' ',@map).')'."\n";
