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

#include "ClearSilver.h"

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

#include "manhat-lib/shared_cs_util.h"
#include "manhat-lib/shared_server_string.h"



static void
set_aspell_settings()
{
 char string[MAX_PATH + 1];
 
 snprintf(string, MAX_PATH + 1 , "%s %s", ASPELL_PATH, ASPELL_ARGS);
 
 cs_set_value("aspell_command", string);
}

 



static void
set_servername_tests()
{
 char server_string[MAX_PATH + 1];
 char url[MAX_PATH + 1];
 
 get_server_string(server_string, MAX_PATH + 1);    /* shared_server_string.c */
 
 cs_set_value("server_string", server_string);
 cs_set_value("alias", ALIAS);
 cs_set_value("sbin_alias", SBIN_ALIAS);
 
 snprintf(url, MAX_PATH + 1, "%s/%s/doorstep", server_string, ALIAS);
 cs_set_value("doorstep_url", url);
 
 
 snprintf(url, MAX_PATH + 1, "%s/%s/super_doorstep", server_string, SBIN_ALIAS);
 cs_set_value("super_doorstep_url", url);


 snprintf(url, MAX_PATH + 1, "%s/%s/super_conf", server_string, SBIN_ALIAS);
 cs_set_value("super_conf_url", url);
 
 
} 


static void
test_pdftohtml()
{
   char file[MAX_PATH +1];
   char command[MAX_PATH *2+1];
   FILE *fp;
   struct stat buf;
   char *buffer;

   snprintf(file, MAX_PATH +1, "../tmp/pdftohtmltest.txt");
   snprintf(command, MAX_PATH *2 +1, "pdftohtml 2> %s", file);
   system(command);
   
   if(stat(file, &buf))
   {
	cs_set_int_value("pdf_command_failed", 1);
	return;
   }
   fp = fopen(file, "r");
   if(!fp)
   {
	cs_set_int_value("file_not_found", 1);
	return;
   }
   buffer = (char *) malloc(sizeof(char) *(buf.st_size +1));
   if(!buffer)
   {
	cs_set_int_value("file_not_found", 1);
	return;
   }
  if (fread (buffer, buf.st_size, 1, fp) != 1)
   {
	cs_set_int_value("read_file_failed", 1);
	return;
   }
  *(buffer + buf.st_size) = '\0';
  cs_set_value("pdftohtml", buffer);
  unlink(file);

	
   
}

static void
set_symlink_test()
{

char url[MAX_PATH + 1];

unlink("../images/links/symlink_tester");
symlink ("../theme_0/manhattan.gif", "../images/links/symlink_tester");

snprintf(url, MAX_PATH + 1, "%slinks/symlink_tester", IMAGE_PATH);
cs_set_value("symlink_test_image", url);

}






int
main ()
{

  cs_cgi_init();    /* shared_cs_util.c */

  cs_set_value("Config.NoCache", "yes");
  
  set_aspell_settings();  
  set_servername_tests() ;
  set_symlink_test();
  test_pdftohtml();
 
  cs_cgi_display ("config_test", 1);    /* shared_cs_util.c */
    
  cs_cgi_destroy();        /* shared_cs_util.c */
  return 0;

}

