#! /usr/bin/perl

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

# This script takes a file containing a list of modules and put each
# module in a separate file with the indicated suffix.

# This script used to be called modulerize.
# It is a modification of Tobias' script 'modularize-sdf' which puts 
# each sdf2 module in a single sdf2 definition file in a separate file.

# Blame concerning the modifications should be directed to Joost Visser:
#   <Joost.Visser@cwi.nl>
# All remaining blame should be directed to Tobias Kuipers:
#   <kuipers@cwi.nl>

use strict;

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

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

# The name of the sdf file
my $sdffile = $ARGV[0];
my $moduleext = $ARGV[1];

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

# The name of the current module
my $modulename;

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 $_;
  }
}    
