#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "global.h"
#include "custom.h"		/* for date FORMAT strings */

#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_super_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_file_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_super_menu.h"

/*void
do_command(const char *filename)
{
	pid_t pid,  status;
	char cmd[MAX_PATH +1];
	
	snprintf(cmd, MAX_PATH +1, "> %s", filename);
	if((pid = fork()) < 0)
		 critical_error (L47E_CANT_START_NEW_PROCESS, "");
	if(pid ==0)
	{
		execl("/bin/df", "df", "-h", ">", "filename", (char*)0);
		exit(88);
	}
	while(wait(&status) != pid);
}*/
		
static void
show_disk_stats (const char *username, const char *key)
{
#define MAX_TMP_NAME 30
  char template[MAX_PATH + 1];
  char cmd[MAX_PATH * 2];
  FILE *fp;
  char line[200], name[MAX_TMP_NAME];
  int fd, i =0;


  cs_set_current_time();

  snprintf (template, MAX_PATH + 1, "%sCGI_DATA_%s_dsk_XXXXXX", TMP_PATH,
	    username);
  fd = mkstemp(template); 
  if(fd == -1)
     cs_critical_error(ERR_MKSTEMP_FAILED,"");
   close(fd);
   change_mkstemp_permission(template);   /* shared_file_util.c */
  snprintf (cmd, MAX_PATH * 2, "df -h > %s", template);
  system (cmd);
  fp = fopen (template, "r");
  if (!fp)
    {
      unlink (template);
      cs_critical_error (ERR_FOPEN_READ_FAILED, "");
    }



  while (fgets (line, 199, fp))
  {
    snprintf(name, MAX_TMP_NAME, "line.%d", i);
    cs_set_value (name, line);
    i++;
  }
  fclose (fp);
  unlink (template);
#undef MAX_TMP_NAME 

}



int
main ()
{
  char key[MAX_KEY + 1];	/* from id=? command line */
  SESSION user;

  cs_cgi_init();
  read_param ( key);
  validate_super_key (key,&user);	/* shared_super_util.c*/
  set_admin_page(&user, key, 0, "super_server_disk");  /* shared_super_menu.c */
  
  show_disk_stats (user.username, key);

  cs_cgi_display("super_server_disk", 1);
  cs_cgi_destroy();
  return 0;			/* exit successfully */

}

