#usage "<b>Rename SIGNALSs in a board with Suffix</b><p>"
       "RUN rename-signals [suffix]<br>"
       "<author>Author: support@cadsoft.de</author>";

// THIS PROGRAM IS PROVIDED AS IS AND WITHOUT WARRANTY OF ANY KIND,
// EXPRESSED OR IMPLIED.


string Version = "Version 1.1"; // check if schematic editor window open

string fileName;

string suffix = argv[1];

// ### Functions ###


if (project.schematic) {
  dlgMessageBox(argv[0]+ "\n" + Version + "\nFirst close the schematic editor!", "OK");
  exit(-1);
}

// ** MAIN **
if (board) {
  board(B) {
    fileName = filesetext(B.name, "rename-signal-suffix~~~.scr");
    int cntsig = 0;
    output(fileName, "wtD") {
      printf("# 'This file is generated by %s, exported from;\n", Version);
      printf("# '%s at %s;\n", fileName, t2string(time()));
      printf("# '%s;\n\n", EAGLE_SIGNATURE);
      B.signals(S) {
        S.contactrefs(C) {
          printf("SIGNAL 'S$S_0%d%s' %s %s;\n", cntsig, suffix, C.element.name, C.contact.name);
          cntsig++;
          break;
        }
      }
    }
    exit ("SCRIPT '" + fileName + "';");
  }
}
else dlgMessageBox("Start this ULP in Board!", "OK");
