/* 
   Riscos SMB/Netbios implementation.
  
   Copyright (C) David Buxton 1998
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/


/******************************************************************************
 *
 * RCS ID
 * $Id: main,v 1.9 2000/04/23 16:48:31 david Exp $
 *
 * HISTORY
 * $Log: main,v $
 * Revision 1.9  2000/04/23 16:48:31  david
 * Changes to attempt fix for copying large files from Windows to
 * RISCOS problem. Fixes failed!
 *
 * Revision 1.8  2000/04/22 14:50:48  david
 * Updates for nmbd module debugging.
 * Updates for integrating nmbd with main smbserver application
 *
 * Revision 1.7  2000/04/17 16:46:01  david
 * updates after testing event poll word non zero stuff
 *
 * Revision 1.6  2000/04/02 16:57:07  david
 * Updates to use the Internet event, use wimp poll event,
 * use non blocking sockets correctly,
 * compile libs in format suitable for RISCOS module
 * new nmbd module
 *
 * Revision 1.5  2000/01/11 20:14:39  david
 * Fixed Disk Size and Free Space reporting.
 * Fixed Workgroup name reporting
 * Upped version to 0.05a
 *
 * Revision 1.4  1999/11/07 15:11:52  david
 * version:updated!
 * wimp:no longer used -> now using toolbox lib instead
 *
 * Revision 1.3  1999/07/20 21:06:00  david
 * calls to ftypes_exit and changed name oif
 * main to RiscosMain
 *
 * Revision 1.2  1999/06/20 13:39:03  david
 * Added stuff to read command line parameters and decide
 * baricon position
 *
 * Revision 1.1  1999/05/09 14:58:46  david
 * Initial revision
 *
 *
 *****************************************************************************/                    
#include <signal.h>
#include "includes.h"
              
#define LEFT_BARICON  0
#define RIGHT_BARICON 1
#define NO_BARICON    2

#define NO_SIG_HANDLERS 8

typedef void (*pSigHandler)(int);

extern void wimp_werr(int,char*);
extern void wimp_process(void);  

extern int bariconPos;
 
pSigHandler RiscosSigHandler[NO_SIG_HANDLERS] =
  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};

void SigHandler(int sig)
{             
  nmbd_stop(False);
  closesockets();
  file_close_all();
  close_malloc(); 
          
  if (sig<NO_SIG_HANDLERS &&
      RiscosSigHandler[sig]!=NULL)
  {
    (*RiscosSigHandler[sig])(sig);
  }
              
}


char *fatal_errors[NO_ERRORS] =
{
   "null",
   "Malloc failed",
   "Calloc failed",
   "Realloc failed"
};

void samba_shutdown(int severity)
{
              
  nmbd_stop(False);
  closesockets();
  file_close_all();
  close_malloc();
  ftypes_exit(); 

  if (severity>0)
  {
   
    if (severity<NO_ERRORS)
    {
      wimp_werr(0,fatal_errors[severity]);
    }                                
    else
    {
      wimp_werr(0,"Samba has suffered a fatal error");
    }

    exit(0);
  }
}

void myexit(void)
{ 
  nmbd_stop(False);
  closesockets();
  file_close_all();
  close_malloc();
  ftypes_exit();  
}                                  

void process_arguements(int argc, char *argv[])
{
  int i;

  if (argc>1)
  {
    for (i=1;i<argc;i++)
    {
         
      if (strcmp(argv[i],"-nobaricon")==0)
      {
         bariconPos=NO_BARICON;
      }                        
         
      if (strcmp(argv[i],"-rightbaricon")==0 &&
          bariconPos!=NO_BARICON)
      {
         bariconPos=RIGHT_BARICON;
      }        
    }

  }
}

int svrTest(tpClients aclient, char *inbuf, char *outbuf,
            int bytes_recv, int bufsize)
{
  return 0;
}
 
extern BOOL doInputs;
extern BOOL doOutputs;
extern BOOL isNullEvent;
extern BOOL callAgain;
extern BOOL didSomething;

int pollWordNonZeroAction(void)
{  
  isNullEvent=False;
  doInputs=True;
  doOutputs=True;
/*
  do_select();  
  if (callAgain==True)
    do_select();  
*/
  do
  {
    do_select();
  } while (callAgain==True);   
  return 1;
}
 
int doNullEvent(void)
{                   
  isNullEvent=True;
  doInputs=True;
  doOutputs=True;
  /*do_select();*/
  return (int)didSomething;
}              

/******************************* MAIN PROGRAM ******************************/

int RiscosMain(int argc, char *argv[])
{
    init_debugf("<samba$dir>.Resources.ALogFile");

    process_arguements(argc, argv);

    init_malloc();

    /* --- initialise the environment --- */
#ifdef NO_TOOLBOX_LIB
    if (smbserver_initialise()==1)
#else
    if (WimpInitialise("<smbserver$dir>","SmbServer")==1)
#endif
    {

        RiscosSigHandler[SIGABRT] = signal(SIGABRT, SigHandler);
        RiscosSigHandler[SIGFPE]  = signal(SIGFPE,  SigHandler);
        RiscosSigHandler[SIGILL]  = signal(SIGILL,  SigHandler);
        RiscosSigHandler[SIGSEGV] = signal(SIGSEGV, SigHandler);
        RiscosSigHandler[SIGTERM] = signal(SIGTERM, SigHandler);

        init_server();

        samba_init();
        nmbd_init();
                  
        atexit(myexit);

        restoreDirs();

        /* --- the main event loop --- */   
        while(True)
        {                   
#ifdef NO_TOOLBOX_LIB
            wimp_process();
#else 
            WimpPoll();                   
#endif
         /*   do_select(); */
        } 
    }
#ifndef NO_TOOLBOX_LIB
    else
    {
        wimpOnlyOneError();

        while (True)
        {
           WimpPoll();
        }
    }                 
#endif

    return 0;
}
