/*
** bsd-switch.c
**
** Envokes Amiga-Dos, if Left-Shift is pressed.
** Otherwise it starts NetBSD
**
** © 1995 - christoph wilhelm
** 
*/

/* Global Var's */
struct MsgPort    *port = NULL;
struct IOStdReq   *req  = NULL;
struct Library    *InputBase = NULL;
char              ExecuteString[120];
char              Version[] = "$VER: BSD-Switch 1.0 - © 1995 Christoph Wilhelm ";

/* ProtoTypes */
BOOL Setup( void );
BOOL CheckShift( void );
void ShutDown( void );

/* Procedures */

BOOL Setup() {
   /*
   ** Open Exec-Messageport, IO-Request and 'input.device'
   **
   ** returns TRUE, if succesfull,
   ** FALSE else.
   */

   int   error = NULL;

   /* Allocate memory for Message-Prot and initialize it!  */
   port = CreatePort("bsd-starter",0);

   if( port != NULL ) {

      /* Create Standard IO-Request */
      req = CreateStdIO( port );

      if( req != NULL ) {

         /* ... and finally open the device */
         error = OpenDevice( "input.device", 0, (struct IORequest*) req, 0 );

         if( error != NULL )
            DeleteStdIO( req );
         else
            /*
            ** PeekQualifier() must know, where the input.device has its
            ** Library-Base
            */
            InputBase = &req->io_Device->dd_Library;
      }
      else
         DeletePort( port );
   }

   if( error == NULL )
      return TRUE;
   else
      return FALSE;
}

BOOL CheckShift() {
   BOOL  pressed;
   UWORD qual;

   qual = PeekQualifier();          /* Read all pressed Qualifiers */
   if( qual & IEQUALIFIER_LSHIFT )  /* Left-Shift pressed ? */
      pressed = TRUE;
   else
      pressed = FALSE;

   return pressed;
}

void ShutDown() {

   /* This cleans up all used Resources */
   CloseDevice( (struct IORequest *) req );
   DeleteStdIO( req );
   DeletePort( port );
}

void main( int argc, char *argv[] ) {
   if( argc < 3 || argc >3 ) {
      printf("\n");
      printf("BSD-Switch - © 1995 Christoph Wilhelm \n");
      printf("\n");
      printf("Usage:   bsd-switch <loadbsd> <kernel> \n");
      printf("         where <loadbsd> represents the directory \n");
      printf("         that holds the 'loadbsd'-command and \n");
      printf("         <kernel> the kernel-directory and its \n"); 
      printf("         name. \n");
      printf("\n");
      printf("Example: 1> BSD-Switch C: devs:kernel/netbsd \n");
      printf("\n");

   }
   else {
      if( Setup() ) {
         /*
         ** Left-Shift pressed ?
         */
         if( ! CheckShift() ) {

            /* Cleanup and make env: assign */
            ShutDown();
            Execute( "Assign env: ram:", NULL, NULL ); /* loadbsd needs this */

            /* Assemble the String to execute */
            strcpy( &ExecuteString[0], argv[1] );
            strcat( &ExecuteString[0], "loadbsd -a " );
            strcat( &ExecuteString[0], argv[2] );

            /* and DO IT! */
            Execute( &ExecuteString[0], NULL, NULL ); 
         }
         else
            /* load Amiga-DOS */
            ShutDown();
      }
   }
   exit(0);
}
