program BootDate;
{
  This program serves a number of functions:

  1. Logging all system boots
  2. Monitoring disk space
  3. Reminding you when a backup was last performed
  4. Displaying the current date and time

  It outputs a line on the screen in the following format

  Date time C: D: Interval

  e.g.

  24-03-1990 08:50p 810k 2.05Mb 5 days

  where C: is the free space on drive C:, D: is the free space on drive D:,
  and Interval is the time since the last backup. This output is logged to
  a file called C:\BOOTDATE.LOG. If this file does not exist it is created.
  The date of the last backup is determined from the date stamp of a file.
  The default name for this file is C:\BACKUP.DAT however an alternative
  may be specified on the command line: BOOTDATE \TAPE\STREAM.LOG for e.g.
  If your backup procedure does not already involve the creation of a date
  stamped file you will need to amend it

  The program relies on DOS 3.x functionality for formatting dates in a
  country dependent manner and should not be run using earlier versions of
  DOS. Turbo Professional is required to recompile this program.

  Paul O'Nolan CIS 72007,242

  Helmstraat 28, The Hague 2584 AT, Netherlands
}
Uses
     TpCrt, TpDate, Dos, TpDos;
Var
     DateStamp,
     Bootline,
     FreeSpaceList,
     DriveCspace,
     DriveDspace,
     BackupDate,
     BackupInterval,
     DateAndTime,
     DateFormat,
     TimeFormat            : string;

     Julian1,
     Julian2               : date;     {type defined in TpDate}

     Logfile               : text;


procedure GetFreeSpaceOn(DriveToCheck: byte; var FreeSpaceDrive: string);
var
     ClustersAvailable,
     TotalClusters,
     BytesPerSector,
     SectorsPerCluster     : word;

     BytesPerCluster,
     DiskCapacity,
     FreeSpace             : longint;

begin
  if GetDiskInfo(DriveToCheck,
                 ClustersAvailable,
                 TotalClusters,
                 BytesPerSector,
                 SectorsPerCluster) then
    begin
      BytesPerCluster := LongInt(SectorsPerCluster) * LongInt(BytesPerSector);
      DiskCapacity    := LongInt(TotalClusters)     * BytesPerCluster;
      FreeSpace       := LongInt(ClustersAvailable) * BytesPerCluster div 1024;

      if FreeSpace > 1024 then
        begin
          str(FreeSpace / 1024:3:2,FreeSpaceDrive);
          FreeSpaceDrive := FreeSpaceDrive + 'Mb';
        end
      else
        begin
          str(FreeSpace,FreeSpaceDrive);
          FreeSpaceDrive := FreeSpaceDrive + 'k';
        end;
    end
  else FreeSpaceDrive := 'ERR';
end;


procedure GetLastBackupDate(DateFormat: string);
var
    F:              file;
    L:              LongInt;
    Daystr,
    Monthstr,
    Yearstr:        String;
    DT:             DateTime; {type defined in DOS unit}
    days,
    months,
    years:          Integer;

begin
  BackupDate     := DateStamp + ' not found';
  BackupInterval := 'Date of last backup unknown';

  if ExistFile(DateStamp) then
    begin
      Assign(F,DateStamp);
      Reset(F);
      GetFTime(F,L);
      Close(F);
      UnPackTime(L,DT);

      BackupDate := DMYtoDateString(DateFormat, DT.day, DT.Month, DT.Year);
      Julian2 := DMYtoDate(DT.day, DT.month, DT.year);
      GetDate(DT.year, DT.month, DT.day, DT.min); {min receives dayofweek}
      Julian1 := DMYtoDate(DT.day, DT.month, DT.year);
      DateDiff(Julian1,Julian2,days,months,years);

      str(days, daystr);
      str(months, monthstr);
      str(years, yearstr);

      case years of
        0: BackupInterval := '';
        1: BackupInterval := yearstr + ' year,  '
        else
          BackupInterval := yearstr + ' years, ';
      end;

      if months = 1 then
        BackupInterval := BackupInterval + monthstr + ' month,  '
      else if (years > 0) or (months > 0) then
        BackupInterval := BackupInterval + monthstr + ' months, ';

      case days of
        0: if months + years = 0 then
             BackupInterval := 'Today';
        1: BackupInterval := BackupInterval + daystr + ' day ';
        else BackupInterval := BackupInterval + daystr + ' days ';
      end; {case}

    end;
end;


procedure GetDateAndTime;
begin
  DateFormat := InternationalDate(true,false);
  TimeFormat := InternationalTime(false,true,false,false);
  DateAndTime := TodayString(DateFormat)+' '+CurrentTimeString(TimeFormat);
end;


begin
  if Paramstr(1) = '' then
    DateStamp := 'C:\BACKUP.DAT'
  else DateStamp := ParamStr(1);

  GetDateAndTime;
  GetFreeSpaceOn(3,DriveCspace);
  GetFreeSpaceOn(4,DriveDspace);
  GetLastBackupDate(DateFormat);

  FreeSpaceList := DriveCspace + ' ' + DriveDspace;
  Bootline      := DateAndTime + ' ' + FreeSpaceList + ' ' + BackupInterval;

  Assign(logfile,'C:\BOOTDATE.LOG');
  if ExistFile('C:\BOOTDATE.LOG') then
    Append(logfile)
  else Rewrite(logfile);

  Writeln(Bootline);
  Writeln(logfile,Bootline);

  Close(logfile);
end.
