#!/usr/bin/perl -w

use strict;
use lib ".";
#use Geda;
use Common;
use Pcb;
use Opt;
use Data::Dumper;

my %disp;

sub testShow() {
    Pcb::flagShowType();
    Pcb::flagShowFlagArr();
    Pcb::flagShowFlagHash();
}
$disp{show} = [ \&testShow, "show flag constants" ];

sub testType() {
    for (@ARGV) {
	my $num = Pcb::flagType2Num($_);
	my $sym = Pcb::flagType2Sym($_);

	printf("<%s> 0x%05x <%s>\n", $_, $num, $sym);
    }
}
$disp{type} = [ \&testType, "test flagType2Num/Sym()" ];

sub testIs() {
    for (@ARGV) {
	my $num  = Pcb::flagIsNumeric($_);
	my $symb = Pcb::flagIsSymbolic($_);
	printf "IsNumeric(%s) = <%s>\n", $_, Common::prHex($num);
	printf "IsSymbolic(%s) = <%s>\n", $_, Common::prStr($symb);

	$num = Pcb::flagToNum($_);
	$symb = Pcb::flagToSymb($_, $OPT{type});
	printf "ToNum(%s) = <%s>\n", $_, Common::prHex($num);
	printf "ToSymb(%s) = <%s>\n", $_, Common::prStr($symb);

	print "\n";
    }
}
$disp{is} = [ \&testIs, "test flagIsNumeric/Symbolic and flagToNum" ];

sub testIsSet() {
    while (@ARGV) {
	my $flag = shift @ARGV;
	my $test = shift @ARGV;
	last if (!defined($test));

	my $res = Pcb::flagIsSet($flag, $test);
	if (defined($res)) {
	    printf "flagIsSet(%s,%s) = 0x%05x\n", $flag, $test, $res;
	} else {
	    printf "flagIsSet(%s,%s) = undef\n", $flag, $test;
	}
    }
}
$disp{isset} = [ \&testIsSet, "test flagIsSet()" ];

sub testToggle() {
    while (@ARGV) {
	my $flag = shift @ARGV;
	my $toggle = shift @ARGV;
	last if (!defined($toggle));

	my $res = Pcb::flagToggle($flag, $toggle, $OPT{type});
	if (defined($res)) {
	    printf "flagToggle(\"%s\", %s) = %s\n", $flag, $toggle, $res;
	} else {
	    printf "flagToggle(\"%s\", %s) = undef\n", $flag, $toggle;
	}
    }
}
$disp{toggle} = [ \&testToggle, "test flagToggle()" ];

sub testSplitSymbolic() {
    if (@ARGV) {
	while (@ARGV) {
	    my $flag = shift @ARGV;

	    my @flag = Pcb::flagSplitSymbolic($flag);
	    printf "flagSplitSymbolic(\"%s\") = %s\n", $flag, join(" ", @flag);
	}
    } else {
	while (<>) {
	    my $flag = $_;
	    chomp $flag;
	    my @flag = Pcb::flagSplitSymbolic($flag);
	    printf "flagSplitSymbolic(\"%s\") = <%s>\n", $flag, join("> <", @flag);
	}
    }
}
$disp{split} = [ \&testSplitSymbolic, "test flagSplitSymbolic()" ];

sub testParse() {
    for my $file (@ARGV) {
	my @line = Common::read_file($file);
	my %hash = Pcb::parse_pcb(@line);
	Pcb::printData("", %hash);
    }
}
$disp{parse} = [ \&testParse, "test parse_pcb()" ];

sub testPolygon() {
    for my $file (@ARGV) {
	my @line = Common::read_file($file);
	my %hash = Pcb::parse_pcb(@line);
	Pcb::polygon(%hash);
    }
}
$disp{polygon} = [ \&testPolygon, "test polygon()" ];

sub testBB() {
    for my $file (@ARGV) {
	my @line = Common::read_file($file);
	my %hash = Pcb::parse_pcb(@line);
	Pcb::get_BB(@line);
    }
}
$disp{bb} = [ \&testBB, "test get_BB()" ];

sub main() {
    $OPT{type} = "PAD_TYPE";
    Opt::opt(\&Opt::UsageDisp);

    my $func = Opt::disp(shift @ARGV, "", $disp);
    &$func();
}

main;


__END__
