/*-----------------------------------------------------------------------
                       D A T A N U M B E R
                      
   Implementation file for the data number module.
   
   Mark Koennecke, Juli 1997
   
       Copyright:

       Labor fuer Neutronenstreuung
       Paul Scherrer Institut
       CH-5423 Villigen-PSI


      The authors hereby grant permission to use, copy, modify, distribute,
      and license this software and its documentation for any purpose, provided
      that existing copyright notices are retained in all copies and that this
      notice is included verbatim in any distributions. No written agreement,
      license, or royalty fee is required for any of the authorized uses.
      Modifications to this software may be copyrighted by their authors
      and need not follow the licensing terms described here, provided that
      the new terms are clearly indicated on the first page of each file where
      they apply.

      IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
      FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
      ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
      DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
      POSSIBILITY OF SUCH DAMAGE.

      THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
      INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
      FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
      IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
      NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
      MODIFICATIONS.
----------------------------------------------------------------------------*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include "fortify.h"
#include "conman.h"
#include "obdes.h"
#include "danu.h"

/* ------------------ the data structure ----------------------------------*/
   typedef struct __DataNumber {
                                 pObjectDescriptor pDes;
                                 char *pFileName;
                               } DataNumber;                         
/*-------------------------------------------------------------------------*/
    pDataNumber CreateDataNumber(char *pFileName)
    {
      pDataNumber pNew = NULL;
      FILE *fd = NULL;
      
      pNew = (pDataNumber)malloc(sizeof(DataNumber));
      if(!pNew)
      {
        return NULL;
      }
      memset(pNew,0,sizeof(DataNumber));
      
      pNew->pDes = CreateDescriptor("DataNumber");
      if(!pNew->pDes)
      {
         free(pNew);
         return NULL;
      }
      
      /* check filename */
      fd = fopen(pFileName,"r");
      if(!fd)
      {
          printf("Serious error: cannot open file for Data Number!!!!\n");
          printf("I continue, but you should not write data files!\n");
          pNew->pFileName = strdup("default.num");
          return pNew;
      }
      fclose(fd);
      pNew->pFileName = strdup(pFileName);
      return pNew;
    }
/*--------------------------------------------------------------------------*/
    void        DeleteDataNumber(void *pData)
    {
      pDataNumber self = NULL;
      
      self = (pDataNumber)pData;
      assert(self);
      
      if(self->pDes)
      {
        DeleteDescriptor(self->pDes);
      }
      if(self->pFileName)
      {
        free(self->pFileName);
      }
      free(self);
    }
/*-------------------------------------------------------------------------*/
    int IncrementDataNumber(pDataNumber self, int *iYear)
    {
       FILE *fd = NULL;
       int iNum;
       time_t iTime;
       struct tm *psTime;
       
       /* open file */
       fd = fopen(self->pFileName,"r");
       if(!fd)
       {
         return -1;
       }
       
       
       /* get and increment number */
       fscanf(fd,"%d",&iNum);
       iNum++;
       fclose(fd);

       /* reopen for rewriting */
       fd = fopen(self->pFileName,"w");
       if(fd == NULL)
       {
          return -1;
       }
       
       /* write file and leave */
       fprintf(fd," %d \n",iNum);
       fprintf(fd,"NEVER, EVER modify or delete this file\n");
       fprintf(fd,"You'll risk eternal damnation and a reincarnation as a cockroach!|n");
       fclose(fd);
       
       /* get year */
       iTime = time(NULL);
       psTime = localtime(&iTime);
       *iYear = psTime->tm_year + 1900;
       
       return iNum;
    }
/*-------------------------------------------------------------------------*/
    int DecrementDataNumber(pDataNumber self)
    {
       FILE *fd = NULL;
       int iNum;
       
       /* open file */
       fd = fopen(self->pFileName,"r");
       if(!fd)
       {
         return -1;
       }
       
       
       /* get and decrement number */
       fscanf(fd,"%d",&iNum);
       iNum--;
       if(iNum < 0)
           iNum = 0;
       fclose(fd);

       /* reopen for rewriting */
       fd = fopen(self->pFileName,"w");
       
       /* write file and leave */
       fprintf(fd," %d \n",iNum);
       fprintf(fd,"NEVER, EVER modify or delete this file\n");
       fprintf(fd,"You'll risk eternal damnation and a reincarnation as a cockroach!|n");
       fclose(fd);

       return iNum;
    }
/*-------------------------------------------------------------------------*/
    int DNWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
                  int argc, char *argv[])
    {
       pDataNumber self = NULL;
       FILE *fd = NULL;
       int iNum, iYear;
       char pBueffel[512];
       
       self = (pDataNumber)pData;
       assert(self);
       assert(pCon);
       
       argtolower(argc,argv);
       if(argc < 2) /* value request */
       {
          fd = fopen(self->pFileName,"r");
          if(!fd)
          {
            sprintf(pBueffel,"ERROR: cannot open file %s",self->pFileName);
            SCWrite(pCon,pBueffel,eError);
            return 0;
          }
          fscanf(fd,"%d",&iNum);
          fclose(fd);
          sprintf(pBueffel,"%s = %d",argv[0],iNum);
          SCWrite(pCon,pBueffel,eValue);
          return 1;
       }
       
       if(strcmp(argv[1],"incr") == 0)
       {
         iNum = IncrementDataNumber(self,&iYear);
         if(iNum > 0)
         {
           SCSendOK(pCon);
           return 1;
         }
         else
         {
           sprintf(pBueffel,"ERROR: cannot increment %s",argv[0]);
           SCWrite(pCon,pBueffel,eError);
           return 0;
         }
       }
       
       sprintf(pBueffel,"ERROR: unknown command %s supplied to %s",
               argv[1], argv[0]);
       SCWrite(pCon,pBueffel,eError);
       return 0;        
    }           
/*------------------------------------------------------------------------*/
    int DEWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
                  int argc, char *argv[])
    {
       pDataNumber self = NULL;
       int iNum;
       char pBueffel[512];
       
       self = (pDataNumber)pData;
       assert(self);
       assert(pCon);

       if(SCMatchRights(pCon,usMugger))
       {
         DecrementDataNumber(self);
         SCWrite(pCon,"Data file killed",eWarning);
         return 1;
       }
       else
       {
         SCWrite(pCon,"ERROR: you are not authorized to kill data files",
                 eError);
         return 0;
       }
    }

/*-------------------------------------------------------------------------*/
    int DNFactory(SConnection *pCon, SicsInterp *pSics, void *pData,
                  int argc, char *argv[])
    {
       pDataNumber self = NULL;
       char pBueffel[512];
       int iRet;
       
       if(argc < 3)
       {
          SCWrite(pCon,
          "ERROR: not enough arguments provided to make DataNumber",eError);
          return 0; 
       }
       
       self = CreateDataNumber(argv[2]);
       if(!self)
       {
         SCWrite(pCon,"ERROR: no memory to create data number",eError);
         return 0;
       }
       
       iRet = AddCommand(pSics, argv[1],DNWrapper, DeleteDataNumber, self);
       if(!iRet)
       {
          sprintf(pBueffel,"ERROR: duplicate command %s not created",argv[1]);
          SCWrite(pCon,pBueffel,eError);
          return 0;
       }
       iRet = AddCommand(pSics,"killfile",DEWrapper,NULL, self);
       if(!iRet)
       {
          sprintf(pBueffel,"ERROR: duplicate command %s not created",argv[1]);
          SCWrite(pCon,pBueffel,eError);
          return 0;
       }
       return 1;
    }              

  
