/******************************************************/
/*                                                    */
/*   TotalDuration.hip (C) Tomasz Kepa 09.VII.1996    */
/*                                 `                  */
/*   $VER: V1.0                                       */
/*                                                    */
/*     Calculates total duration of modulelist        */
/*                  in HippoPlayer.                   */
/*     The module list shouldn't be empty :-)         */
/*                      Enjoy!                        */
/*                                                    */
/*      Aha, read the doc! (TotalDuration_*.guide)    */
/*                                                    */
/******************************************************/

address 'HIPPOPLAYER'
options results

parse arg OPTION   /* parse options from command line */

if OPTION = '/?' | OPTION = '?' | upper(OPTION) = '-H' | upper(OPTION) = 'HELP' | upper(OPTION) = '-HELP' then
	do
		say
		say 'TotalDuration V1.0 (C) Tomasz Kepa 09.VII.1996'
		say
		say 'This script was written for use with HippoPlayer.'
		say 'It simply counts duration of whole list of modules =)'
		say
		say 'Usage:'
		say
		say 'TotalDuration.hip [SD=ShortDisplay] [ND=NoDisplay]'
		say
		exit
	end
                          /* clear all the variables: */

NO = 0                   /* number of modules in list */
CURRENT = 1               /* current position in list */
TOTAL = 0                           /* total duration */
DURATION = 0                /* duration of one module */
HIGH = 0                          /* highest duration */
LOW = 0                            /* lowest duration */
SD = FALSE
ND = FALSE

CUP = '[1A'                             /* cursor up */


get NFIL                 /* how many modules in list? */
NO = result

if upper(OPTION) = 'SD' | upper(OPTION) = 'SHORTDISPLAY' then SD = TRUE
else
if upper(OPTION) = 'ND' | upper(OPTION) = 'NODISPLAY' then ND = TRUE

                   /* the rest is obvious I hope. :-) */

if NO = 0 then
	do
		say 'Module list is empty. The duration is 0:00 :-)'
		exit
	end
else
do forever
	choose CURRENT
	play
	get DURA
	DURATION = value(result)
	HIGH = max(HIGH, DURATION)                 /* get the highest */
                                                      /* duration */
	if DURATION ~= 0 then                       /* and the lowest */
		do                                            /* duration */
			if LOW = 0 then LOW = DURATION
			else
			LOW = min(LOW, DURATION)
		end

	TOTAL = TOTAL + DURATION
	call CHANGETFORMAT DURATION
	DURF = result
	call CHANGETFORMAT TOTAL
	TOTF = result
	if ND = FALSE then say 'Checking mod nš 'CURRENT' of 'NO'. Duration is 'DURF'; 'TOTF' so far. '100 * CURRENT % NO'% done.'
	if NO < CURRENT + 1 then leave
		else
	if SD = TRUE then say CUP''CUP
	CURRENT = CURRENT + 1
end

say
call CHANGETFORMAT TOTAL
say 'Total duration of 'NO' modules is 'result' (that''s exactly 'TOTAL' seconds)'
call CHANGETFORMAT HIGH
HIG = result
call CHANGETFORMAT LOW
LW = result
call CHANGETFORMAT TOTAL % NO
AVG = result
say 'Highest duration: 'HIG', lowest duration: 'LW'. Average: 'AVG
return

/*******************************************************/
/*                                                     */
/* A little subroutine that calculates hh:mm:ss format */
/*               having amount of seconds.             */
/*                                                     */
/*******************************************************/

CHANGETFORMAT:
	parse arg DURATION    /* parse amount of seconds   */

	ZERO = ''             /* clear the variables       */
	HRS = ''              /* to prevent trashing the   */
                          /* subroutine =)             */

	MINS = DURATION % 60    /*     integer division    */
	SECS = DURATION // 60   /* remainder from division */
	HOURS = MINS % 60

	if SECS < 10 then ZERO = '0'  /* MM:0S happens :-) */
	if MINS >= 60 then MINS = MINS - (HOURS * 60)
	if HOURS ~= 0 then
		do
			HRS = HOURS':'
			if MINS < 10 then HRS = HRS'0'
		end
	return HRS''MINS':'ZERO''SECS
