#import "LoginStatPanelObject.h"
#include <stdio.h>
#include <time.h>
#include <sys/types.h>

@implementation LoginStatPanelObject

NSRect frame;
int temp_x, temp_y;

//INIT
// This is one of the first methods executed. (Presently not used)
// after the nib is loaded. This is not the best way to initialize
// an object because not all method and varibles are ready to be used.
- init
{	
        [super init];

        return self;
}


// AWAKEFROMNIB
// This is one of the last methods executed after the nib file is loaded.
// This is the BEST way I know how to initialize an object and jump into
// a method after the nib file was loaded, because you are all the methods
// and variables needed can be used. (Unlike init which is not compeletly
// ready when that message is sent
- (void)awakeFromNib
{

        [self SetDate:NULL];
        [self Last:NULL];
}



- (void)Last:(id)sender
{

    // Local Variables
    FILE *calCmd;
    char c, buff[100000], *pbuff;

    [LoginStatsPanel makeKeyAndOrderFront:NULL];

    sprintf( buff, "last");
    if ( (calCmd=popen(buff, "r")) == NULL )
    [[LoginStatsView documentView] setString:@"Can't build the Scrollview!"];
    else {
            pbuff = buff;
            while( (c=fgetc(calCmd)) != EOF )
                    *(pbuff++)=c;
            *pbuff='\0';
            pclose(calCmd);
            [[LoginStatsView documentView] setString:[NSString stringWithCString:buff]];
            }

    [LoginStatsView display];
    
}

// LastComm
// Lastcomm gives information on previously executed commands.
// With no arguments, lastcomm prints information about all the
// commands recorded during the current accounting file's life-
// time.
- (void)LastComm:(id)sender
{

    // Local Variables
    FILE *calCmd;
    char c, buff[500000], *pbuff;

    [LoginStatsPanel makeKeyAndOrderFront:NULL];

      sprintf( buff, "lastcomm");

    if ( (calCmd=popen(buff, "r")) == NULL )
    [[LoginStatsView documentView] setString:@"Can't build the Scrollview!"];
    else {
            pbuff = buff;
            while( (c=fgetc(calCmd)) != EOF )
                    *(pbuff++)=c;
            *pbuff='\0';
            pclose(calCmd);
            [[LoginStatsView documentView] setString:[NSString stringWithCString:buff]];
            }

    [LoginStatsView display];

    
}

- (void)LoginAccounting:(id)sender
{

    // Local Variables
    FILE *calCmd;
    char c, buff[100000], *pbuff;

    [LoginStatsPanel makeKeyAndOrderFront:NULL];

    sprintf( buff, "/usr/etc/ac -p");

    if ( (calCmd=popen(buff, "r")) == NULL )
    [[LoginStatsView documentView] setString:@"Can't build the Scrollview!"];
    else {
            pbuff = buff;
            while( (c=fgetc(calCmd)) != EOF )
                    *(pbuff++)=c;
            *pbuff='\0';
            pclose(calCmd);
            [[LoginStatsView documentView] setString:[NSString stringWithCString:buff]];
            }

    [LoginStatsView display];
    
}

- (void)SearchLast:(id)sender
{

    // Local Variables
    FILE *calCmd;
    char c, buff[100000], *pbuff;
    char *username;
    char *datestring;


    [LoginStatsPanel makeKeyAndOrderFront:NULL];

    username = [[StringInput stringValue] cString];
    datestring = [[DateInput stringValue] cString];

    //sprintf( buff, "last | grep eric | grep 'Jun 25'");

    sprintf( buff,"last | grep %s | grep '%s'",username,datestring);

    if ( (calCmd=popen(buff, "r")) == NULL )
    [[LoginStatsView documentView] setString:@"Can't build calendar!"];
    else {
            pbuff = buff;
            while( (c=fgetc(calCmd)) != EOF )
                    *(pbuff++)=c;
            *pbuff='\0';
            pclose(calCmd);
            [[LoginStatsView documentView] setString:[NSString stringWithCString:buff]];
            }

    [LoginStatsView display];
    
}

// SystemAccounting
// system accounting information for every process executed
- (void)SystemAccounting:(id)sender
{

    // Local Variables
    FILE *calCmd;
    char c, buff[100000], *pbuff;

    [LoginStatsPanel makeKeyAndOrderFront:NULL];

      sprintf( buff, "/usr/etc/sa");

    if ( (calCmd=popen(buff, "r")) == NULL )
    [[LoginStatsView documentView] setString:@"Can't build the Scrollview!"];
    else {
            pbuff = buff;
            while( (c=fgetc(calCmd)) != EOF )
                    *(pbuff++)=c;
            *pbuff='\0';
            pclose(calCmd);
            [[LoginStatsView documentView] setString:[NSString stringWithCString:buff]];
            }

    [LoginStatsView display];
    
}

// SystemAccounting
// system accounting information for every process executed
// -m    Print number of processes and number of CPU minutes for
//          each user.
- (void)SystemAccountingM:(id)sender
{

    // Local Variables
    FILE *calCmd;
    char c, buff[100000], *pbuff;

    [LoginStatsPanel makeKeyAndOrderFront:NULL];

      sprintf( buff, "/usr/etc/sa -m");

    if ( (calCmd=popen(buff, "r")) == NULL )
    [[LoginStatsView documentView] setString:@"Can't build the Scrollview!"];
    else {
            pbuff = buff;
            while( (c=fgetc(calCmd)) != EOF )
                    *(pbuff++)=c;
            *pbuff='\0';
            pclose(calCmd);
            [[LoginStatsView documentView] setString:[NSString stringWithCString:buff]];
            }

    [LoginStatsView display];
    
}


// SET DATE
- (void)SetDate:(id)sender
// This will assign variables for the current date.
{

        // Local Variables
        char *MonthString;
        char buff[500];

        long time(), tmpTime;
        struct tm *tm, *localtime();
        tmpTime = time(0);
        tm = localtime( &tmpTime );
        day = (tm->tm_mday);
        month = (tm->tm_mon+1);


  switch (month)  { /* start switch */

   case 1:
          MonthString = "Jan";
          break;

   case 2:
          MonthString = "Feb";
          break;

   case 3:
          MonthString = "Mar";
            break;

   case 4:
          MonthString = "Apr";
            break;

   case 5:
          MonthString = "May";
            break;

   case 6:
          MonthString = "Jun";
            break;

   case 7:
          MonthString = "Jul";
            break;

   case 8:
          MonthString = "Aug";
            break;

   case 9:
          MonthString = "Sep";
            break;

   case 10:
          MonthString = "Oct";
            break;

   case 11:
          MonthString = "Nov";
            break;

   case 12:
          MonthString = "Dec";
            break;
                                } /* end switch */

        sprintf( buff,"%s %d",MonthString,day);
        [DateInput setStringValue:[NSString stringWithCString:buff]];

 }


@end
