#!/bin/sh
: ; exec klone $0 "$@"
; The above line finds the klone executable in the $PATH
;;encodes log lines from the spy daemon, masking the machine name
;;(stack-dump-on-error t)
;;(kdb t)

(setq args (getopts "USAGE: spy-encode [options] < input-file > output-file"
    ("-v" () verbose "verbose operation")
))

(defun main (&aux result)
  (catch 'EOF (while t 
      (if (setq result (process-line (read-line)))
	  (write-line result)
)))))

(defun process-line (line &aux
    (re {regcomp (+
	"^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) " ; 1 name of day
	"(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)  *" ; 2 month
	"([0-9]+) "			; 3 day
	"([0-9][0-9]:[0-9][0-9]:[0-9][0-9]) " ; 4 time string
	"([0-9][0-9][0-9][0-9])  *"	; 5 year (4-digits)
	"([^ ]+) *: +"			; 6 name of machine
	"([^ ]+)? +"			; 7 optional data
	"(STARTED)?$"			; 8 optional keywords
      )}
    )
  )
  (if (regexec re line) (with (
	match-start #[re 6 0]
	match-end #[re 6 1]
      )
      (+ (subseq line 0 match-start)
	(encode (subseq line match-start match-end))
	(subseq line match-end)
      )
    )
    (verbose? "line not understood: %r0\n" line)
  )
)

;; encode in base64, 5 chars
(defun encode (string &aux
    (value (*:hash string))
    ;; 64 digits
    (digits "_-aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789")
  )
  (String (list
;;      (get digits (mod (/ value 1073741824) 64))
      (get digits (mod (/ value 16777216) 64))
      (get digits (mod (/ value 262144) 64))
      (get digits (mod (/ value 4096) 64))
      (get digits (mod (/ value 64) 64))
      (get digits (mod value 64))
  ))
)

(main)

;;; EMACS MODES
;;; Local Variables: ***
;;; mode:lisp ***
;;; End: ***

