#!/usr/bin/perl -w

use strict;
use Opt;
use Prefix;
use SwPwr;

#############

sub Usage($) {
    my $str = shift;

    if ($str) {
	print $str;
	print "\n";
    }

    print
"Usage:
\tsw_pwr.pl <function> Vi=.. Vo=.. other_values...
Available functions:
";
    for my $k (sort keys %SwPwr::disp) {
	my $r = $SwPwr::disp{$k};
	my ($func, $doc) = @$r;
	printf "\t%-15s %s\n", "$k:", $doc;
    }

    if ($str) {
	exit 1;
    }
    exit 0;
}


sub main() {
    my %data;
    my %save;

    SwPwr::SwInit(\%data, \&Usage);
    %save = %data;

    for my $function (@ARGV) {
	if (defined($function)) {
	} else {
	    Usage("");
	}

	my $ref = $SwPwr::disp{$function};
	if (!defined($ref)) {
	    Usage("ERR: Unknown function: <$function>\n");
	}
	my ($func,$doc) = @{$ref};

	if ($data{IC} eq "mc33063") {
	    &{$func}(\%data);
	    SwPwr::IC_mc33063(\%data);
	    &{$func}(\%data);
	    SwPwr::common(\%data);
	    SwPwr::IC_mc33063_check(\%data);
	}

	if (defined($data{Vmax})) {
	    %save = %data;
	    $save{Vi} = $data{Vmax};
	    &{$func}(\%data);
	    SwPwr::common(\%data);
	    &{$func}(\%save);
	    SwPwr::common(\%save);
	    SwPwr::printIt(\%data, \%save);
	    if ($data{IC} eq "mc33063") {
		Prefix::println( 1, [ \%data ], " ", qw/ Vrmin V / );
	    }
	} elsif ($function eq "pr") {
	    SwPwr::printIt(\%data);
	} else {
	    &{$func}(\%data);
	    SwPwr::common(\%data);
	    SwPwr::printIt(\%data);
	}
	print "\n";
    }
}

main();

__END__
