#include <perms.h>
extern int Outputstream(void);
extern float Stor(_imp_string255 S);
extern _imp_string255 Rtos(float R, int A, int B);
extern void Readline(_imp_string255 S);
extern int /* boolean */ Qualifierpresent(_imp_string255 S);
_imp_string255 Productcode = _imp_str_literal("PLC");
_imp_string255 Fname = _imp_str_literal("PLC");
_imp_string255 Version = _imp_str_literal("2");
_imp_string255 Release = _imp_str_literal("11");
_imp_string255 Revision = _imp_str_literal("4");
extern void Fatalerror(_imp_string255 Message);
extern void Printidentification(void);
const int Summary = 0;
const int Detail = 1;
void Warningmessage(_imp_string255 Message) {
  int S = Outputstream;
  Selectoutput(0);
  Printstring(_imp_join(Fname, _imp_str_literal(": ")));
  Printstring(Message);
  Newline();
  Selectoutput(1);
  Printstring(_imp_join(Fname, _imp_str_literal(": ")));
  Printstring(Message);
  Newline();
  Selectoutput(S);
}
void Ps(_imp_string255 S) {
  int I;
  for (I = 1; I != Length(S) + 1; I += 1)
    if (Charno(S, I) >= ' ') Printsymbol(Charno(S, I));
}
void Printlimits(_imp_string255 S, _imp_string255 File, int Mode) {
  _imp_string255 A;
  _imp_string255 B;
  _imp_string255 Lxs;
  _imp_string255 Uxs;
  _imp_string255 Lys;
  _imp_string255 Uys;
  _imp_string255 Cxs;
  _imp_string255 Cys;
  double Cx;
  double Cy;
  if (_imp_on_event(3, 4, 9)) {
    Newline();
    return;
  }
  if (S == _imp_str_literal("") && File == _imp_str_literal("")) return;
  if (S == _imp_str_literal("")) {
    if (_imp_cond_resolve(File, NULL, _imp_str_literal(":"), File))
      ;
    if (_imp_cond_resolve(File, NULL, _imp_str_literal("]"), File))
      ;
    if (_imp_cond_resolve(File, File, _imp_str_literal(";"), NULL))
      ;
    if (Length(File) == 12)
      Printstring(_imp_str_literal(" from MEBES file"));
    else
      Printstring(_imp_str_literal(" default from input file"));
    Newline();
    return;
  } else if (_imp_cond_resolve(S, NULL, _imp_str_literal("EXT"), NULL)) {
    Printstring(_imp_str_literal(" extents from input file"));
    Newline();
    return;
  }
  Toupper(S);
  while (_imp_cond_resolve(S, A, _imp_str_literal("(INPUT "), B))
    S = _imp_join(A, B);
  while (_imp_cond_resolve(S, A, _imp_str_literal(")"), B)) S = _imp_join(A, B);
  while (_imp_cond_resolve(S, A, _imp_str_literal("  "), B))
    S = _imp_join(A, _imp_join(_imp_str_literal(" "), B));
  while (_imp_cond_resolve(S, A, _imp_str_literal(" "), B))
    S = _imp_join(A, _imp_join(_imp_str_literal(","), B));
  if (_imp_cond_resolve(S, Lxs, _imp_str_literal(","), A))
    ;
  if (_imp_cond_resolve(A, Lys, _imp_str_literal(","), A))
    ;
  if (_imp_cond_resolve(A, Uxs, _imp_str_literal(","), Uys))
    ;
  if (Mode == Detail)
    Spaces(10 - Length(Lxs));
  else
    Space();
  Printstring(Lxs);
  Printsymbol(',');
  Printstring(Lys);
  if (Mode == Detail)
    Spaces(20 - Length(Lys) - Length(Uxs));
  else
    Printstring(_imp_str_literal(" to "));
  Printstring(Uxs);
  Printsymbol(',');
  Printstring(Uys);
  Cx = ((Stor(Uxs) - Stor(Lxs)) / 2) + Stor(Lxs);
  Cy = ((Stor(Uys) - Stor(Lys)) / 2) + Stor(Lys);
  Cxs = Rtos(Cx, 0, 5);
  while (Charno(Cxs, Length(Cxs)) == '0') Length(Cxs) = Length(Cxs) - 1;
  if (Charno(Cxs, Length(Cxs)) == '.') Length(Cxs) = Length(Cxs) - 1;
  Cys = Rtos(Cy, 0, 5);
  while (Charno(Cys, Length(Cys)) == '0') Length(Cys) = Length(Cys) - 1;
  if (Charno(Cys, Length(Cys)) == '.') Length(Cys) = Length(Cys) - 1;
  if (Mode == Detail)
    Spaces(20 - Length(Uys) - Length(Cxs));
  else
    Printstring(_imp_str_literal(" centre "));
  Printstring(Cxs);
  Printsymbol(',');
  Printstring(Cys);
  Newline();
}
int _imp_mainep(int _imp_argc, char **_imp_argv) {
  const int Maxlayer = 1000;
  const int True = 1;
  const int False = 0;
  const int Primary = 0;
  const int Secondary = 1;
  const int Switch = 1;
  static _imp_string255 Gash;
  _imp_string255 Lastdirectory;
  _imp_string255 Thisdirectory;
  _imp_string255 Code;
  _imp_string255 Param;
  _imp_string255 File;
  _imp_string255 User = _imp_str_literal("");
  const int Eof = 0x1827A;
  const int Nsc = 0x10001;
  const int Nmf = 0x182CA;
  const int Topbit = 0x10000000;
  _imp_string255 Defnam;
  int Status;
  int Count;
  int Files;
  int Total;
  int Source;
  int Hadfunction = False;
  _imp_string255 Junk;
  _imp_string255 Es;
  _imp_string255 Rs;
  _imp_string255 Cincnames[1000];
  _imp_string255 Functions[1000];
  static _imp_string255 Resolutions[1000] = {[0:999] = _imp_str_literal("?")};
  static _imp_string255 Reverses[1000];
  _imp_string255 Grows[1000];
  _imp_string255 Oselects[1000];
  _imp_string255 Pselects[1000];
  _imp_string255 Pinputs[1000];
  _imp_string255 Plimits[1000];
  _imp_string255 Psizings[1000];
  _imp_string255 Pscalings[1000];
  _imp_string255 Porients[1000];
  _imp_string255 Players[1000];
  _imp_string255 Pstructures[1000];
  _imp_string255 Sselects[1000];
  _imp_string255 Sinputs[1000];
  _imp_string255 Slimits[1000];
  _imp_string255 Ssizings[1000];
  _imp_string255 Sscalings[1000];
  _imp_string255 Sorients[1000];
  _imp_string255 Slayers[1000];
  _imp_string255 Sstructures[1000];
  void Checkfile(void) {
    _imp_string255 Line;
    _imp_string255 Uline;
    _imp_string255 Cincname;
    if (_imp_on_event(9)) return;
    {
      if (_imp_on_event(9)) {
        Warningmessage(_imp_join(
            _imp_str_literal("Warning: Unable to open .cinc file "), Rs));
        _imp_signal(9, 0, 0, _imp_str_literal(""));
      }
      Openinput(1, Rs);
      Selectinput(1);
    }
    if (_imp_cond_resolve(Rs, NULL, _imp_str_literal("]"), Cincname))
      ;
    else if (_imp_cond_resolve(Rs, NULL, _imp_str_literal("/"), Cincname))
      ;
    else
      Cincname = Rs;
    if (!(_imp_cond_resolve(Cincname, Cincname, _imp_str_literal(".CINC"),
                            NULL) ||
          _imp_cond_resolve(Cincname, Cincname, _imp_str_literal(".cinc"),
                            NULL)))
      return;
    {
      if (_imp_on_event(9)) {
        Closeinput();
        if (Grows(Count) != _imp_str_literal("0")) {
          if (Psizings(Count) != _imp_str_literal("0"))
            if ((Stor(Grows(Count)) > 0 && Stor(Psizings(Count)) > 0) ||
                (Stor(Grows(Count)) < 0 && Stor(Psizings(Count)) < 0))
              if (Functions(Count) != _imp_str_literal("XOR")) {
                Selectoutput(0);
                Printstring(_imp_join(
                    _imp_str_literal("File "),
                    _imp_join(Cincname,
                              _imp_str_literal(" has both GROW and Primary "
                                               "SIZE in same direction"))));
                Newline();
                if (User == _imp_str_literal("")) {
                  Selectinput(0);
                  Prompt(_imp_str_literal(
                      "Enter your initials to confirm this is OK: "));
                  Readline(User);
                  Toupper(User);
                }
              }
          if (Ssizings(Count) != _imp_str_literal("0"))
            if ((Stor(Grows(Count)) > 0 && Stor(Ssizings(Count)) > 0) ||
                (Stor(Grows(Count)) < 0 && Stor(Ssizings(Count)) < 0)) {
              Selectoutput(0);
              Printstring(_imp_join(
                  _imp_str_literal("File "),
                  _imp_join(Cincname,
                            _imp_str_literal(" has both GROW and Secondary "
                                             "SIZE in same direction"))));
              Newline();
              if (User != _imp_str_literal("")) {
                Selectinput(0);
                Prompt(_imp_str_literal(
                    "Enter your initials to confirm this is OK: "));
                Readline(User);
                Toupper(User);
              }
            }
        }
        return;
      }
      Count = Count + 1;
      Cincnames(Count) = Cincname;
      Players(Count) = _imp_str_literal("");
      Psizings(Count) = _imp_str_literal("0");
      Porients(Count) = _imp_str_literal("0");
      Pscalings(Count) = _imp_str_literal("1");
      Players(Count) = _imp_str_literal("");
      Pinputs(Count) = _imp_str_literal("");
      Plimits(Count) = _imp_str_literal("");
      Pstructures(Count) = _imp_str_literal("");
      Ssizings(Count) = _imp_str_literal("0");
      Sorients(Count) = _imp_str_literal("0");
      Sscalings(Count) = _imp_str_literal("1");
      Slayers(Count) = _imp_str_literal("");
      Sinputs(Count) = _imp_str_literal("");
      Sstructures(Count) = _imp_str_literal("");
      Slimits(Count) = _imp_str_literal("");
      Functions(Count) = _imp_str_literal("");
      Grows(Count) = _imp_str_literal("0");
      Oselects(Count) = _imp_str_literal("no");
      Pselects(Count) = _imp_str_literal("no");
      Sselects(Count) = _imp_str_literal("no");
      Reverses(Count) = _imp_str_literal("no");
      Source = Primary;
      for (;;) {
        Readline(Line);
        if (Length(Line) <= 3) continue;
        if (Substring(Line, 1, 3) == _imp_str_literal("! #")) {
          Count = Count - 1;
          return;
        }
        Uline = Line;
        Toupper(Uline);
        Code = Substring(Uline, 1, 3);
        if (_imp_cond_resolve(Line, NULL, _imp_str_literal(" "), Param))
          ;
        if (Code == _imp_str_literal("SWI"))
          Source = Source ^ Switch;
        else if (Code == _imp_str_literal("INP"))
          if (Source == Primary) {
            Gash = _imp_str_literal("");
            if (_imp_cond_resolve(Param, NULL, _imp_str_literal(":"), Gash))
              ;
            else if (_imp_cond_resolve(Param, NULL, _imp_str_literal("]"),
                                       Gash))
              ;
            else if (_imp_cond_resolve(Param, NULL, _imp_str_literal("/"),
                                       Gash))
              ;
            else
              Gash = Param;
            Pinputs(Count) = Gash;
          } else {
            if (_imp_cond_resolve(Param, NULL, _imp_str_literal(":"), Gash))
              ;
            else if (_imp_cond_resolve(Line, NULL, _imp_str_literal("]"), Gash))
              ;
            else if (_imp_cond_resolve(Line, NULL, _imp_str_literal("/"), Gash))
              ;
            else
              Gash = Param;
            Sinputs(Count) = Gash;
          }
        else if (Code == _imp_str_literal("LIM"))
          if (Source == Primary)
            Plimits(Count) = Param;
          else
            Slimits(Count) = Param;
        else if (Code == _imp_str_literal("FUN")) {
          Functions(Count) = Param;
          Hadfunction = True;
        } else if (Code == _imp_str_literal("RES"))
          Resolutions(Count) = Param;
        else if (Code == _imp_str_literal("GRO"))
          Grows(Count) = Param;
        else if (Code == _imp_str_literal("SIZ"))
          if (Source == Primary)
            Psizings(Count) = Param;
          else
            Ssizings(Count) = Param;
        else if (Code == _imp_str_literal("SCA"))
          if (Source == Primary)
            Pscalings(Count) = Param;
          else
            Sscalings(Count) = Param;
        else if (Code == _imp_str_literal("ORI"))
          if (Source == Primary)
            Porients(Count) = Param;
          else
            Sorients(Count) = Param;
        else if (Code == _imp_str_literal("REV"))
          Reverses(Count) = Param;
        else if (Code == _imp_str_literal("SEL"))
          if (_imp_cond_resolve(Uline, NULL, _imp_str_literal(" OUT"), NULL))
            Oselects(Count) = _imp_str_literal("YES");
          else if (Source == Primary)
            Pselects(Count) = _imp_str_literal("YES");
          else
            Sselects(Count) = _imp_str_literal("YES");
        else if (Code == _imp_str_literal("LAY"))
          if (Source == Primary)
            Players(Count) = Param;
          else
            Slayers(Count) = Param;
        else if (Code == _imp_str_literal("STR"))
          if (Source == Primary)
            Pstructures(Count) = Param;
          else
            Sstructures(Count) = Param;
      }
    }
  }
  void Outputinformation(void) {
    int I;
    int Different;
    if (User != _imp_str_literal("")) {
      Printstring(_imp_join(
          _imp_str_literal("User "),
          _imp_join(User, _imp_str_literal(
                              " confirmed that same sign sizing was OK"))));
      Newline();
    }
    Newline();
    if (Count > 1) {
      Different = False;
      for (I = 2; I != Count + 1; I += 1)
        if (Pinputs(I) != Pinputs(1) || Pstructures(I) != Pstructures(1))
          Different = True;
    } else
      Different = True;
    if (Different == False) {
      Ps(_imp_join(_imp_str_literal("All primary   files input from \""),
                   _imp_join(Pinputs(1), _imp_str_literal("\""))));
      if (Pstructures(1) != _imp_str_literal(""))
        Ps(_imp_join(_imp_str_literal(" with structure \""),
                     _imp_join(Pstructures(1), _imp_str_literal("\""))));
      Newline();
    } else {
      Printstring(_imp_str_literal(
          "Filename       Primary Input                  Structure"));
      Newline();
      for (I = 1; I != Count + 1; I += 1) {
        Ps(Cincnames(I));
        Spaces(15 - Length(Cincnames(I)));
        Ps(Pinputs(I));
        Space();
        Spaces(30 - Length(Pinputs(I)));
        Ps(Pstructures(I));
        Newline();
      }
    }
    if (Hadfunction == True) {
      Newline();
      if (Count > 1) {
        Different = False;
        for (I = 2; I != Count + 1; I += 1)
          if (Sinputs(I) != Sinputs(1) || Sstructures(I) != Sstructures(1))
            Different = True;
      } else
        Different = True;
      if (Different == False) {
        Ps(_imp_join(_imp_str_literal("All secondary files input from \""),
                     _imp_join(Sinputs(1), _imp_str_literal("\""))));
        if (Sstructures(1) != _imp_str_literal(""))
          Ps(_imp_join(_imp_str_literal(" with structure \""),
                       _imp_join(Sstructures(1), _imp_str_literal("\""))));
        Newline();
      } else {
        Ps(_imp_str_literal(
            "Filename       Secondary Input                Structure"));
        Newline();
        for (I = 1; I != Count + 1; I += 1) {
          if (Functions(I) == _imp_str_literal("")) continue;
          Ps(Cincnames(I));
          Spaces(15 - Length(Cincnames(I)));
          Ps(Sinputs(I));
          Space();
          Spaces(30 - Length(Sinputs(I)));
          Ps(Sstructures(I));
          Newline();
        }
      }
    }
    Newline();
    if (Count > 1) {
      Different = False;
      for (I = 2; I != Count + 1; I += 1)
        if (Plimits(I) != Plimits(1)) Different = True;
    } else
      Different = True;
    if (Different == False) {
      Ps(_imp_str_literal("All primary   files have LIMITS "));
      Printlimits(Plimits(1), Pinputs(1), Summary);
    } else {
      Ps(
          _imp_str_literal("Filename       Primary Limits   LX,LY       UX,UY  "
                           "              CX,CY"));
      Newline();
      for (I = 1; I != Count + 1; I += 1) {
        Ps(Cincnames(I));
        Spaces(15 - Length(Cincnames(I)));
        Printlimits(Plimits(I), Pinputs(I), Detail);
      }
    }
    if (Hadfunction == True) {
      Newline();
      if (Count > 1) {
        Different = False;
        for (I = 2; I != Count + 1; I += 1)
          if (Slimits(I) != Slimits(1) || Sinputs(I) != Sinputs(1))
            Different = True;
      } else
        Different = True;
      if (Different == False) {
        Ps(_imp_str_literal("All secondary files have LIMITS "));
        Printlimits(Slimits(1), Sinputs(1), Summary);
      } else {
        Ps(
            _imp_str_literal("Filename       Secondary Limits   LX,LY     "
                             "UX,UY                CX,CY"));
        Newline();
        for (I = 1; I != Count + 1; I += 1) {
          if (Slimits(I) == _imp_str_literal("") &&
              Sinputs(I) == _imp_str_literal(""))
            continue;
          Ps(Cincnames(I));
          Spaces(15 - Length(Cincnames(I)));
          Printlimits(Slimits(I), Sinputs(I), Detail);
        }
      }
    }
    Newline();
    Ps(
        _imp_str_literal("Filename       Layers         Resol'n  Grow Sizing "
                         "Scaling Orient Reverse Select"));
    Newline();
    for (I = 1; I != Count + 1; I += 1) {
      Ps(Cincnames(I));
      Spaces(15 - Length(Cincnames(I)));
      if (Players(I) == _imp_str_literal(""))
        Players(I) = _imp_str_literal("CFLT");
      Ps(Players(I));
      Space();
      Spaces(14 - Length(Players(I)));
      Junk = Resolutions(I);
      if (_imp_cond_resolve(Junk, NULL, _imp_str_literal("."), NULL)) {
        while (Charno(Junk, Length(Junk)) == '0')
          Length(Junk) = Length(Junk) - 1;
        if (Charno(Junk, Length(Junk)) == '0')
          Junk = _imp_join(Junk, _imp_str_literal("0"));
      }
      Ps(Junk);
      Space();
      Spaces(12 - Length(Junk) - Length(Grows(I)));
      Ps(Grows(I));
      Space();
      Junk = Psizings(I);
      if (_imp_cond_resolve(Junk, NULL, _imp_str_literal("."), NULL)) {
        while (Charno(Junk, Length(Junk)) == '0')
          Length(Junk) = Length(Junk) - 1;
        if (Charno(Junk, Length(Junk)) == '0')
          Junk = _imp_join(Junk, _imp_str_literal("0"));
      }
      Spaces(6 - Length(Junk));
      Ps(Junk);
      Space();
      Spaces(7 - Length(Pscalings(I)));
      Ps(Pscalings(I));
      Space();
      Spaces(6 - Length(Porients(I)));
      Ps(Porients(I));
      Spaces(4);
      Ps(Reverses(I));
      Space();
      Spaces(6 - Length(Reverses(I)));
      Ps(Pselects(I));
      Newline();
      if (Functions(I) != _imp_str_literal("")) {
        Ps(_imp_str_literal("   fun "));
        Ps(Functions(I));
        Spaces(8 - Length(Functions(I)));
        if (Slayers(I) == _imp_str_literal(""))
          Slayers(I) = _imp_str_literal("CFLT");
        Ps(Slayers(I));
        Space();
        Junk = Ssizings(I);
        if (_imp_cond_resolve(Junk, NULL, _imp_str_literal("."), NULL)) {
          while (Charno(Junk, Length(Junk)) == '0')
            Length(Junk) = Length(Junk) - 1;
          if (Charno(Junk, Length(Junk)) == '0')
            Junk = _imp_join(Junk, _imp_str_literal("0"));
        }
        Spaces(34 - Length(Slayers(I)) - Length(Junk));
        Ps(Junk);
        Space();
        Spaces(7 - Length(Sscalings(I)));
        Ps(Sscalings(I));
        Space();
        Spaces(6 - Length(Sorients(I)));
        Ps(Sorients(I));
        Spaces(11);
        Ps(Sselects(I));
        Newline();
      }
    }
  }
  if (Qualifierpresent(_imp_str_literal("IDENTIFY"))) {
    Printidentification();
    if (!Qualifierpresent(_imp_str_literal("FILE"))) return;
  }
  Count = 0;
  Files = 0;
  for (;;) {
    if (_imp_cond_resolve(Rs, NULL, _imp_str_literal("CATS_START.CINC"),
                          NULL) ||
        _imp_cond_resolve(Rs, NULL, _imp_str_literal("cats_start.cinc"), NULL))
      continue;
    Files = Files + 1;
    Checkfile();
  }
  Outputinformation();
  {
    if (_imp_on_event(9))
      Fatalerror(_imp_str_literal(
          "Unable to open file PLC.LOG in the current directory"));
    Openoutput(3, _imp_str_literal("plc.log"));
  }
  Selectoutput(3);
  Outputinformation();
  Closeoutput();
  exit(0);
  return (1);
}
