#!/usr/bin/perl -w
#
# msc - Generate text message sequence charts
# Copyright (C) 2001 - Tarball <rubens_ramos@yahoo.com>
#
# $Id: msc.in,v 1.5 2001/05/29 10:21:40 rubensr Exp $
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Generated automatically from msc.in by configure.

use Getopt::Long;

$VERSION="msc version 1.1.0";

$task_w=8;
$mesg_w=15;
$comm_w=5;
$help=0;
$pbreak=0;
$FORMFEED=12;
$swap=0;

ParseOpts();

print "\n";
while (<>) {
  if (!((/^\s*\#.*$/) || (/^\s*$/))) {
	if (/Title\s*:\s*/)	{
	  @title = split(/:/);
	  chomp @title;
	  print $title[1], " - $ARGV\n\n";
	}
	elsif (/Tasks\s*:\s*/) {
	  @tasks = split(/:/);
	  @tasks = split(/,/, $tasks[1]);
	  chomp @tasks;
	  print " " x $mesg_w;
	  for ($i=0; $i<$#tasks+1; $i++) {
		print pack("A$task_w", $tasks[$i]);
	  }
	  print "\n";
	  print " " x $mesg_w;
	  for ($i=0; $i<$#tasks+1; $i++) {
		print pack("A$task_w", "|");
	  }
	  print "\n";
	}
	elsif (/^\*/) {
	  s/^\*//g;
	  print $_;
	}
	else {
	  @mesgs = split(/:/);
	  chomp @mesgs;
	  if ($mesgs[0] eq "") {
		print " " x $mesg_w;
		for ($i=0; $i<$task_w*($#tasks)+2; $i++) {
		  if (($i % $task_w) == 0) {
			print "|";
		  } else {
			print " ";
		  }
		}
	  } else {
		print pack("A$mesg_w", $mesgs[0]);
		for ($i=0,$found=0; $i<$#tasks+1; $i++) {
		  if ($mesgs[1] eq $tasks[$i]) {
			$min = $i * $task_w;
			$found = 1;
		  }
		}
		
		if (!$found)
		  {
			print "\nmsc: Couldn't find task $mesgs[1]..aborting.\n\a";
			exit 1;
		  }
		
		for ($i=0,$found=0; $i<$#tasks+1; $i++) {
		  if ($mesgs[2] eq $tasks[$i]) {
			$max = $i * $task_w;
			$found = 1;
		  }
		}
		
		if (!$found)
		  {
			print "\nmsc: Couldn't find task $mesgs[2]..aborting.\n\a";
			exit 1;
		  }
		
		if ($max > $min) {
		  $flag = 1;
		} else {
		  $aux = $min;
		  $min = $max;
		  $max = $aux;
		  $flag = 0;
		}
		
		for ($i=0; $i<$task_w*($#tasks)+2; $i++) {
		  if (($i == $min+1) && ($flag == 0)) {
			print "<";
		  }
		  elsif (($i == $max-1) && ($flag == 1)) {
			print ">";
		  }
		  elsif (($i > $min) && ($i < $max)
				 && (($i % $task_w) == 0)) {
			print "+";
		  }
		  elsif (($i % $task_w) == 0) {
			print "|";
		  }
		  elsif (($i < $min) || ($i > $max)) {
		    print " ";
		  }
		  elsif ($mesgs[0] =~ /.*\(\)/) {
			if ($swap) {
			  print "-";
			}
			else {
			  print "=";
			}
		  }
		  else {
			if ($swap) {
			  print "=";
			}
			else {
			  print "-";
			}
		  }
		}
	  }
	  print " " x $comm_w;
	  if ($#mesgs == 3) {
		print $mesgs[3];
	  }
	  print "\n";
	}
  }
  if (eof) {
	print " " x $mesg_w;
	for ($i=0; $i<$#tasks+1; $i++) {
	  print pack("A$task_w", "|");
	}
	print "\n\n";
	if ($pbreak) {
	  print chr $FORMFEED;
	}
  }
}

sub ParseOpts {
	%optctl = ("messagewidth" => \$mesg_w,
			   "taskwidth" => \$task_w,
			   "commentwidth" => \$comm_w,
			   "version" => \$version,
			   "pagebreak" => \$pbreak,
			   "swapstyles" => \$swap,
			   "help" => \$help);

	GetOptions(\%optctl, "messagewidth=i", 
			   "taskwidth=i", "commentwidth=i",
			   "help", "pagebreak", "version",
			   "swapstyles");

	if ($help) {
		Usage();
		exit 0;
	}

	if ($version) {
	  print $VERSION . "\n";
	  exit 0;
	}
}

sub Usage {
print "$VERSION

Usage:
msc [options] [file...]

Options:
-m|--messagewidth=<int> : width of message column
-t|--taskwidth=<int> : width of task columns
-c|--commentwidth=<int> : comments tab distance
-p|--pagebreak : add a pagebreak after chart
-s|--swapstyles : swap linestyles for functions and messages
-h|--help : this message
-v|--version : msc version number
"
}

__END__
