/*

feierabend.wav~Mo-Fr,7:30-16:00,nicht_da.wav~sa-so,0:00-23:59,wochenende.wav

blanks erlaubt.

*/

#include <stdio.h>

#ifndef UNIX
#include <conio.h>
#endif

#include <time.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include "bastypes.h"
#include "util.h"
#include "os.h"

#define max_str_len 2048

HMTX hmtxsemnum;
char is_init_num = 0;

void          num2nam_init         (void)
{
  OSCreateMutexSem("\\sem32\\ctsemnum",&hmtxsemnum ,0,FALSE);
  is_init_num = 1;
}

void          num2nam_exit         (void)
{
  OSCloseMutexSem(&hmtxsemnum);
}



//static char name_of_caller   [max_str_len];
//static char name_of_eaz      [max_str_len];

char *del_blanks (char *src)
{
  static char dest_str[max_str_len];
  char *dest = dest_str;
  short c_cnt = 0;

  *dest = 0;

  while (*src != 0) {
    if (*src == ',') c_cnt++;
    if (c_cnt < 2) {
      if (*src == ' ') {
        src++;
      } else {
        *dest++ = *src++;
      }
    } else {
      *dest++ = *src++;
    }
  }
  *dest = 0;
  return (dest_str);
}

short min_of_day (char* str)
{
  char help[20] = "";
  short rc=0;

  if (strlen(str) == 4) strcat (help,"0");
  strcat (help,str);

  rc = atoi(&help[3]);
  *strchr(help,':') = 0;
  rc += atoi (help) *60;

  return (rc);
}

short day_of_week (char *str)
{
  if (!stricmp(str,"SU")) return (7);
  if (!stricmp(str,"SA")) return (6);
  if (!stricmp(str,"FR")) return (5);
  if (!stricmp(str,"TH")) return (4);
  if (!stricmp(str,"WE")) return (3);
  if (!stricmp(str,"TU")) return (2);
  return (1);
}

char *check_time (char *str)
{
  char *strok;
  struct tm *tmbuf;
  time_t tod;
  static char default_str[max_str_len];
  static char help[max_str_len];
  static char strok_help[max_str_len];
  short begin_day, end_day, begin_min, end_min;
  short act_day, act_min;

  time( &tod );
  tmbuf = localtime( &tod );
  strcpy (default_str,str);

  if (tmbuf->tm_wday == 0) tmbuf->tm_wday = 7;
  act_day = tmbuf->tm_wday;
  act_min = tmbuf->tm_hour * 60 + tmbuf->tm_min;

  if (strchr(str,'~')) {
    strtok( str, "~" );
    while ((strok = strtok (NULL,"~")) != NULL) {
      strcpy (strok_help, strok);
      strcpy (help,del_blanks(strok_help));
      strcpy (strok_help,help);
      strcpy(help,strok);
      help[2]=0;
      begin_day = day_of_week(help);
      strcpy(help,&strok_help[3]);
      help[2]=0;
      end_day   = day_of_week(help);
      strcpy(help,&strok_help[6]);
      *strchr(help,'-') = 0;
      begin_min   = min_of_day(help);
      strcpy(help,strchr(strok_help,'-')+1);
      strcpy(help,strchr(help,'-')+1);
      *strchr(help,',') = 0;
      end_min   = min_of_day(help);

      if ((act_day >= begin_day) && (act_day <= end_day)) {
        if ((act_min >= begin_min) && (act_min <= end_min)) {
          strtok (strok_help,",");
          strtok (NULL,",");
          strcpy(help,strtok (NULL,","));
          return (help);
        }
      }
    }
    *strchr(default_str,'~') = 0;
    return (default_str);
  }
  return (default_str);
}

void process_star (char *str, char *token)
{
   short pos_star;
   short pos_pipe;
   short i,j;
   static char  tmp_str [max_str_len];

   pos_star = util_pos ('*',str);
   pos_pipe = util_pos ('|',str);

   if ((pos_star > 0) && (pos_star < pos_pipe)) {
      if ((short)strlen (token) > (pos_star-1)) token [pos_star-1] = 0;
      strcpy (tmp_str,str);
      strcpy (str,""); j = 0;
      for (i=0;i<(short)strlen(tmp_str);i++) {
         if ((i < pos_star-1) || (i > pos_pipe-2)) {
            str[j++] = tmp_str[i];
         }
      }
      str [j] = 0;
   }
}

/* remove trailing CR (if there is one) from a string */

void delete_cr(char *str)
{
  char *p;

  p = str + strlen(str) - 1;

  if (p >= str)
  {
    /* Strip off the trailing blanks */
    while( (p >= str) && ( isspace(*p) ) )
    {
      *p = '\0';
      --p;
    }

    /* Get rid of backslash if there is one */
    if( (p >=str) && (*p == '\n') )
    {
      *p = '\0';
    }
  }
}

void valid_number(char* c)
{
   while (*c && *c != '|') if (!(isdigit(*c) || (*c == '*'))) memmove(c, c+1, strlen(c)); else c++;
}

void better_string (char *org_token, char *filename, short column, char* vorgabe, char* result)
{
  FILE *namedat;
  short i;
  char str[max_str_len], tmp[max_str_len], token[max_str_len];
  char *stok;

  if (!is_init_num) num2nam_init();

  namedat = fopen (filename,"r");

  OSRequestMutexSem(&hmtxsemnum,-1);

  if (namedat) {
     while (fgets(str,max_str_len,namedat)) {

       if ((str[0] != '#' ) && (str[0] != '*' ))
       {

         valid_number (str);

         strcpy (token,org_token);
         delete_cr( str );

         process_star (str,token);

         strcpy( tmp, str );
         i = 0;
         while ((tmp[i] != '|') && (tmp[i] != 0)) i++;
         tmp[i] = 0;

         if( !strcmp(tmp,token) )
         {
           stok = strtok( str, "|" );

           if( stok != NULL )
           {
             for( i=1 ; i < column ; i++ )
               stok = strtok( NULL, "|" );

             fclose( namedat );

             if( stok == NULL ) {
               strcpy (result,check_time(vorgabe));
               OSReleaseMutexSem(&hmtxsemnum);
               return;
             }

             if( stok[0] == '*' ) {
               strcpy (result,check_time(vorgabe));
               OSReleaseMutexSem(&hmtxsemnum);
               return;
             }
             strcpy (result,check_time(stok));
             OSReleaseMutexSem(&hmtxsemnum);
             return;
           }
         }
       }
     }
     fclose (namedat);
  }

  strcpy (result,check_time(vorgabe));
  OSReleaseMutexSem(&hmtxsemnum);
  return;
}

