/*ProfiPacket - packet radio terminal program
  Copyright (C) 1999  Alexander Feigl

  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

  Author:

  Alexander Feigl
  Burachstraße 51

  D-88250 Weingarten

  Mail : Alexander.Feigl@gmx.de
*/


#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef __GNUC__
#define _storage_text_ __attribute__ ((section(".text")))
#else
#define _storage_text_
#endif


#include "global.h"
#include "QSO.h"
#include "GUIProtos.h"

#ifdef AmigaOS

#include <exec/memory.h>
#include <dos/exall.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#ifdef __GNUC__
extern void *SysBase;
extern void *DOSBase;
#include <inline/exec.h>
#include <inline/dos.h>
#endif /*__GNUC__*/

#else

#include "amigaemu.h"

#endif /*AmigaOS*/


extern struct List ServerList;
extern struct List UserBaseList;
extern void MAIN_Init();
extern void MAIN_KillAllServers();
extern void MAIL_Cleanup();
extern void GUI_Cleanup();
extern void PACK_Cleanup();
extern void MAIN_Cleanup();
extern void QSO_Cleanup();
extern void AUDIO_Cleanup();


static struct StackSwapStruct stackswap;


int main(int argc,char **argv)
 {
  // stack initalization
#ifdef AmigaOS
  stackswap.stk_Lower=AllocVec(12384,0x1);
  if (stackswap.stk_Lower==NULL)
   {
    exit (103);
   }
  stackswap.stk_Upper=(ULONG) stackswap.stk_Lower+12384;
  stackswap.stk_Pointer=(APTR) stackswap.stk_Upper-4;
  StackSwap(&stackswap);
#endif AmigaOS

  RestartProgram=0;
  ServerList.lh_Head=(struct Node *) &ServerList.lh_Tail;
  ServerList.lh_Tail=NULL;
  ServerList.lh_TailPred=(struct Node *) &ServerList.lh_Head;

  UserBaseList.lh_Head=(struct Node *) &UserBaseList.lh_Tail;
  UserBaseList.lh_Tail=NULL;
  UserBaseList.lh_TailPred=(struct Node *) &UserBaseList.lh_Head;

  MAIN_Init();

  Clean_Up();
 }

void Clean_Up(void)
 {
  MAIN_KillAllServers();
  MAIL_Cleanup();
  GUI_Cleanup();
  PACK_Cleanup();
  MAIN_Cleanup();
  QSO_Cleanup();
  AUDIO_Cleanup();


#ifdef AmigaOS
  StackSwap(&stackswap);
  FreeVec(stackswap.stk_Lower);
#endif AmigaOS

  if (RestartProgram)
   {
    FileTransferWindow=0;
    FilterWindow=0;
    MainWindow=0;
    PrefWindow=0;
    LaunchWindow=0;
    SaveBufferWindow=0;
    ConnectWindow=0;
    UserSettingsWindow=0;
    ConnectMsgWindow=0;
    MessageWindow=0;
    BBSMainWindow=0;
    BBSSendWindow=0;
    BBSViewWindow=0;
    SpyWindow=0;
    MHeardWindow=0;
    MHeardLWindow=0;
    GetUserDataWindow=0;
    EncryptionWindow=0;
    main(0,NULL);
   }

  exit(0);
 }

