/* NEWSSHUTDOWN.C.
   This program tries to obtain an exclusive mode lock which causes
   blocking ASTs to be delivered to the other programs that have called
   init_lock();
   The program needs to be run with SYSLCK privilege
**
**  MODIFICATION HISTORY:
**	V6.1b9	17-Aug-1994     Mark Martinec   mark.martinec@ijs.si
**	  - code cleanup to make it compile under gcc 2.6.0 with full
**	    warnings reporting turned on - with no or very few harmless warnings
**	V6.1b9	17-Sep-1994     Mark Martinec   mark.martinec@ijs.si
**	  - code cleanup to preserve the read-only nature of string literals
**	    (strategically placed 'const' attribute to string parameters)
**	V6.1b10 09-DEC-1996	Mark Pizzolato	mark@infocomm.com
**	  - minor declaration cleanup to get warningless compile under 
**	    DEC C V5.3 on VAX
**--
*/

#ifdef vaxc
#module NEWSSHUTDOWN "V6.2"
#endif

#define _NEWSSHUTDOWN_C

/* Causes variable declarations for things declared in NEWSVARIABLES.H */
#define _NEWS_MAIN_PROGRAM

#include "newsinclude.h"
#include "newsextern.h"

int main(argc, argv)
  int argc;
  char **argv;
{
    int status;
    $DESCRIPTOR_CONST(table, "LNM$FILE_DEV");
    $DESCRIPTOR_CONST(lognam, "NEWS_LOCKED_COMMAND");
    $DESCRIPTOR_CONST(waitnam, "NEWS_LOCKED_WAIT_MINUTES");
    $DESCRIPTOR_CONST(prcnam, "NEWS_LOCKED");
    char trans[LNM$C_NAMLENGTH+1];
    $DESCRIPTOR_CONST(command, trans);
    char wait_str[LNM$C_NAMLENGTH+1];
    $DESCRIPTOR(wait_dsc, wait_str);
    int wait_minutes = 1;
    struct itm3 { unsigned short len, code; void *buf; unsigned short *retlen; }
	trnlist[] = { {(sizeof trans)-1, LNM$_STRING,
                       (void*)trans, &(command.dsc$w_length)},
                      {0,0,0,0} };
    struct itm3 
	trnlist1[] = { {(sizeof wait_str)-1, LNM$_STRING,
                        (void*)wait_str, &(wait_dsc.dsc$w_length)},
                       {0,0,0,0} };
    if (!(1 & sys$trnlnm(0,&table,&lognam,0,trnlist) ))
	{
	fprintf(stderr, "No Command Specified via logical NEWS_LOCKED_COMMAND\n");
	exit(0);
	}
    if ((1 & sys$trnlnm(0,&table,&waitnam,0,trnlist1) ))
	{
	wait_str[wait_dsc.dsc$w_length] = '\0';
	if (0 < atoi(wait_str))
	    wait_minutes = atoi(wait_str);
	}
    if (!((status = acquire_exclusive_lock(60*wait_minutes)) & 1))
	exit(status);
    /* 
     * We've got the lock now (exclusively), so everyone else 
     * has already shutdown, now we can go do what ever needed exclusive 
     * access to things.
     */
    lib$spawn(&command, 0, 0, 0, &prcnam, 0, &status, 0,0,0,0,0,0);
    exit(status);
}
