#!/usr/bin/perl

if (!open( ENTRIES, "entrylist" ) ) {
  print "Can't open entrylist\n";
  exit 1;
}

  while(<ENTRIES>) {
    if (/^#/) { next; }
    chop;
    @entries = split /,/;
    if (!/^.+,(\w+),(\w+)/) { next; }
    push @entryCtrl, @entries[0];
    push @entryRealNames, @entries[1];
    push @entryTypes, @entries[2];
    push @entryNames, @entries[3];
    push @entryCategory, @entries[4];
    push @entryDebug, @entries[5];
  }

close ENTRIES;

if (!open( H_IN, "addressee.src.h" ) ) {
  print "Can't open addressee.src.h\n";
  exit 1;
}
if (!open( H_OUT, ">../addressee.h" ) ) {
  print "Can't open addressee.h\n";
  exit 1;
}
  print H_OUT "/*** Warning! This file has been generated by the script makeaddressee ***/\n";

  while( <H_IN> ) {
    if (/--DECLARATIONS--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] =~ /A/ ) {
          print H_OUT "    /**\n";
          print H_OUT "      Set $entryRealNames[$i].\n";
          print H_OUT "     */\n";
          print H_OUT "    void set" . ucfirst($entryNames[$i]);
          print H_OUT "( const $entryTypes[$i] &$entryNames[$i] );\n";

          print H_OUT "    /**\n";
          print H_OUT "      Return $entryRealNames[$i].\n";
          print H_OUT "     */\n";
          print H_OUT "    $entryTypes[$i] $entryNames[$i]() const;\n";
        }

        if ( $entryCtrl[$i] !~ /L/ ) { next; }
        print H_OUT "    /**\n";
        print H_OUT "      Return translated label for $entryNames[$i] field.\n";
        print H_OUT "     */\n";
        print H_OUT "    static QString $entryNames[$i]Label();\n\n";
      }
    } else {
      print H_OUT;
    }
  }

close H_OUT;
close H_IN;

if (!open( CPP_IN, "addressee.src.cpp" ) ) {
  print "Can't open addressee.src.cpp\n";
  exit 1;
}
if (!open( CPP_OUT, ">../addressee.cpp" ) ) {
  print "Can't open addressee.cpp\n";
  exit 1;
}
  print CPP_OUT "/*** Warning! This file has been generated by the script makeaddressee ***/\n";

  while( <CPP_IN> ) {
    if (/--VARIABLES--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /A/ ) { next; }
        print CPP_OUT "  $entryTypes[$i] $entryNames[$i];\n";
      }
    } elsif (/--DEFINITIONS--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] =~ /A/ ) {
          print CPP_OUT "void Addressee::set" . ucfirst($entryNames[$i]);
          print CPP_OUT "( const $entryTypes[$i] &$entryNames[$i] )\n{\n";        
          print CPP_OUT "  if ( $entryNames[$i] == mData->$entryNames[$i] ) return;\n";
          print CPP_OUT "  detach();\n  mData->empty = false;\n";
          print CPP_OUT "  mData->$entryNames[$i] = $entryNames[$i];\n}\n\n";
        
          print CPP_OUT "$entryTypes[$i] Addressee::$entryNames[$i]() const\n{\n";
          print CPP_OUT "  return mData->$entryNames[$i];\n}\n\n";
        }

        if ( $entryCtrl[$i] !~ /L/ ) { next; }
        @labelwords = split ' ', $entryRealNames[$i];
        for( $j=0; $j < @labelwords; ++$j ) {
          $labelwords[$j] = ucfirst $labelwords[$j];
        }
        $label = join ' ', @labelwords;
        print CPP_OUT "QString Addressee::$entryNames[$i]Label()\n{\n";
        print CPP_OUT "  return i18n(\"$label\");\n";
        print CPP_OUT "}\n\n\n";
      }
    } elsif (/--EQUALSTEST--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] =~ /E/ ) {
          print CPP_OUT "  if ( mData->$entryNames[$i] != a.mData->$entryNames[$i] ) return false;\n";
        }
      }        
    } elsif (/--STREAMOUT--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] =~ /A/ ) {
          print CPP_OUT "  s << a.mData->$entryNames[$i];\n";
        }
      }        
    } elsif (/--STREAMIN--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] =~ /A/ ) {
          print CPP_OUT "  s >> a.mData->$entryNames[$i];\n";
        }
      }        
    } elsif (/--DEBUG--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /A/ ) { next; }
        print CPP_OUT "  kdDebug(5700) << \"  " . ucfirst($entryNames[$i]);
        print CPP_OUT ": '\" << $entryNames[$i]()$entryDebug[$i] << \"'\" << endl;\n";
      }
    } else {
      print CPP_OUT;
    }
  }

close CPP_OUT;
close CPP_IN;

if (!open( CPP_IN, "field.src.cpp" ) ) {
  print "Can't open field.src.cpp\n";
  exit 1;
}
if (!open( CPP_OUT, ">../field.cpp" ) ) {
  print "Can't open field.cpp\n";
  exit 1;
}
  print CPP_OUT "/*** Warning! This file has been generated by the script makeaddressee ***/\n";

  while( <CPP_IN> ) {
    if (/--ENUMS--/) {
      $first = 1;
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /F/ ) { next; }
        if ( $first ) { $first = 0; }
        else { print CPP_OUT ",\n"; }
        print CPP_OUT "      " . ucfirst($entryNames[$i]);
      }
      print CPP_OUT "\n";
    } elsif (/--CASELABEL--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /F/ ) { next; }
        if ( $entryCtrl[$i] !~ /L/ ) { next; }
        print CPP_OUT "    case FieldImpl::" . ucfirst($entryNames[$i]) . ":\n";
        print CPP_OUT "      return Addressee::$entryNames[$i]Label();\n";
      }
    } elsif (/--CASEVALUE--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /A/ ) { next; }
        if ( $entryCtrl[$i] !~ /F/ ) { next; }
        if ( $entryTypes[$i] ne "QString" ) { next; }
        print CPP_OUT "    case FieldImpl::" . ucfirst($entryNames[$i]) . ":\n";
        print CPP_OUT "      return a.$entryNames[$i]();\n";
      }
    } elsif (/--CASESETVALUE--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /A/ ) { next; }
        if ( $entryCtrl[$i] !~ /F/ ) { next; }
        if ( $entryTypes[$i] ne "QString" ) { next; }
        print CPP_OUT "    case FieldImpl::" . ucfirst($entryNames[$i]) . ":\n";
        print CPP_OUT "      a.set" . ucfirst($entryNames[$i]) . "( value );\n";
        print CPP_OUT "      return true;\n";
      }
    } elsif (/--CREATEFIELDS--/) {
      for( $i=0; $i<@entryNames; ++$i ) {
        if ( $entryCtrl[$i] !~ /F/ ) { next; }
        print CPP_OUT "    createField( FieldImpl::" . ucfirst($entryNames[$i]);
        if ( $entryCategory[$i] ) {
          print CPP_OUT ", $entryCategory[$i]";
        }
        print CPP_OUT " );\n";
      }
    } else {
      print CPP_OUT;
    }
  }

close CPP_OUT;
close CPP_IN;
