/*
 * Run by the OPen command in main menu. Remember to recompile!
 */

#include <stdlib.bzh>

int logdoor(int door) {
  int f;

  if ((f=fopen(bl_logfile,"at"))!=-1) {
    fprintf(f,"%02u:%02u Opened door %u\n",bf_hour,bf_min,door);
    fclose(f);
  }
}

int make_dorinfo1def(char fn) {
  char nam1, nam2, sys1, sys2;
  int f, security, foo;

  if ((f=fopen(fn,"wt"))==-1) {
    printf("No way, %s, man!",fn);
    return(1);
  }
  if (bv_unum==0) security=32000;
  else security=1;
  nam1=copy(bu_name,1,pos(" ",bu_name)-1);
  nam2=copy(bu_name,pos(" ",bu_name)+1,30);
  sys1=copy(bg_sysop_name,1,pos(" ",bg_sysop_name)-1);
  sys2=copy(bg_sysop_name,pos(" ",bg_sysop_name)+1,30);
  if (!bv_com) foo=0;
  else foo=bv_baud;
//             1   2   3      4   5              6  7   8   9  10  11  12
  fprintf(f,"%s\n%s\n%s\nCOM%u\n%u BAUD,N,8,1\n%u\n%s\n%s\n%s\n%u\n%u\n%d\n",
      bg_bbbs_name,                  // 1    - BBS name
      sys1,                          // 2    - SysOp first name
      sys2,                          // 3    - SysOp last name
      bv_com,                        // 4    - COM port
      foo,                           // 5    - Baud rate
      foo,                           // 6    - Baud, again
      nam1,                          // 7    - User first name
      nam2,                          // 8    - User last name
      bu_city,                       // 9    - User location
      (bu_termcap & 15)>0,           // 10   - ANSI (1/0)
      security,                      // 11   - user security
      bf_timleft);                   // 12   - Time left
  fclose(f);
  return(0);
}

int make_doorsys(char fn) {
  int f;

  if ((f=fopen(fn,"wt"))!=-1) {
    fprintf(f,"%s\n%u\n%u\n%d\nG\nA\n",bu_name,bv_com,
      bv_realbaud,bf_timleft);
    fclose(f);
  }
}

int main() {
  char b;
  int z, n;

  if ((n=bn_zstatus)==1) bn_zstatus=8;
  if (bv_comstring=="") showfile(strcat(bg_menudir,"doormenu"));
  do {
    switch (z=parsecom("Quit/?/1/2/3/4/5",b=input("Number of door to open: ",60,1))) {
       case 2:{
                showfile(strcat(bg_menudir,"doormenu"));
                break;
              }
       case 3:{
                if (yellsysop(0)) {
                  logdoor(1);
                  tictactoe(1,0);
                }
                break;
              }
       case 4:{
                if (yellsysop(0)) {
                  tictactoe(2,1);
                  logdoor(2);
                }
                break;
              }
       case 5:{
                if (yellsysop(0)) {
                  tictactoe(2,2);
                  logdoor(3);
                }
                break;
              }
       case 6:{
                if (yellsysop(0)) {
                  logdoor(4);
                  make_dorinfo1def(sprintf("dorinfo%u.def",bv_nodenumber));
                  make_doorsys(sprintf("door%u.sys",bv_nodenumber));
                  make_pcboard12sys(sprintf("pcb%u.s12",bv_nodenumber));
                  make_pcboard14sys(sprintf("pcb%u.s14",bv_nodenumber));
                  system("lw2 s3 duel",1);
                }
                break;
              }
       case 7:{
                logdoor(5);
                spawn("mastermind");
                break;
              }
     case 255:{
                printf(bv_txt[23],b);
                bv_comstring="";
                break;
              }
    }
  } while (bv_carrier && (z==2 || z==255));
  bn_zstatus=n;
}

