/*
.--------------------.----------.
| SKEL-ShutDown v1.0 | ShutDown |
`--------------------^----------'

/**********************/
/*****  Includes  *****/
/**********************/

#include <proto/all.h>
#include <stdio.h>
#include <stdlib.h>
#include <exec/types.h>  /* STRPTR         */
#include <exec/ports.h>  /* struct Message */
#include <exec/memory.h> /* MEMF_PUBLIC    */
#include <exec/nodes.h>  /* NT_MESSAGE     */
#include <Tempest/Headers.h>
#include <stdarg.h>
#include <string.h>

void PRINTF(char *,...);
void clean_up(char *);
int Loadnody(int);

/************************/
/*****  Structures  *****/
/************************/

struct MsgPort *msgp, *replymsgp=NULL;
struct TempMessage *TempMsg=NULL;
struct node_info nody;
char TMP[50];

/**************************/
/*****  Main Program  *****/
/**************************/

int main(int argc,char *argv[])
{
/************************/
/*****  Variables  ******/
/************************/

 char string1[100],string2[100],shit1[150];
 int NODE,stat;

/****************************/
/*****  CLI Interface  ******/
/****************************/

 sprintf(shit1,"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"\"");
 if(argc<2)
  {
   PRINTF("\n[m[1m    _______ _________\n");
   PRINTF("   / ___/ [4m/[m[1m/ / ___/ /    [[32mShutDown v3.51[m[1m] - [3mShut Down Node[m\n");
   PRINTF("[1m  [4m/[m[1m__  /   _/ ___/ [4m/[m[1m__\n");
   PRINTF("[1m [4m/[m[1m____[4m/[m[1m_/\\_[4m)[m[1m____[4m/[m[1m____/   For Tempest BBS - [Programmer: Michael Bockert]\n");
   PRINTF(shit1);
   PRINTF("\n[m   USAGE: ShutDown [NODE] [Option]\n");
   PRINTF(" EXAMPLE: ShutDown 1\n\n");
   PRINTF("   [NODE] Node Number to shut down\n");
   PRINTF(" [Option] Optional Option / Condition of shutdown\n\n");
   PRINTF(" Optional Options List:\n");
   PRINTF(" =====================\n");
   PRINTF(" u = Only shutdown if no user is online.\n");
   exit(0);
  }
 NODE=atoi(argv[1]);

 TempMsg=(struct TempMessage *)AllocVec(sizeof(struct TempMessage), MEMF_PUBLIC | MEMF_CLEAR);
 if(!TempMsg) exit(0);

 sprintf(TMP,"TempestMsgPort-%2.2d",NODE);
 msgp = (struct MsgPort *) FindPort(TMP);
 if(!msgp) clean_up("Could Not Find Msg Port");

 replymsgp=(struct MsgPort *)CreatePort("ReplyPort",0);
 if(!replymsgp) clean_up("Could not create the reply port!");

 TempMsg->Msg.mn_Node.ln_Type = NT_MESSAGE;
 TempMsg->Msg.mn_ReplyPort    = replymsgp;
 TempMsg->Msg.mn_Length       = sizeof(struct TempMessage);
 TempMsg->command=2;

 if(argc>2)
  {
   argv[2][0]=toupper(argv[2][0]);
   switch(argv[2][0])
    {
     case 'U': Loadnody(NODE);
               if(nody.UserInfo->Name[0]!=0) clean_up("\nUser Online, Aborting!\n");
               break;
     default:  clean_up("\nNo Such Option, Aborting!\n");
    }
  }
 PutMsg(msgp,TempMsg);
 WaitPort(replymsgp);
 GetMsg(replymsgp);
 clean_up("");
}

void clean_up(char *text )
{
 if(replymsgp) DeletePort(replymsgp);
 if(TempMsg)   FreeVec(TempMsg);
 if(strlen(text)>=1) PRINTF( "Error: %s\n",text);
 exit(0);
}

void PRINTF(char *fmt,...)
{
 va_list args;
 char string[150];
 va_start(args, fmt);
 vsprintf(string, fmt, args);
 va_end(args);
 Write(Output(),string,strlen(string));
}

//************************
//*****  Load Nody  ******
//************************

int Loadnody(int node)
{
 char string[50];
 register int y;
 int file,stat;
 sprintf(string,"Node:NodeInfo_%d",node);
 for(y=1;y<3;y++)
  {
   file=Open(string,MODE_OLDFILE);
   if(file!=0)
    {
     stat=Read(file,(char *)&nody,sizeof(struct node_info));
     Close(file);
     if(stat==sizeof(struct node_info)) return(1);
    }
  }
 return(0);
}
