#include <stdio.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>		/* for unlink */
#include <sys/types.h>		/* for opendir() et. al. */
#include <dirent.h>

#include "global.h"
#include "custom.h"


#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_cookie.h"


void
delete_server_key(const char *key)
{
 char key_path[MAX_PATH + 1];
 char real_keyname[MAX_KEY + 1];
 
 if(COOKIES_ENABLED)         /* custom.h */
    check_key_for_cookie(key,real_keyname);     /* shared_util.c */
 else
    strncpy(real_keyname, key, MAX_KEY + 1);
       
 snprintf(key_path, MAX_PATH, "../%s/keys/%s",  USERS_DIR, real_keyname);
 unlink(key_path);
}


static void
read_parameters(char *key)
{
   cs_get_required_parameter ("id", key, MAX_KEY);
}




int
main()
{
 char key[MAX_KEY + 1];
 SESSION user;

 cs_cgi_init(); 
 read_parameters(key);
 validate_server_key(key,&user);   /* shared_util.c */
 
 delete_server_key(key);
 delete_central_user_keys(user.username);  /* shared_authenticate.c */

 if(COOKIES_ENABLED)
  {
     set_cookie(COOKIE_DONE, ALIAS,"");
     printf("<html><head><meta http-equiv=\"REFRESH\" content=\"1; URL=%s\"></head></html>",  EXIT_URL);
  }
 else
    printf ("Location:%s\n\n", EXIT_URL);  /* custom.h */
 cs_cgi_destroy();
 return 0;
}

