
/*
**
**  $VER: Reboot.c 1.7 (15.3.97)
**  Reboot 1.7
**
**  main file
**
**  (C) Copyright 1994-97 by Roland 'Gizzy' Mainz
**          All Rights Reserved
**
*/

/* We did not want any names before V36 */
#define INTUI_V36_NAMES_ONLY 1

/* amiga includes */
#include <exec/types.h>
#include <dos/dos.h>
#include <intuition/intuition.h>
#include <workbench/startup.h>
#include <workbench/icon.h>
#include <libraries/locale.h>

/* amiga prototypes */
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/intuition_protos.h>
#include <clib/icon_protos.h>
#include <clib/locale_protos.h>

/* amiga pragmas */
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <pragmas/intuition_pragmas.h>
#include <pragmas/icon_pragmas.h>
#include <pragmas/locale_pragmas.h>
#include <pragmas/alib_pragmas.h> /* tagcall stubs */

/* ansi includes */
#include <string.h>

/* version string */
#include "Reboot_rev.h"

/* locale */
#define CATCOMP_NUMBERS
#define CATCOMP_STRINGS
#include "Reboot_strings.h"

/* misc defines */
#ifndef NAME
#define NAME "Reboot"
#endif /* !NAME */

/* prototypes */
#ifdef _DCC
                void                  chkabort( void );
                long                  wbmain( struct WBStartup * );
#endif /* _DCC */

                long                  main( long, STRPTR * );
        static  void                  DefaultSettings( void );
        static  void                  FreeInitProjectResult( void );
        static  void                  ReadENVPrefs( void );

        static  void                  ClearRDA( void );
        static  void                  ScanRDA( void );
        static  void                  ScanToolTypes( STRPTR * );

        static  void                  RunTool( void );
        static  BOOL                  OpenLibStuff( void );
        static  void                  CloseLibStuff( void );
        static  BOOL                  CreateBasicResources( void );
        static  void                  DeleteBasicResources( void );
                void                  AttemptOpenLibrary( struct Library **, STRPTR, STRPTR, ULONG );
        static  void                  RunTool( void );
        static  void                  MyReboot( void );
        static  STRPTR                SafeGetCatalogStr( struct Catalog *, LONG, STRPTR );



/* version_string */
STRPTR versionstring = VERSTAG;

long main_retval,
     main_retval2;

static struct RDArgs  *startuprda;

/* shared libraries */
extern struct Library *SysBase,
                      *DOSBase;

struct Library *IntuitionBase,
               *IconBase,
               *LocaleBase;

/* locale support */
struct Catalog *ct;

/* templates for ReadArgs */
#define STARTUP_TEMPLATE "REBOOTDELAY/K/N,QUIET=NOREQ/S,OFF/S"
#define ENV_TEMPLATE     STARTUP_TEMPLATE /* no /A switches */


static
struct
{
    long *rebootdelay;
    long *noreq;
    long *off;
} result;

static
struct
{
    ULONG rebootdelay;
    BOOL  noreq;
    BOOL  off;
} project;


#ifdef _DCC
/* disable CTRL_C break support (DICE CTRL_C abort function) */
void chkabort( void )
{
}
#endif /* _DCC */


/****** Reboot/Reboot ********************************************************
*
*    NAME
*       Reboot -- rebooting utility
*
*    FORMAT
*       Reboot [REBOOTDELAY <seconds>] [QUIET|NOREQ] [OFF]
*
*    TEMPLATE
*       REBOOTDELAY/K/N,QUIET=NOREQ/S,OFF/S
*
*    TOOLTYPES
*       REBOOTDELAY/N
*       QUIET/S
*       NOREQ/S
*       OFF/S
*
*    PURPOSE
*       To reboot the computer.
*
*    INTRODUCTION
*       A simple tool to be used with Kickstart 2.x/3.x, written by
*       Roland 'Gizzy' Mainz, freeware, all rights reserved.
*       No guarantee of any kind is made that  the programm described below 
*       in this document is 100% reliable. You use this material on your own
*       risk.
*
*       While playing with ENVOY 1.6, I need a program which allows to reboot
*       another AMIGA through the network (by my own "RemoteNetShell") or
*       after a given time.
*       First I wrote a little script (using WAIT) and a C program calling
*       exec.library/ColdReboot() to solve this problem.
*       Switching to IAM's ENVOY 2.0 I replaced this script by this tool,
*       adding some features (WB (including multiselect), SHELL ReadArgs,
*       locale support and test mode).
*
*    DESCRIPTION
*       Reboot is used to reboot the whole computer like a "cold"
*       system start.
*
*       Unless you specify otherwise (through REBOOTDELAY option), reboot
*       will wait 2 seconds before rebooting the system.
*       This is used to let the system (and it's disk drives) settle.
*       This delay time can't be less than 1 second, a REBOOTDELAY=0
*       will also wait this minimum time.
*
*       Whenever the QUIET (or NOREQ) switch is set, the verify requesters
*       ("Are you sure#?" etc.) will be suppressed.
*
*       If the OFF switch is set, the tool act like when a CTRL-C signal
*       has been send to the tool.
*
*       Variable support:
*         The environment REBOOT variable (either LOCAL or GLOBAL) can be
*         used for default settings. The parsing template for this
*         variable is the same like the shell template (see above).
*
*       Reboot supports workbench multiselect (e.g. project icons) !
*
*    BUGS
*       - Shell variables longer than 255 are currently not supported.
*
*       - Does not work properly with my Apollo 2030 accerlator board.
*         The system reboots, then hangs. cause known,
*         comments welcome.
*
*    NOTES
*       "Set Reboot OFF" (e.g. use of a local variable) is only valid if
*       you start Reboot from this shell.
*
*    HISTORY
*       V1.1:
*         - First release
*
*       V1.2: Fixed bugs
*         - The "Abort reboot" requester couldn't be canceld 1 second before
*           the given time.
*           Fixed.
*
*         - I did not read the autodocs exactly:
*           The function BuildEasyRequest might return 0 or 1 at low mem time.
*           Now Reboot simply fails with ERROR_NO_FREE_STORE for both 0 or 1.
*           If 1 was returned, an enforcer hit would occur.
*           Never happed, but
*           fixed.
*
*         - AttemptOpenLibrary could return (sometimes) random pointers.
*           In fact, this never happened by me, but maybe ...
*           Fixed.
*
*         - While getting Reboot's environment variable, the "Abort reboot"
*           requester might be canceled. Now we check this:
*           Fixed.
*
*         - Additional, there was some code cleanup which adds support for
*           DICE INLINE calls.
*
*         - The executable is now PURE and can be made resident.
*
*         - The time between the program checks the "Abort reboot" requester
*           has been decrased down to a half second.
*
*       V1.3
*         - Starting with V1.3 environment variables are supported.
*           Things set in var REBOOT (local or global) will be parsed after
*           built-in default settings and before any user-given arguments
*           (shell args or project icons), which can overide those ENV
*           settings given there.
*
*           This feature may cause Enforcer hits (I think so, I've never seen
*           one).
*
*       V1.4
*         - Starting with V1.4, ENV parsing should be safe.
*           For Bug reports, I've included a Reboot.ld, which includes the
*           debugging info used by Enforcer/FindHit.
*
*         - Running CPU FASTROM causes sometimes problems: The system reboots,
*           but then hang atfer a while... ???
*
*       V1.5
*         - Now I fully support SAS/C for all my projects (SMakefile
*           included). A matching DMakefile is on the way.
*
*       V1.6
*         - Unused utility.library code has been removed
*
*       V1.7
*         - Recompiled with SAS/C 6.57
*
*         - Minor code cleanup.
*
*         - Moved contents of the #?.guide to this autodoc
*
*    INSTALLATION
*       After unpacking this archieve:
*
*       Workbench:
*         - Drop the 'Reboot' icon in your favorite drawer (like
*            "SYS:Utilities/").
*           See SHELL for copying the catalog files.
*
*       Shell:
*         - Unpack this archieve at "SYS:Utilities/"
*           and add the following statements at your "S:user-startup" startup
*           file:
*
*           Assign Reboot: SYS:Utilities/Reboot
*           path Reboot: ADD
*         OR
*         - Drop/Copy the 'Reboot' file in your favorite directory
*           (like "Sys:Utilities").
*           To copy the (german) catalog files, type
*           Copy Catalogs/#? TO="LOCALE:Catalogs/" ALL QUIET CLONE
*
*       SAS:
*         - Type 'smake install' in shell
*
*    SOURCE
*       DICE users may use DMakefile (if included).
*       GNU-C support will be done in one future version.
*
*       All:
*       If you like to include a new language (like Français or Nederlands
*       etc.), send me a filled "Reboot_blank.ct" file named "Reboot_#?.ct"
*       (#? for your language). As an example, see "Reboot_deutsch.ct".
*       Note that I can currently only process "Reboot_#?.ct" files whose
*       language is supported by AmigaOS 3.1 (e.g. a AmigaDOS Locale language
*       driver exists).
*
*       SAS/C:
*       Use SMakefile to create the executable.
*
*    AUTHOR's REQUEST
*        By  releasing  this program I do  not  place any obligations on you,
*        feel free to share this program with your  friends (and enemies).
*
*        If you want to blame me, report any bugs, or wants a new version
*        send your letter to:
*                        Roland Mainz
*                        Hohenstaufenstraße 8
*                        52388 Nörvenich
*                        GERMANY
*
*        Phone: (+49)(0)2426/901568
*
*        EMAIL is also available:
*        GISBURN@w-specht.rhein-ruhr.de
*
*        If you want to send me attachments larger than 1MB (up to 5MB,
*        more with my permission):
*        Up to March 1997 I'm reachable using this email address, too:
*        Reinhold.A.Mainz@KBV.DE
*
*        | Please put your name and address in your mails !
*        | German mailers should add their phone numbers.
*        | See BUGS section above when submitting bug reports.
*
*        Sorry, but I can only look once a week for mails.
*        If you don't hear something from me within three weeks, please
*        send your mail again (but watch about new releases) (problems with
*        this email port are caused by reconfigurations, hackers, network
*        problems etc.).
*
*        The  entire  "Reboot"  package may  be  noncommercially
*        redistributed, provided  that  the package  is always  distributed
*        in it's complete  form (including it's documentation).  A small copy
*        fee for media costs is okay but any kind of commercial distribution
*        is strictly forbidden! Comments  and  suggestions  how  to  improve
*        this program  are generally appreciated!
*
*        Thanks to Matt Dillon for his DICE, and Olaf 'Olsen'
*        Barthel for his help, ideas and some text clips from his
*        documentations.
*
*    SEE ALSO
*
******************************************************************************
*
*/


/* DICE workbench entry */
#ifdef _DCC
long wbmain( struct WBStartup *wbstartup )
{
    /* Call main like SAS-C */
    return( main( 0L, (STRPTR *)wbstartup ) );
}
#endif /* _DCC */


long main( long ac, STRPTR *av )
{
    LONG               numArgs,
                       x;
    struct WBStartup  *wbstartup;
    struct WBArg      *wbarg;
    struct DiskObject *tooldobj,
                      *projectdobj;
    BPTR               oldToolLock    = NULL,
                       oldProjectLock = NULL;

    main_retval2 = 0L;
    main_retval  = RETURN_OK;

    if( CreateBasicResources() )
    {
      DefaultSettings();

      /* check if something goes wrong (ENV error etc.) */
      if( main_retval == RETURN_OK )
      {
/* Workbench */
        if( ac == 0L )
        {
          wbstartup = (struct WBStartup *)av;

          numArgs = wbstartup -> sm_NumArgs;
          wbarg   = wbstartup -> sm_ArgList;

          if( *(wbarg[ 0 ] . wa_Name) )
          {
            if( wbarg[ 0 ] . wa_Lock )
            {
              oldToolLock = CurrentDir( (wbarg[ 0 ] . wa_Lock) );
            }

            if( tooldobj = GetDiskObjectNew( (wbarg[ 0 ] . wa_Name) ) )
            {
              /* two possible cases when started from workbench ... */
              if( numArgs < 2L )
              {
                /* ... first case, only our tool icon is given, create one project here */

                ScanToolTypes( (STRPTR *)(tooldobj -> do_ToolTypes) );

                /* check if something goes wrong (parsing error, etc.) */
                if( main_retval == RETURN_OK )
                {
                  RunTool();
                }

                FreeInitProjectResult();
              }
              else
              {
                /* ... second case, a couple of project icons are given, multiple projects will start from here */
                for( x = 1L ; x < numArgs ; x++ )
                {
                  if( wbarg[ x ] . wa_Lock )
                  {
                    oldProjectLock = CurrentDir( (wbarg[ x ] . wa_Lock) );
                  }

                  if( *(wbarg[ x ] . wa_Name) )
                  {
                    if( projectdobj = GetDiskObject( (wbarg[ x ] . wa_Name) ) )
                    {
                      ScanToolTypes( (STRPTR *)(tooldobj -> do_ToolTypes) );
                      ScanToolTypes( (STRPTR *)(projectdobj -> do_ToolTypes) );

                      /* check if something goes wrong (parsing error, etc.) */
                      if( main_retval == RETURN_OK )
                      {
                        RunTool();
                      }

                      FreeInitProjectResult();
                      DefaultSettings();

                      FreeDiskObject( projectdobj );
                    }
                  }

                  if( wbarg[ x ] . wa_Lock )
                  {
                    CurrentDir( oldProjectLock );
                  }
                }
              }

              FreeDiskObject( tooldobj );
            }

            if( wbarg[ 0 ] . wa_Lock )
            {
              CurrentDir( oldToolLock );
            }
          }
        }
        else
        {
/* CLI/Shell */
          if( startuprda = ReadArgs( STARTUP_TEMPLATE, (LONG *)(&result), NULL ) )
          {
            /* did we get a CTRL_C signal ? */
            if( !CheckSignal( SIGBREAKF_CTRL_C ) )
            {
              ScanRDA();

              /* check if something goes wrong (parsing error, etc.) */
              if( main_retval == RETURN_OK )
              {
                RunTool();
              }

              FreeInitProjectResult();
            }
            else
            {
              /* SIGBREAKF_CTRL_C found */
              main_retval2 = ERROR_BREAK;
              main_retval  = RETURN_WARN;
            }

            FreeArgs( startuprda );
          }
          else
          {
            /* ReadArgs failed */
            main_retval2 = IoErr();
            main_retval  = RETURN_FAIL;
          }
        }
      }

      DeleteBasicResources();
    }

    PrintFault( main_retval2, NAME );

    SetIoErr( main_retval2 );

    return( main_retval );
}


static
void DefaultSettings( void )
{
    ClearRDA();

    memset( (void *)(&project), 0, sizeof( project ) );

    project . rebootdelay = 2UL;

    ReadENVPrefs();
}


static
void ReadENVPrefs( void )
{
    struct RDArgs envvarrda =
    {
      NULL,
      256L,
      0L,
      0L,
      NULL,
      0L,
      NULL,
      RDAF_NOPROMPT
    };

    TEXT varbuff[ 258 ];

    envvarrda . RDA_Source . CS_Buffer = varbuff;

    if( GetVar( NAME, varbuff, 256L, 0UL ) != (-1L) )
    {
      strcat( varbuff, "\n" );

      if( ReadArgs( ENV_TEMPLATE, (LONG *)(&result), (&envvarrda) ) )
      {
        ScanRDA();

        FreeArgs( (&envvarrda) );
      }
      else
      {
        /* Can't parse ENV variable */
        main_retval2 = IoErr();
        main_retval  = RETURN_FAIL;
      }
    }
}


static
void ClearRDA( void )
{
    memset( (void *)(&result), 0, sizeof( result ) );
}


static
void ScanRDA( void )
{
    if( result . rebootdelay )
    {
      project . rebootdelay = (ULONG)(*(result . rebootdelay));
    }

    if( result . noreq )
    {
      project . noreq = TRUE;
    }

    if( result . off )
    {
      project . off = TRUE;
    }

    ClearRDA();
}


static
void ScanToolTypes( STRPTR *tt )
{
    STRPTR s;

    if( s = FindToolType( tt, "REBOOTDELAY" ) )
      (void)StrToLong( s, (LONG *)(&(project . rebootdelay)) ); /* note: result ignored */

    if( s = FindToolType( tt, "NOREQ" ) )
      result . noreq = (long *)s;

    if( s = FindToolType( tt, "QUIET" ) )
      result . noreq = (long *)s;

    if( s = FindToolType( tt, "OFF" ) )
      result . off = (long *)s;

    ScanRDA();
}

static
void FreeInitProjectResult( void )
{
    /* NOP */
}


static
BOOL CreateBasicResources( void )
{
    return( OpenLibStuff() );
}


static
void DeleteBasicResources( void )
{
    CloseLibStuff();
}


static
BOOL OpenLibStuff( void )
{
    IconBase = LocaleBase = NULL;

    if( IntuitionBase = OpenLibrary( "intuition.library", 37UL ) )
    {
      AttemptOpenLibrary( (&IconBase),     NAME, ICONNAME,         37UL );
      AttemptOpenLibrary( (&LocaleBase),   NAME, "locale.library", 38UL );

      if( LocaleBase )
      {
        ct = OpenCatalogA( NULL, NAME ".catalog", NULL );
      }

      if( IconBase )
      {
        return( TRUE );
      }
    }

    CloseLibStuff();

    /* Can't open requires libraries */
    main_retval2 = ERROR_OBJECT_NOT_FOUND;
    main_retval  = RETURN_FAIL;

    return( FALSE );
}


static
void CloseLibStuff( void )
{
    if( LocaleBase )
    {
      CloseCatalog( ct );

      CloseLibrary( LocaleBase );
    }

    CloseLibrary( IconBase );
    CloseLibrary( IntuitionBase );
}


void AttemptOpenLibrary( struct Library **library, STRPTR title, STRPTR libname, ULONG libversion )
{
    struct EasyStruct LibNotFoundES,
                      LibWrongVersionES;

    LibNotFoundES . es_StructSize   = sizeof( struct EasyStruct );
    LibNotFoundES . es_Flags        = 0UL;
    LibNotFoundES . es_Title        = title;
    LibNotFoundES . es_TextFormat   = SafeGetCatalogStr( ct, MSG_LIB_NOT_FOUND_REQTEXT, MSG_LIB_NOT_FOUND_REQTEXT_STR );
    LibNotFoundES . es_GadgetFormat = SafeGetCatalogStr( ct, MSG_LIB_NOT_FOUND_REQGAD,  MSG_LIB_NOT_FOUND_REQGAD_STR );

    LibWrongVersionES . es_StructSize   = sizeof( struct EasyStruct );
    LibWrongVersionES . es_Flags        = 0UL;
    LibWrongVersionES . es_Title        = title;
    LibWrongVersionES . es_TextFormat   = SafeGetCatalogStr( ct, MSG_LIB_WRONG_VERSION_REQTEXT, MSG_LIB_WRONG_VERSION_REQTEXT_STR );
    LibWrongVersionES . es_GadgetFormat = SafeGetCatalogStr( ct, MSG_LIB_WRONG_VERSION_REQGAD,  MSG_LIB_WRONG_VERSION_REQGAD_STR );

    if( (*library) == NULL )
    {
      for( ;; )
      {
        /* attemp to open shared library */
        (*library) = OpenLibrary( libname, 0UL );

        if( *library )
        {
          if( ((*library) -> lib_Version) < libversion )
          {
            EasyRequest( NULL, (&LibWrongVersionES), NULL, libname, libversion );

            CloseLibrary( (*library) );
            (*library) = NULL;
          }

          break;
        }

        /* prompt the user */
        if( EasyRequest( NULL, (&LibNotFoundES), NULL, libname ) == 0L )
        {
          /* user canceled */
          break;
        }
      }
    }
}


static
void RunTool( void )
{
    struct EasyStruct RebootES;

    RebootES . es_StructSize   = sizeof( struct EasyStruct );
    RebootES . es_Flags        = 0UL;
    RebootES . es_Title        = NAME;
    RebootES . es_TextFormat   = SafeGetCatalogStr( ct, MSG_REBOOT_REQTEXT, MSG_REBOOT_REQTEXT_STR );
    RebootES . es_GadgetFormat = SafeGetCatalogStr( ct, MSG_REBOOT_REQGAD,  MSG_REBOOT_REQGAD_STR );

    if( project . noreq )
    {
      MyReboot();
    }
    else
    {
      if( EasyRequestArgs( NULL, (&RebootES), NULL, NULL ) == 1L )
      {
        /* user wants to reboot */
        MyReboot();
      }
      else
      {
        /* cancelled */
        main_retval2 = ERROR_BREAK;
        main_retval  = RETURN_WARN;
      }
    }
}


static
void MyReboot( void )
{
    struct Window *abortreqwindow;
    ULONG          delay,
                   x;
    BOOL           abort;
    ULONG          abortreqsignal,
                   sigmask,
                   signals;

    struct EasyStruct AbortRebootES;

    AbortRebootES . es_StructSize   = sizeof( struct EasyStruct );
    AbortRebootES . es_Flags        = 0UL;
    AbortRebootES . es_Title        = NAME;
    AbortRebootES . es_TextFormat   = SafeGetCatalogStr( ct, MSG_ABORTREBOOT_REQTEXT, MSG_ABORTREBOOT_REQTEXT_STR );
    AbortRebootES . es_GadgetFormat = SafeGetCatalogStr( ct, MSG_ABORTREBOOT_REQGAD,  MSG_ABORTREBOOT_REQGAD_STR );

    abort = FALSE;

    if( (project . rebootdelay) < 1UL )
    {
      project . rebootdelay = 1UL;
    }

    if( project . noreq )
    {
      abortreqwindow = NULL;
    }
    else
    {
      /* Here we must check if we got the "low mem" return value (see BuildEasyRequestArgs autodoc)
       * which may be 0 or 1
       * For 0, I cannot determinate if this is a result from alert or a 'real' low mem failure
       */
      abortreqwindow = BuildEasyRequestArgs( NULL, (&AbortRebootES), 0UL, NULL );

      if( (abortreqwindow == ((struct Window *)1UL)) || (abortreqwindow == NULL) )
      {
        /* Can't open abort requester */
        main_retval2 = ERROR_NO_FREE_STORE;
        main_retval  = RETURN_FAIL;

        return;
      }
    }

    abortreqsignal = ((abortreqwindow)?(1UL << abortreqwindow -> UserPort -> mp_SigBit):(0UL));

    sigmask = SIGBREAKF_CTRL_C | abortreqsignal;

    delay = (project . rebootdelay) * 2UL;

    /* for each cycle we'll check if any abort signal arrived (CTRL_C or requester) */
    for( x = 0UL ; x <= delay ; x++ )
    {
      /* check if user aborts countdown */
      signals = CheckSignal( sigmask );

      if( signals & SIGBREAKF_CTRL_C )
      {
        abort = TRUE;
      }

      /* no window, no signal, no event processing ... */
      if( abortreqsignal )
      {
        if( signals & abortreqsignal )
        {
          if( SysReqHandler( abortreqwindow, NULL, FALSE ) == 0L )
          {
            abort = TRUE;
          }
        }
      }

      if( abort )
      {
        break;
      }

      /* wait a little bit */
      Delay( (TICKS_PER_SECOND / 2UL) );
    }

    /* No abort ? */
    if( abort == FALSE )
    {
      if( abortreqwindow )
      {
        /* if we got no abort msg, check the requester for any outstanding msg */
        if( SysReqHandler( abortreqwindow, NULL, FALSE ) == 0L )
        {
          abort = TRUE;
        }
      }
    }


    /* Check if user send us CTRL-C or has selected test mode using something like "Setenv Reboot OFF" */
    if( abort || (project . off) )
    {
      /* Aborted by requester or user-setup */
      main_retval2 = ERROR_BREAK;
      main_retval  = RETURN_WARN;
    }
    else
    {
      /* bite in the dust */
      ColdReboot();
    }

    /* user has aborted the rebooting sequence */
    FreeSysRequest( abortreqwindow );
}


static
STRPTR SafeGetCatalogStr( struct Catalog *ct, LONG id, STRPTR s )
{
    if( LocaleBase )
    {
      s = GetCatalogStr( ct, id, s );
    }

    return( s );
}


