/* $Id: cheap.c,v 1.22 1995/10/21 17:23:21 akool Exp akool $
 *
 * ISDN accounting for isdn4linux. (Feiertagsberechnung)
 *
 * Copyright 1995 by Andreas Kool (akool@Kool.f.EUnet.de)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Log: cheap.c,v $
 * Revision 1.22  1995/10/21  17:23:21  akool
 * Tarife 1996 entsprechend Telekom-Unterlagen korrigiert
 *
 * Revision 1.14  1995/10/05  19:02:21  akool
 * Ueberschreitung verschiedener Zeittakte bei einer Verbindung implementiert.
 *
 * Revision 1.2  1995/09/30  16:39:21  akool
 * Um neue Tarifstruktur der Telekom ab 1.1.96 erweitert
 * First public release
 *
 * Revision 1.1  1990/11/10  23:15:11  akool
 * Initial revision (Code-Teile aus irgend einer alten c't)
 *
 */


#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>


#define MUTT      3
#define KARF      4
#define OST1      5
#define OST2      6
#define CHRI      7
#define PFI1      8
#define PFI2      9
#define FRON     10
#define EINH     11
#define MARI     12
#define ALLE     13
#define BUSS     14

#define A_FEI 	 17

struct w_ftag {
  char tag;
  char monat;
  char telekom; /* TRUE, wenn auch fuer die Deutsche Telekom ein Feiertag (siehe Telefonbuch!) */
};


struct {
  int tag;
  int monat;
  int jahr;
  int tgind;
} datum;


char tab_tage[2][12] = {{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
	                { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }};


static struct w_ftag t_ftag[A_FEI] = {
  {  1,  1, 1 },  /* Neujahr */
  {  6,  1, 0 },  /* Erscheinungsfest - nur Baden-Wuerttemberg und Bayern */
  {  1,  5, 1 },  /* Maifeiertag */
  {  0,  0, 1 },  /* Muttertag */
  {  0,  0, 1 },  /* Karfreitag */
  {  0,  0, 1 },  /* Ostersonntag */
  {  0,  0, 1 },  /* Ostermontag */
  {  0,  0, 1 },  /* Christi Himmelfahrt */
  {  0,  0, 1 },  /* Pfingstsonntag */
  {  0,  0, 1 },  /* Pfingstmontag */
  {  0,  0, 0 },  /* Fronleichnam - nur in Baden-Wuerttemberg, Bayern, Hessen, Nordrhein-Westfalen, Rheinland-Pfalz und im Saarland */
  {  3, 10, 1 },  /* Tag der deutschen Einheit - vor 1990 am 17.6. */
  { 15,  8, 0 },  /* Maria Himmelfahrt - nur Saarland und ueberwiegend katholischen Gemeinden Bayerns */
  {  1, 11, 0 },  /* Allerheiligen - nur Baden-Wuerttemberg, Bayern, Nordrhein-Westfalen, Rheinland-Pfalz und im Saarland */
  {  0,  0, 0 },  /* Buss- und Bettag - nur bis incl. 1994 (wg. Pflegeversicherung abgeschafft) */
  { 25, 12, 1 },  /* 1. Weihnachtsfeiertag */
  { 26, 12, 1 }}; /* 2. Weihnachtsfeiertag */


int schalt(register int j)
{
  return((j % 4 == 0) && (j % 100 != 0) || (j % 400 == 0));
} /* schalt */


int tag_num(register int t, register int m, register int j)
{
  register char *tm = tab_tage[schalt(j)];

  while (--m)
    t += *tm++;

  return(t);
} /* tag_num */


void num_tag(int jahr, int lfd)
{
  register int   i;
  register char *t;


  while (lfd < 1)
    lfd += tag_num(31, 12, --jahr);

  t = tab_tage[schalt(jahr)];

  for (i = 1; lfd > *t && i < 13; i++)
    lfd -= *t++;

  if (i > 12)
    num_tag(++jahr, lfd);
  else {
    datum.monat = i;
    datum.tag = lfd;
    datum.jahr = jahr;
  } /* else */
} /* num_tag */


void comp_feier_tage(int jj)
{
  static   struct w_ftag t_stag[A_FEI];
  static   int 	  	 firsttime = 1;
  register int 		 mm, tt, i, j, a, b;


  if (firsttime) {
    for (i = 0; i < A_FEI; i++)
      t_stag[i] = t_ftag[i];

    firsttime = 0;
  }
  else
    for (i = 0; i < A_FEI; i++)
      t_ftag[i] = t_stag[i];


  /* Berechnung von Ostern nach C.F.Gauss */

  i = jj / 100 - jj / 400 + 4;
  j = i - jj / 300 + 11;
  a = (((jj % 19) * 19) + j) % 30;
  b = (((jj % 4) * 2 + (4 * jj) + (6 * a) + i) % 7) + a - 9;

  if (b < 1) {
    tt = 31 + b;
    mm = 3;
  }
  else {
    if ((b == 26) || ((a == 28) && (b == 25) && ((11 * (j + 1) % 30) < 19)))
	b -= 7;
    tt = b;
    mm = 4;
  } /* else */

  num_tag(jj, tag_num(tt, mm, jj));

  t_ftag[OST1].monat = datum.monat; t_ftag[OST1].tag = datum.tag;

  num_tag(jj, 1 + tag_num(datum.tag, datum.monat, jj));
  t_ftag[OST2].monat = datum.monat; t_ftag[OST2].tag = datum.tag;

  num_tag(jj, - 3 + tag_num(datum.tag, datum.monat, jj));
  t_ftag[KARF].monat = datum.monat; t_ftag[KARF].tag = datum.tag;

  /* Pfingsten */
  num_tag(jj, 51 + tag_num(datum.tag, datum.monat, jj));
  t_ftag[PFI1].monat = datum.monat; t_ftag[PFI1].tag = datum.tag;
  num_tag(jj, 1 + tag_num(datum.tag, datum.monat, jj));
  t_ftag[PFI2].monat = datum.monat; t_ftag[PFI2].tag = datum.tag;

  /* Himmelfahrt */
  num_tag(jj, -10 + tag_num(t_ftag[PFI1].tag, t_ftag[PFI1].monat, jj));
  t_ftag[CHRI].monat = datum.monat; t_ftag[CHRI].tag = datum.tag;

  /* Fronleichnam */
  num_tag(jj, 11 + tag_num(t_ftag[PFI1].tag, t_ftag[PFI1].monat, jj));
  t_ftag[FRON].monat = datum.monat; t_ftag[FRON].tag = datum.tag;
} /* comp_feier_tage */


int billig(time_t when)
{
  register int        i;
  auto 	   struct tm *tm;


  tm = localtime(&when);


  if (tm->tm_wday == 6) /* Samstag immer */
    return(1);

  if (tm->tm_wday == 0) /* Sonntag immer */
    return(1);

  if ((tm->tm_hour > 17) || (tm->tm_hour < 8))   /* zwischen 18:00 .. 7:59 immer */
    return(1);

  if ((tm->tm_mday == 24) && (tm->tm_mon == 11)) /* Heilig-Abend immer */
    return(1);

  if ((tm->tm_mday == 31) && (tm->tm_mon == 11)) /* Sylvester immer */
    return(1);

  comp_feier_tage(tm->tm_year + 1900);

  for (i = 0; i < A_FEI; i++)
    if ((t_ftag[i].monat == tm->tm_mon + 1) &&
        (t_ftag[i].tag == tm->tm_mday) &&
         t_ftag[i].telekom)
      return(1);

  return(0); /* Sorry, it's expensive! */

} /* billig */


double cheap(time_t when, int zone)
{
  auto int takt;


  switch (zone) {
    case 1 : takt = 360.00; break;
    case 2 : takt =  60.00; break;
    case 3 : takt =  21.00; break;
    case 4 : takt =  21.00; break;
    case 5 : takt =  12.00; break;
  } /* switch */

  if (billig(when)) {
    if (takt == 12.00)
      takt = 16.00;
    else
      takt *= 2.0;
  } /* if */

  return(takt);
} /* cheap */


double cheap96(time_t when, int zone)
{
  register int        i, takt = 0;
  auto 	   struct tm *tm;

  /* neue Tarife aus: iX 10/95, Pg 32 */
  static   double gebuehr[5][5] =
    { { 90.00, 90.00, 150.00, 240.00, 240.00 },
      { 26.00, 30.00,  45.00,  60.00, 120.00 },
      { 12.00, 13.50,  21.50,  30.00, 120.00 },
      { 11.50, 12.50,  20.00,  25.00, 120.00 },
      { 11.50, 12.50,  20.00,  25.00, 120.00 } };


  tm = localtime(&when);


  if ((tm->tm_wday == 6) || (tm->tm_wday == 0))  /* Samstag/Sonntag */
    takt = 4;

  if ((tm->tm_mday == 24) && (tm->tm_mon == 11)) /* Heilig-Abend */
    takt = 4;

  if ((tm->tm_mday == 31) && (tm->tm_mon == 11)) /* Sylvester */
    takt = 4;

  comp_feier_tage(tm->tm_year + 1900);

  for (i = 0; i < A_FEI; i++)
    if ((t_ftag[i].monat == tm->tm_mon + 1) &&
        (t_ftag[i].tag == tm->tm_mday) &&
         t_ftag[i].telekom)
      takt = 4;

  if (!takt) {
    if ((tm->tm_hour > 8) && (tm->tm_hour < 12))
      takt = 1;
    else if ((tm->tm_hour > 11) && (tm->tm_hour < 18))
      takt = 2;
    else if ((tm->tm_hour > 17) && (tm->tm_hour < 21))
      takt = 3;
    else if ((tm->tm_hour > 1) && (tm->tm_hour < 5))
      takt = 5;
    else
      takt = 4;
  } /* if */

  return(gebuehr[zone - 1][takt - 1]);
} /* cheap96 */
