#! /usr/bin/env perl

# "my"֥롼֤Τߡפ饢Ǥѿǡ
# ʸˡŪʰ̣Ǥ
# "local"֥롼ȡΥ֥롼󤬸Ƥ
# ֥롼󤫤饢Ǥѿǡ̿Ūʰ̣
# Ǥ

sub inserted() {

}

sub getfile() {
  my $c=$_[0];
  my $b = `basename $c`;
  chop($b);
  return $b;
}

sub getcategory() {
  my $c=$_[0];
  my $d = `dirname $c`;
  my $b = `basename $c`;
  chop($d);
  chop($b);

  my @ds=split(/\//,$d);

  my $dd=$ds[$#ds];
  my $cat;

  if ( $dd eq "run" ) {
    $cat = "RunCategory";
  } elsif ( $dd eq "event" ) {
    $cat = "EventCategory";
  } elsif ( $dd eq "interfaces" ) {
    $cat = "InterfacesCategory";
  } elsif ( $dd eq "modules" ) {
    $cat = "ModulesCategory";
  } elsif ( $dd eq "readout" ) {
    $cat = "ReadoutCategory";
  } elsif ( $dd eq "datarecord" ) {
    $cat = "DatarecordCategory";
  } elsif ( $dd eq "persistency" ) {
    $cat = "PersistencyCategory";
  } elsif ( $dd eq "analysis" ) {
    $cat = "AnalysisCategory";
  } elsif ( $dd eq "commands" ) {
    $cat = "CommandsCategory";
  } elsif ( $dd eq "system" ) {
    $cat = "SystemCategory";
  } elsif ( $dd eq "geometry" ) {
    $cat = "GeometryCategory";
  } elsif ( $dd eq "random" ) {
    $cat = "RandomCategory";
  } else {
    die "ʤ,ƥ꤭ʤȤ\n";
  }

  return $cat;
}

sub getclass() {
  my $c=$_[0];
  my $d = `dirname $c`;
  my $b = `basename $c`;
  chop($d);
  chop($b);
  my @a=split(/\./,$b);

  return $a[0];
}

sub filecomment1() {

  my $file = $_[0];
  my $class = $_[1];
  my $category = $_[2];
  my $out = $_[3];

  print($out "/\*!\n");
  print($out "//\n");
  print($out "//  \\file $file\n");
  print($out "//  \\brief This file contains the $class class\.\n");
  print($out "//  \\author Go IWAI <goiwai\@users.sourceforge.jp>\n");
  print($out "//\n");

}

sub filecomment2() {
  my $out = $_[0];
  print($out "\*/\n");
}

sub classcomment() {
  my $file = $_[0];
  my $class = $_[1];
  my $category = $_[2];
  my $out = $_[3];

  print($out "\n");
  print($out "/\*!\n");
  print($out "// \\example\n");
  print($out "//\n");
  print($out "// If example code exists\.\n");
  print($out "//\n");
  print($out "\*/\n");
  print($out "\n");
  print($out "/\*!\n");
  print($out "// \\class $class\n");
  print($out "// \\brief This is the $class class\.\n");
  print($out "// \\author Go IWAI <goiwai\@users.sourceforge.jp>\n");
  print($out "//\n");
  print($out "// The $class class detail description\.\n");
  print($out "// Write it down ASAP\!\n");
  print($out "//\n");
  print($out "// \\todo None\?\n");
  print($out "// \\bug None\?\n");
  print($out "// \\ingroup $category\n");
  print($out "\*/\n");

}

sub main() {
  foreach $f (@ARGV) {

    my $file=&getfile($f);
    my $class=&getclass($f);
    my $category=&getcategory($f);

    my $tmp="$f.tmp";

    open(OUT,"> $tmp");
    open(IN,$f);
    my $matchcnt = 0;
    while(<IN>){
      my $ll=$_;
      chop($ll);
      if ( $ll =~ /^class $class/ || $ll =~ /^typedef.+$class;$/ ) {	
	&classcomment($file,$class,$category,OUT);
	print(OUT "$ll\n");
      } elsif ( $ll =~ /^\/\/\s+=+/ ) {
	if ( $matchcnt == 0 ) {
	  print(OUT "$ll\n");
	  &filecomment1($file,$class,$category,OUT);
	  $matchcnt ++;
	} elsif ( $matchcnt == 1 ) {
	  &filecomment2(OUT);
	  print(OUT "$ll\n");
	  $matchcnt ++;
	} else {
	  print "եƬȤ2֥åʾ夢褦˸ޤ\n";
	}
      } else {
	print(OUT "$ll\n");
      }
    }
    close(IN);
    close(OUT);

    system("mv $tmp $f");
  }
}

&main();

1;
