#!/usr/bin/perl -w

use strict;

sub Usage() {
    print
"Usage
    $0 <sym file>

Description
    In geda sym files, the following types take dashstyle dashlength dashspace
    as part of their field list:
     L line
     B box
     V circle
     A arc
     H path

    If dashstyle is 0 (= type solid), then dashlength and dashspace should be -1.
    This program corrects that.
";
    exit();
}

my @fld;
my $line = "";
my $cnt = 0;
my @line = ();

sub minus($) {
    my $ix = shift;
    my $s = $fld[$ix] . $fld[$ix+1];

    my $len = length($s) - 2; # spaces neede, "-1" needs two positions
    if ($len < 1) { $len = 1; }

    my $str = (" " x $len) . "-1";

    #print "$ix: $len <$s> <$str>\n";

    $str;
}

sub fix($) {
    my $ix = shift;
    if (@fld < 2*$ix + 4 + 1) {
	print "E: <$line>";
	printf("  \@fld: %2d / %2d\n", @fld+0, 2*$ix + 4 + 1);
	return;
    }

    if ($fld[2*$ix] == 0 && ($fld[2*$ix + 2] != -1 || $fld[2*$ix + 4]) != -1) {
	my $str = "";
	for (my $ii = 0; $ii < 2*$ix+1; $ii++) { $str .= $fld[$ii]; }
	#print "<$str>\n";
	my $dashlength = minus(2*$ix+1);
	my $dashspace  = minus(2*$ix+3);

	$str .= $dashlength . $dashspace;

	for (my $ii = 2*$ix + 5; $ii < @fld; $ii++) { $str .= $fld[$ii]; }

	$_ = $str;
	$cnt++;
    } else {
	#print "I: $_\n";
    }
    push @line, $_;
    #print "<", join(">\n<", @fld), ">\n";
}

sub main() {
    my $Hact = 0;
    my $Tlines = 0;
    my %opt = ();

    for (my $ix = 0; $ix < @ARGV-1; $ix++) {
	my $str = shift @ARGV;
	if ($str =~ m/=/) {
	    my ($k, $v) = split(/=/, $str, 2);
	    $opt{$k} = $v;
	} else {
	    $opt{$str} = 1;
	}
    }

    if (@ARGV == 0) {
	Usage();
    }

    my $file = $ARGV[0];

    while (<>) {
	$line = $_;

	@fld = split(/(\s+)/);

	if ($Hact == 1) {
	    if ($fld[0] eq "z") {
		$Hact = 0;
	    }
	    push @line, $_;
	    next;
	}

	if ($Tlines) {
	    #print $line;
	    $Tlines--;
	    push @line, $_;
	    next;
	}

	if ($fld[0] eq "L") { fix(8); next; }
	if ($fld[0] eq "B") { fix(8); next; }
	if ($fld[0] eq "V") { fix(7); next; }
	if ($fld[0] eq "A") { fix(9); next; }

	if ($fld[0] eq "H") {
	    fix(4);
	    $Hact = 1;
	    next;
	}

	if ($fld[0] eq "T") {
	    #print $line;
	    $Tlines = $fld[2*9];
	}

	push @line, $_;
    }

    if ($opt{test}) {
	if ($cnt) {
	    print "$file: needs upd\n";
	} else {
	    #print "$file: ok\n";
	}
    } elsif ($cnt) {
	print @line;
    }
}

######

main();
exit($cnt);

__END__

type     field position
L line   8  9 10  # last 3
B box    8  9 10  # in the middle
V circle 7  8  9  # in the middle
A arc    9 10 11  # last 3
H path   4  5  6  # in the middle



    my $type = substr($_, 0, 1);
    my $fld  = substr($_, 1);

    print "<$type> <$fld>\n";
    last;

    my @fld = split(/\s*\S+/g);

    print "<", join(">\n<", @fld), ">\n";
    last;
    if (m/^L(\s+\S+)(\s+\S+)(\s+\S+)(\s+\S+)(\s+\S+)(\s+\S+)(\s+\S+)(\s+\S+)(\s+\S+)(\s+\S+)/) {
	print "$_\n";
	my $pre = "L$1$2$3$4$5$6$7";
	my $sty = $8;
	my $len = $9;
	my $spc = $10;
	print "$pre / $sty / $len / $spc\n";
	last;
    }
