#include "shared_survey_www_util.h"


int
read_password(const char *count)
{
   char password[MAX_PATH +1];
   char line[MAX_PATH +1];
   FILE *fp;

    snprintf(password, MAX_PATH +1, "../%s/%s/%s/%s",
              SURVEY_DIR, SURVEY_WWW_DIR, count, PASSWORD_FNAME);

    fp = fopen(password, "r");
    if(!fp)
       cs_critical_error(ERR_FOPEN_READ_FAILED, password);
    fgets(line, MAX_PATH, fp);
    fclose(fp);
    strtrm(line);
    return atoi(line);
}



int 
read_password_from_path(const char *path)
{
   char password[MAX_PATH +1];
   char line[MAX_PATH +1];
   FILE *fp;

    snprintf(password, MAX_PATH +1, "%s/%s",
                            path, PASSWORD_FNAME);

    fp = fopen(password, "r");
    if(!fp)
       return 0;

    fgets(line, MAX_PATH, fp);

    fclose(fp);
    strtrm(line);
    return atoi(line);





}


void set_url(const char *new_id, int password)
{
     char url[MAX_PATH +1];
     char server_string[MAX_PATH +1];

     get_server_string(server_string, MAX_PATH +1);

     snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s", server_string, ALIAS, "survey_www_survey", new_id);
     cs_set_value("survey_url.0", url);

     snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s&amp;pass=%d", server_string, ALIAS, "survey_www_bar_graph", new_id, password);
     cs_set_value("survey_url.1", url);

     snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s&amp;pass=%d", server_string, ALIAS, "survey_www_table", new_id, password);
     cs_set_value("survey_url.2", url);

     snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s", server_string, ALIAS, "survey_www_bar_graph", new_id);
     cs_set_value("survey_url.3", url);

     snprintf(url, MAX_PATH +1, "%s/%s/%s?id=%s", server_string, ALIAS, "survey_www_table", new_id);
     cs_set_value("survey_url.4", url);
     cs_set_int_value("access_code", password);
     
}

