//*********************************************************************
//*                     DB-Reminder  2021-10-24                       *
//*                         >> OnStartup <<                           *
//*                                                                   *
//* Dieses Script komprimiert (packt) die Datenbank von Dialog nach   *
//* x Tagen (Standard sind 7 Tage)                                    *
//*                                                                   *
//* Funktionalitaet: [x] neutral                                      *
//*                  [ ] nur Basis_Modul                              *
//*                  [ ] nur Pathfinder                               *
//*                                                                   *
//* Datum  : unbekannt                                                *
//* Autoren: unbekannt                                                *
//* Stand  : 24.10.2021                                               *
//*                                                                   *
//* DateiName : _i_OSt_DBReminder.ds                                  *
//* Einbindung: {$I _i_OSt_DBReminder.ds}                             *
//* Aufruf    : DatabaseReminder;                                     *
//*                                                                   * 
//*********************************************************************
procedure Init_DataBaseReminder ( var myDialogDataINI : String;
                                  var remindAfter     : Byte
);
begin
//{-------------------------------------------------------------------}
//{                 Anwenderspezifische Einstellungen                 }
//{-------------------------------------------------------------------}

  // the path to the 'default.ini' file in your dialog's data folder
  myDialogDataINI := GetCurrentDir + '\data\' + 'default.ini';

  // the time that needs to pass between the last compaction date and
  // the current time. the number is specified in days and defaults to 7,
  // but fractions are possible: 7.5 would warn you if seven and a half
  // days passed since the last time the database was compacted.
  remindAfter := 7;
  
//{-------------------------------------------------------------------}
//{                       Ende der Einstellungen                      }
//{-------------------------------------------------------------------}

//{===================================================================}
//{           !!!  Ab hier bitte nichts mehr ändern  !!!              }
//{===================================================================}
end;

const
  // set this to true if dialog's installed on a FAT partition
  hackFAT = false;

  // the text in the message box that pops up when it's time to compact.
  // the '%days%' variable will contain the number specified above.  
  // the '%lastcompact%' variable will contain the date and time
  // the database was last compacted on.
  mbReminderWarning = 'Die Datenbank wurde zuletzt am %lastcompact% gepackt. Dieses geschah vor mehr als %days% Tag(en). Jetzt packen?';
  
  //strings for debugging purposes
  mbFileNotFound = 'Die Datei "%fname%" konnte nicht gefunden werden';
  mbLastCompact = 'Zuletzt gepackt am: %date%';
  
  // message box captions
  mbCaption = 'Erinnerung: Die Datenbank sollte mal wieder gepackt werden!'; 
  mbDbgCaption = 'Debug DB reminder';
  
var
  // holds the file handle
  fHandle : THandle;      
  // tFileTime variables; we only use the creation time in this script
  myCreationTime, myLastAccessTime, myLastWriteTime : tFileTime;
  // the original file date and time
  myUTCSystemTime : tSystemTime;
  // file date and time with time zone compensation
  mySystemTime : tSystemTime;
  // these will hold the date and time values in pascal-usable form
  myFileDate, myFileTime, myFileDateTime : tDateTime;
  // the final messagebox string
  myReminderWarning : string;
  // string for debugging purposes
  strTxt : string;

procedure DatabaseReminder;

var   
   myDialogDataINI : String;
   remindAfter     : Byte;
      
Begin
   Init_DataBaseReminder (myDialogDataINI, remindAfter);
   //check existance of file 
   if (FileExists(myDialogDataINI) = false) then begin 
      strTxt := stringReplace(mbFileNotFound, '%fname%', myDialogDataINI, [rfReplaceAll]);
      MsgBox(0, strTxt, mbDbgCaption, MB_OK);
   end else begin
      // we need a special file handle, normal ones won't do
      fHandle := createFile(myDialogDataINI, GENERIC_READ, FILE_SHARE_READ, '', OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    
      // obtain the file creation, access and modified times
      getFileTime(fHandle, myCreationTime, myLastAccessTime, myLastWriteTime);
      closeHandle(fHandle); 
  
      // FAT file system "workaround"
      if hackFAT
         // transform last modified file time into system time
      then fileTimeToSystemTime(myLastWriteTime, myUTCSystemTime)
         // transform creation file time into system time
      else fileTimeToSystemTime(myCreationTime, myUTCSystemTime);
  
      // compensate for the local time zone
      systemTimeToTzSpecificLocalTime('', myUTCSystemTime, mySystemTime);

      // encode the system time into two tDateTime structures and merge them  
      myFileDate := encodeDate(mySystemTime.wYear, mySystemTime.wMonth, mySystemTime.wDay);
      myFileTime := encodeTime(mySystemTime.wHour, mySystemTime.wMinute, mySystemTime.wSecond, mySystemTime.wMilliseconds);
      myFileDateTime := myFileDate + myFileTime;
     
      //Debugging - enable MessageBox(..) for more information
      strTxt := stringReplace(mbLastCompact, '%date%', dateTimeToStr(myFileDateTime), [rfReplaceAll]);
      //MessageBox(0, strTxt, mbDbgCaption, MB_OK);    

      // check the number of days that have passed between the current date/time
      // and the last compact date
      if (now - myFileDateTime) >= remindAfter then begin
         // replace the '%date%' in the reminder warning text with the actual number
         // and the '%lastcompact%' with the string-formatted last compact date
         myReminderWarning := stringReplace(mbReminderWarning, '%days%', floatToStrF(remindAfter, ffGeneral, 15, 1), [rfReplaceAll]);
         myReminderWarning := stringReplace(myReminderWarning, '%lastcompact%', dateTimeToStr(myFileDateTime), [rfReplaceAll]);

         // pop up a message box and see if the user wants to compact
         if (MsgBox(0, myReminderWarning, mbCaption, MB_YESNO) = IDYES) then
            ADoLater('CompactDatabase');                                                 
      end; // if
   end;  // if
End;  // procedure