#! /usr/bin/perl

# $Id: unpack-sdf.src,v 1.1 2001/05/31 14:20:52 mdejonge Exp $

# A script which puts each sdf2 module in a single sdf2 definition file
# in a separate file.

# This script used to be called modularize-sdf2.
# All blame should be directed to: Tobias Kuipers <kuipers@cwi.nl>

use strict;

if ($#ARGV != 0) {
  &usage;
}

sub usage() {
  print "Usage: unpack-sdf <file.sdf>\n";
  exit 0;
}

# The name of the sdf2 file
my $sdffile = $ARGV[0];

# Flag which tells us we're not in a module yet:
my $inmodule = 0;

# The name of the current module
my $modulename;

# The extension of the month
my $moduleext = ".sdf";

open (SDF,"$sdffile") || die ("Can't open $sdffile\n");

while (<SDF>) {
  if ($_ =~ /^\s*module\s+(\S+)/) {
    $modulename = $1;
    if ($inmodule) {
      # We are already in a module, close it
      close(MODULE);
    } else {
      # We're in a module now (first one)
      $inmodule = 1;
    }
    # And open a new one
    open(MODULE, ">$modulename$moduleext") || die("Can't open $modulename$moduleext\n");
    
    # Write the current line to the new module
    print MODULE $_;
  } elsif ($inmodule) {
    print MODULE $_;
  }
}    
