#ifndef XPKMASTER_PROGRESS_C
#define XPKMASTER_PROGRESS_C

/* Routinesheader

	Name:		progress.c
	Main:		xpkmaster
	Versionstring:	$VER: progress.c 1.1 (24.03.97)
	Author:		SDI
	Distribution:	PD
	Description:	Progress report handler

 1.0   06.10.96 : first real version
 1.1   24.03.97 : added auto hook, changed speed calculation
 1.2   25.04.97 : changed time calculation
*/

#include <exec/types.h>
#include <pragma/intuition_lib.h>
#include "xpkmaster.h"

LONG callprogress(struct XpkBuffer *xbuf)
{
  struct XpkProgress *prog = &xbuf->xb_Prog;
  struct Hook *hk = xbuf->xb_ChunkHook;
  struct XpkPrefsSemaphore *sem = 0;

  if(!hk && (xbuf->xb_Flags & XMF_AUTOPRHOOK) && (sem = GetPrefsSem()))
    hk = sem->xps_ProgressHook;

  if(hk)
  {
    if(prog->xp_UCur && prog->xp_ULen)
    {
      ULONG secs;
      LONG mics;

      CurrentTime (&secs, (ULONG *) &mics);
      secs -= xbuf->xb_Secs;
      mics -= xbuf->xb_Mics;

      /* 7813 = 100000 / 128, 0x20000000 = 0x100000000/128 (ULONG size),
         +1 prevents division by zero */
      if(prog->xp_UCur < 0x20000000)
        prog->xp_Speed = (prog->xp_UCur<<7) / ((secs<<7) + mics/7813 + 1);
      else
        prog->xp_Speed = prog->xp_UCur / (secs + 1);

      prog->xp_Done = 100 * prog->xp_UCur/prog->xp_ULen;
      prog->xp_CF = 100 - 100 * prog->xp_CCur / prog->xp_UCur;
    }
    if(prog->xp_CF < 0)
      prog->xp_CF = 0;

    if(MyCallHookPkt(hk, prog A4SUPP2))
      xbuf->xb_Result = XPKERR_ABORTED;
  }

  if(sem)
    ReleaseSemaphore((struct SignalSemaphore *) sem);

  return xbuf->xb_Result;
}

#endif /* XPKMASTER_PROGRESS_C */
