
# -*- perl -*-

# Copyright (C) 1998-2009 Yves Renard
#
# This file is a part of GETFEM++
#
# Getfem++  is  free software;  you  can  redistribute  it  and/or modify it
# under  the  terms  of the  GNU  Lesser General Public License as published
# by  the  Free Software Foundation;  either version 2.1 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 Lesser General Public
# License for more details.
# You  should  have received a copy of the GNU Lesser General Public License
# along  with  this program;  if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.


eval 'exec perl -S $0 "$@"'
  if 0;

$prefix = "wc";
$count = int(1000 * rand);
if ($ENV{TMPDIR} eq "") { $tmpdir = "/tmp"; } else { $tmpdir = $ENV{TMPDIR} }
if (substr($tmpdir, length($tmpdir)-1, 1) eq "/")
  { $tmpdir = substr($tmpdir, 0, length($tmpdir)-1); }
do { $tmp1 = $tmpdir."/".$prefix."\_$count"; ++$count; } while (-f $tmp1);


sub catch { `rm -f $tmp1`; }
$SIG{INT} = 'catch';

sub read_line {
  $comm = 0;
  while(1) {
    $li = <DATAF>; if (!($li)) { return; }
    chomp($li);

    if ($comm) {
      if ($li  =~ /\*\//) { ($a, $li) = split('\*\/', $li, 2); $comm = 0; }
      else { $li = ""; }
    }
    else {
      ($li, $a) = split('\/\/', $li, 2);
      if ($li =~ /\/\*/) {
	($li, $a) = split('\/\*', $li, 2);
	if (!($a  =~ /\*\//)) { $comm = 1; }
      }
    }
    $li2 = $li; $li2 =~s/\s//g;
    if ($li2) { return; }
  }
}

open(TMPF1, ">$tmp1") or die "Open file impossible : $!\n";

if ($ARGV[0]) { $lss = `ls $ARGV[0]`; }
else { $lss = `ls *.h *.cc`; }

while ($lss) {

  ($filename, $lss) = split('\n', $lss, 2);

  open(DATAF, $filename) or die "Open file impossible : $!\n";
  read_line;
  while ($li) {
    print TMPF1 $li, "\n";
    read_line;
  }
}

close(TMPF1);
print `wc $tmp1`;
`rm -f $tmp1`;
print "(usage : word_count or word_count \"???*.h\")\n";
