#! /usr/local/bin/4  ss
\ F8/4 SOURCE v0.11.xx, xec.file "quotaq"
\
\ read stdin or file "./sna-dir.txt", ret MB KB BYTES used space.
\	ls -l . | quotaq
\ displays dir and sum of files & sizes, generates html text.
\ any output from <ls -l...> can be piped or redirected in.
\ silent opr. it stdin and stdout is file/pipe, output summary, only.
\ a single, quoted string argument can passed in for immediate evaluation.

\ cr order cr
\ f" util.f8" included	\ not required, used when testing
\ search order
only linux also forth also hidden also fig also ans also forth 
prevdef also definitions decimal
default-is [type]
: digits #digits ;
\ sole, "evaluate"d argument recognized is "hpg5" to omitting the http base-ref
\ if a name "hpg5" exists, such that sna-dir.html would refer to local copies.
\ e.g.		quotaq "0 constant hpg5"
3 argstg evaluate

\ -------------------------------------------------------------------------------
\                       - config -
\ -------------------------------------------------------------------------------

\ - def'd for pipe/files handling --- config ------------------------------------

0. 2variable dir-used dir-used 2!	\ file sizes accu
0 value n	\ lines count
0 value p 	\ {read-line}	ptr
0 value u	\		stg len
0 value file	\ html output

\ - input name default if stdin is console --------------------------------------

\ {f"} returns tested in-chan path+name
f" ./sna-dir"	-dup 0= [IF] drop	\ 1st choice
f" ./sna-dir.txt" 			\ if 1st not found
[THEN]
    dup stg df1	\ returns ( -- p 0 ) if neither one found

\ -------------------------------------------------------------------------------
\ 			- data, words -
\ -------------------------------------------------------------------------------

\ - html strings ----------------------------------------------------------------

string ' <a name="' dup stg n0
string ' "></a>' dup stg n1
string '  <a href="' dup stg h1 also vt <ht> previous h1 drop c!
string ' ">' dup stg h2
string ' </a>' dup stg h3
string ' <html><head><title>latest files</title>' pad place
if-nfound hpg5 string ' <base href="http://home.snafu.de/phpr/">' pad +place
string ' </head><body><h4><a href="">http://home.snafu.de/phpr</a></h4><p><pre>' pad +place 
pad count dup stg b1
string ' </p><!-- (C) 2001 lxhp@china.com --></body></html>' dup stg b2
string ' </pre></p><blockqupte><font size="-1"><p> &nbsp; Sa. ' dup stg s1
string ' </p><p> &nbsp; generated w/ <a href="quotaq">quotaq</a> f8 script</p></font></blockquote>' dup stg s2


\ - i/o redirection, aux. words -------------------------------------------------

\ skip <bl> delimited text section
: bl-sc bl scan bl skip ;

in-chan pad fstat w@ 0=		\ whether input is PIPEd
[if]
: get-char	( p -- u )
  locals| p |
  begin p 1 in-chan read-file [ s" AGAIN" errno ] literal + until ;
: get-line
  pad ddup $100 bounds do drop i get-char 0= i c@ $a = or -leave i loop over- dup 0> 
  [ out-chan cons? ] [if] >r 2dup type cr r> [then]
;
[else]				\ or file/console
: get-line
  pad dup $100 in-chan read-line -dup 
  if [ s" EAGAIN" errno negate ] literal =/= endif
  [ out-chan cons? ] [if] >r 2dup type cr r> [then] ;
[then]

: xf file stdout xg-chan ;	\ xchange stdout w/ file
: xo stdout out-chan xg-chan ;	\ xchange stdout w/ supplied output channel
: odu				\ send summary to stdout
  dir-used 2@ 1024 dup * m/mod 0 .r "M emit space 
  1024 / 0 .r "K emit space n #. ." Files " 
  "( emit dir-used 2@ 1024 dup u* also rational #digits to #fplaces df. previous ")M emit 
  dir-used 2@ 10 ud.r ."  BYTES, " .time&date
  cr ;

: qdu xo odu xo ;		\ display summary to console/supplied channel
: fdu s1 file write-line -exit xf odu xf s2 file write-line drop ; \ to file

\ -------------------------------------------------------------------------------
\			- xec prog -
\ -------------------------------------------------------------------------------

\ - init/open file ---------------------------------------------------------------

in-chan cons? [if]				\ def. in-chan if stdin is console
	df1 r/o open-file 0= if bye-r endif
	in-chan is-chan drop
[then]

pad 400 in-chan ch-name pad place
s" .html" pad +place 
pad count ": scan if "# over c! endif drop	\ use modified name if in-chan is a pipe
pad count r/w &644 mcreate-file			\ set perm to -rw-rw-r-
if drop new-chan dup is-chan endif to file	\ secondary, html-text output channel

get-line 3drop	\ drop header line

\ - count used space, generate linked directory ----------------------------------

file ch-id 0> if
    b1 file write-line drop
    begin
    get-line in-chan ch-rnum 0>
    while
    if
	over to p dup to u 			\ save line & length
	bl-sc bl-sc bl-sc bl-sc
	-dup if 
		1 +to n 0. 2over >number 2drop dir-used 2+! \ acc. file size
		bl-sc bl-sc bl-sc bl-sc		\ advance to filename
		2dup ". scan sdrop over- negate 2 pick swap	\ name="...
		n0 file write-file 2drop file write-file 2drop n1 file write-file 2drop
		h1 file write-file 2drop 	\ begin html entry
		file write-file 2drop
		h2 file write-file 2drop
		p u 
		    bl-sc bl-sc bl-sc bl-sc
		    2dup bl-sc drop also vt <ht> previous swap 1- c!
		    file write-file drop
		h3 file write-line 2drop
	endif
    endif
    repeat
    fdu b2 file write-line drop
else
begin
    get-line in-chan ch-rnum 0>
while
    if
	over to p dup to u 			\ save line & length
	bl-sc bl-sc bl-sc bl-sc
	-dup if 
	    1 +to n 0. 2swap >number 2drop dir-used 2+! 
	endif
    endif
repeat
endif

qdu 		\ display/write result
0 sp@ sys exit 	\ out-comment for testing...
