/* $Revision Header built automatically *************** (do not edit) ************
**
** © Copyright by Lars Eilebrecht (Shadowfox)
**
** File             : Work:Program/SC/PRG/ShadowStart/ShadowStart.c
** Created on       : Mittwoch, 16.11.94 17:23:09
** Created by       : Lars Eilebrecht
** Current revision : V1.3
**
**
** Purpose
** -------
**     Disable "WBStartup" and/or "User-Startup" during startup. (V37)
**
** Revision V1.3
** --------------
** created on Montag, 14.08.95 23:07:13  by  Lars Eilebrecht.   LogMessage :
**   - added validation test for SYS: partition
**     minor code changes
**
** Revision V1.2
** --------------
** created on Freitag, 09.12.94 01:05:03  by  Lars Eilebrecht.   LogMessage :
**   - rewrote ShadowStart for use with Kick/WB 2.0+
**     (Activation-keys are now left or right alt)
**     minor code changes
**
** Revision V1.1
** --------------
** created on Montag, 28.11.94 18:58:55  by  Lars Eilebrecht.   LogMessage :
**  -*-  changed on Samstag, 03.12.94 22:12:20  by  Lars Eilebrecht.   LogMessage :
**   - recompiled ShadowStart without startup-code
**     (results in a shorter executable)
**  -*-  created on Montag, 28.11.94 18:58:55  by  Lars Eilebrecht.   LogMessage :
**   - added NewShell-button
**     renamed other buttons, so that it fit on a lores-screen
**     removed stdio.h include (shorter executable)
**     cleaned up code a little
**
** Revision V1.0
** --------------
** created on Mittwoch, 16.11.94 17:23:09  by  Lars Eilebrecht.   LogMessage :
**     --- Initial release ---
**
*********************************************************************************/
#define REVISION "1.3"
#define REVDATE  "14.08.95"
#define REVTIME  "23:07:13"
#define AUTHOR   "Lars Eilebrecht"
#define VERNUM   1
#define REVNUM   3
#define TITLE "ShadowStart "REVISION" ("REVDATE") (V37) © by "AUTHOR

#include <exec/types.h>
#include <intuition/intuition.h>
#include <libraries/dos.h>
#include <devices/inputevent.h>

#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/input_pragmas.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
#include <clib/input_protos.h>
#include <clib/alib_protos.h>

static const char ver[]="$VER: "TITLE"\0";

int ShadowStart(void)
{
  LONG rc=RETURN_FAIL;

  struct Library *DOSBase;

  if(DOSBase=OpenLibrary("dos.library",37))
  {
    struct Library *IntuitionBase;   

    if(IntuitionBase=OpenLibrary("intuition.library",37))
    {
      struct MsgPort    *MsgPort;
      struct IOStdReq   *InputReq;
      struct Library    *InputBase;
       
      if(MsgPort=CreateMsgPort())
      {                                                         // create MsgPort and open input.device
        if(InputReq=CreateStdIO(MsgPort))
        {
          if(!OpenDevice("input.device",0,(struct IORequest *)InputReq,0))
          {
            BPTR syslock=Lock("SYS:",ACCESS_READ);              // get lock on SYS: partition

            rc=RETURN_OK;
            if(syslock)
            {            
              struct InfoData id;
              BOOL success=success=Info(syslock,&id);           // get info on SYS:
              while(success&&(id.id_DiskState==ID_VALIDATING))
              {                                                 // delay program excution until SYS: is validated
                Delay(150);                                     // wait 3 seconds...
                success=Info(syslock,&id);                      // and check again
              }                            
              if(!success)
                rc=RETURN_FAIL;
              if(Rename("SYS:_WBStartup","SYS:WBStartup"))      // rename to original names
                Rename("SYS:_WBStartup.info","SYS:WBStartup.info");
              Rename("SYS:S/_User-Startup","SYS:S/User-Startup");

              InputBase=(struct Library *)InputReq->io_Device;
              if(PeekQualifier()&IEQUALIFIER_LALT||PeekQualifier()&IEQUALIFIER_RALT)
              {                                                 // check for pressed alt-keys
                LONG result;

                struct EasyStruct shadowES =                    // declare EasyRequest-struct
                                  {
                                    sizeof(struct EasyStruct),
                                    0, // no flags
                                    "ShadowStart "REVISION" ("REVDATE") © by Lars `SFX' Eilebrecht",
                                    "\nSelect startup-items you want to disable\n\nor select `NewShell' to open a shell-window\n",
                                    "WB + User|WB|User|NewShell|Cancel",
                                  };

                result=EasyRequest(NULL,&shadowES,NULL);
                switch (result)
                {                                               // note: success of rename-operation isn't checked...
                  case 1: 
                    Rename("SYS:S/User-Startup","SYS:S/_User-Startup");
                  case 2:
                    Rename("SYS:WBStartup","SYS:_WBStartup");
                    Rename("SYS:WBStartup.info","SYS:_WBStartup.info");
                    break;
                  case 3:
                    Rename("SYS:S/User-Startup","SYS:S/_User-Startup");
                    break;
                  case 4:
                   if(!Execute("NewShell",0,0))                 // open a shell-window...
                     rc=RETURN_FAIL;
                   Wait(SIGBREAKF_CTRL_C);                      // 'hold' startup-sequence...
                }
              }
              UnLock(syslock);
            }
            CloseDevice((struct IORequest *)InputReq);
          }
          DeleteStdIO(InputReq);
        }
        DeleteMsgPort(MsgPort);
      }
      CloseLibrary(IntuitionBase);
    }
    else
      PrintFault(ERROR_INVALID_RESIDENT_LIBRARY,"intuition.library");
    CloseLibrary(DOSBase);
  }
  return(rc);
}
