#include <exec/types.h>
#include <exec/exec.h>
#include <stdio.h>
#include <stdlib.h>
#include "work:romconf/doorheader.h"
#define gu getuserstring
#define pm prompt
#define hk hotkey
#define sm sendmessage
char cmd[200];
void showbull(char *str);
void end();
main(int argc, char *argv[])
{
  FILE *fi;
  char respond[10];
  char filename[200];
  char nodelocal[200];
  char conflocal[200];
  char bbsloc[50];
  int Bconf=0,Bnode=0;
  int node;
  Register(argv[1][0]-'0');
  node = argv[1][0]-'0';
  gu(cmd,BB_CONFLOCAL);
  gu(bbsloc,BB_LOCAL);

  sprintf(filename,"%sNode%d/Bulletins/",bbsloc,node);
  strcpy(nodelocal,filename);

  sprintf(filename,"%sBulletins/",cmd);
  strcpy(conflocal,filename);

  strcpy(filename,nodelocal);
  strcat(filename,"bullhelp.txt");
  fi=fopen(filename,"r");
  if(fi!=NULL) { fclose(fi); Bnode=1; }

  strcpy(filename,conflocal);
  strcat(filename,"bullhelp.txt");
  fi=fopen(filename,"r");
  if(fi!=NULL) {  Bconf=1; fclose(fi); }
  sm("",1);
  while(1)
  {
    if(Bnode==1 && Bconf==1)
    {
      sm("[36mPlease enter bulletin type [33mN[36m)ode or [33mC[36m)onference or [32m{return=exit}[36m>:",0);
      hk("",cmd);
      if(cmd[0]<' ') { sm("[0m",1);sm("",1);ShutDown(); end(); }
      if(cmd[0]=='N' || cmd[0]=='n')
      {
         sm("[34mNode.[0m",1);
         showbull(nodelocal);
      }
      if(cmd[0]=='C' || cmd[0]=='c')
      {
         sm("[34mconference.[0m",1);
         showbull(conflocal);
      }
    }
    if(Bnode==0 && Bconf==1)
    {
      showbull(conflocal);
    }
    if(Bnode==1 && Bconf==0)
    {
      showbull(nodelocal);
    }
    if(Bnode==0 && Bconf==0)
    {
      sm("[0mSorry, we have no bulletins[0m",1);
      sm("",1);
      ShutDown(); end();
    }
   }
}

void showbull(char *str)
{
  char string[200];
  char cmd[200];
  FILE *fi;
  int num;
  strcpy(string,str);
  strcat(string,"BullHelp");
  showgfile(string);
  
  while(1)
  {
    sm("[0m",1);
    sm("[34mType '?' [33m{[36mRETURN[33m}[34m to view menu[0m",1);
    pm("[36mEnter bulletin number you wish to view or [33m{[32mRETURN[33m to exit}[0m >:",cmd,5);
    num=atoi(cmd);
    if(cmd[0]=='?')
    {
      strcpy(string,str);strcat(string,"BullHelp");
      showgfile(string);
      continue;
    }
    sm("[0m",1);
    if(num==0){ sm("[0m",1); ShutDown(); end(); }
    sprintf(string,"%sBull%d.txt",str,num);
    fi=fopen(string,"r");
    if(fi==NULL)
    {
      sm("",1);
      sm("[33mSorry, can't locate that Bulletin[0m",1);
      continue;
    }
    fclose(fi);
    sprintf(string,"%sBull%d",str,num);
    showgfile(string);
  }
  
}
  
void end()
{
  exit(0);
}