/*****************************************************************************
 *
 * Nom			: main.c
 *
 * version		: $VER: main.c 1.2 (11.04.98)
 *
 * History
 * V1.0: initial version
 * V1.1: can be started from WB with Tooltypes
 *       added notifyintuition routines to close the window automatically
 * v1.2: window can initialy sized thanks to tooltypes
 *       window goes to back every seconds
 *       first public release
 * v1.3: close|open workbench now works
 *       changed clock style
 * v2.0: Totally customizable via a GUI
 * v2.1: Transparent. Removed some unused code 
 *
 * future: customizable clock style
 *****************************************************************************
 */
#include <intuition/intuition.h>
#include <dos/dos.h>
#include <exec/ports.h>
#include <exec/libraries.h>
#include <exec/memory.h>
#include <workbench/startup.h>

#include <proto/intuition.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include "utils.h"
/* bump revision
 */
char  * nosense = "\0$VER: " TITLE " (13.03.98)" ;

void main() {
  /* lance du CLI
   */
  idWin * myprj ;
  if (exists() == FALSE) {
    Delay(50) ;
    if ((myprj = init(NULL)) != NULL) {
      //writeDate(myprj) ;
      processwin(myprj) ;
      close(myprj) ;
    }
    
  }

}
void wbmain() {
  /* lance du WB
   */
  void main() ;
  main() ;  
}
int exists() {
  int ret = FALSE ;
  struct EasyStruct EZ ;
  struct MsgPort * oldPort ;
  struct MsgPort * replyPort ; 
  struct backMsg * msg ;

  Forbid() ;
  oldPort = FindPort("backclock") ;
  Permit() ;
  
  if (oldPort) {
    /* le programme est deja lance
     */
    EZ.es_StructSize   = sizeof(struct EasyStruct) ;
    EZ.es_Flags        = NULL ;
    EZ.es_Title        = TITLE ;
    EZ.es_TextFormat   = "You are about to quit BackClock\nAre you sure ?" ;
    EZ.es_GadgetFormat = "Quit|Cancel" ;
    if (EasyRequestArgs(NULL, &EZ, NULL, NULL) == 1) {
      /* utilisateur choisit quit
       */  
      if ((replyPort = CreateMsgPort()) != NULL) {
        if ((msg = AllocVec(sizeof(struct backMsg), MEMF_PUBLIC)) != NULL) {
          msg->execmsg.mn_Node.ln_Type = NT_MESSAGE ;
          msg->execmsg.mn_Length       = sizeof(struct backMsg) ;
          msg->execmsg.mn_ReplyPort    = replyPort ;
          msg->Class = BC_Quit ;  // stop !!!
          PutMsg(oldPort, (struct Message*)msg) ;
          WaitPort(replyPort) ;
          DeleteMsgPort(replyPort) ;
          FreeVec(msg) ;
          ret=OK ;
        }else {
          ret=ERRORNOMEM ;
          DeleteMsgPort(replyPort) ;
        }
      }else ret=ERRORNOMEM ;
    }else ret = OK ;
  }
  return(ret) ;
}