/*****************************************************************************
 *
 * Nom      : main.c
 *
 * version    : $VER: main.c 1.25 (15.08.99)
 *
 * History of backclock
 * 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 (no tooltypes anymore)
 * v2.1: Transparent. Removed some unused code 
 * v2.32b: redraw window when (de)selected 
 *         uses rtracker.library
 * v2.32c: now stay in front of Workbench or Directory Opus
 *         last minute : now set task priority to -20
 *
 * v3.0  : customizable clock style !!!
 * v3.1  : shadow pos & len configurable
 *         can display time with leds
 * v3.2  : color leds customisable
 * v3.21 : can open on any specified public screen
 *         Backclock is now a commodity
 *         (problem with notify still not resolved)
 * v3.22 : small bugfix
 *****************************************************************************
 */
#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 <workbench/workbench.h>

#include <proto/icon.h>
#include <proto/intuition.h>
#include <proto/exec.h>
#include <proto/dos.h>

#include <stdlib.h>

#include "utils.h"
/* version string
 */
char  * nosense = VERSION_STRING ;
extern struct Library * IconBase ;
#ifdef __VBCC__ // VBCC doesn't open base libraries automatically
extern struct Library *IntuitionBase ;
#endif

void main(int argc, char **argv) {
  /* protos
   */
  int exists(void) ;
  void StartupWait(void) ;
  /* launched from CLI
   */
  idWin * myprj ;
  if (exists() == FALSE) {
    SetTaskPri(FindTask(NULL), -20) ; // for those who don't use executive
    StartupWait() ;
    if ((myprj = init(argc, argv)) != NULL) {
      processwin(myprj) ;
      close(myprj) ;
    }    
  }
}

int exists() {
  /* backclock already launched
   */
  int ret = FALSE ;
  struct EasyStruct EZ ;
  struct MsgPort * oldPort ;
  struct MsgPort * replyPort ; 
  struct backMsg * msg ;

  Forbid() ;
  oldPort = FindPort("backclock") ;
  Permit() ;
  
  if (oldPort) {
    #ifdef __VBCC__
    if(IntuitionBase=OpenLibrary("intuition.library", 37L))
    #endif
      /* old port exists
       */
      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) {
        /* user choosed quit
         */
        if ((replyPort = CreateMsgPort()) != NULL) {
          if ((msg = AllocVec(sizeof(struct backMsg), MEMF_PUBLIC)) != NULL) {
            /* send a quit message to backclock
             */
            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 ;
    #ifdef __VBCC__
    }else
      puts("Closing down backclock: no intuition") ;
    #endif
  }
  return(ret) ;
}


void StartupWait() {
  /* open the backclock icon
   * read the tooltype for : wait
   * and wait the selected time
   */
  struct DiskObject * backicon ;
  char * ptime ;
  ULONG time ;

  if ((IconBase = OpenLibrary("icon.library", 37))) {
    /* opens the icon.library
     */
    backicon = GetDiskObject("backclock") ;

    if (backicon) {
      ptime = FindToolType(backicon->do_ToolTypes, "INITIALWAIT") ; // get the tooltype
      if (ptime)
        /* found
         */
        time = atoi(ptime) ;
      else
        /* not found
         */
        time = 1 ;

      FreeDiskObject(backicon) ;
    }else
      time = 1 ;
  }else
    time = 1 ;
  Delay(time * 50) ;
}

