#!/bin/sh
: ; exec klone $0 "$@"
; The above line finds the klone executable in the $PATH

(setq zirconlog "~/.zirconlog")
(setq cleaned-file "~/.koala.log")

(if (or (not (setq fdi (open  zirconlog :error ())))
    (not (setq fdo (open cleaned-file :direction :output :if-exists :supersede
	  :error ()
  ))))
  (progn
    (print-format *standard-error* "Problem opening files...\n")
    (exit 1)
  )
)

(if (not (setq name (getenv "IRCNICK"))) (progn
    (print-format *standard-error* "Please define the shell variable IRCNICK\n")
    (exit 1)
))

(setq lines (list))
(setq lineno 0)
(setq re-my-line (regcomp (+ "^[>][ ]|[*][ ]" (quote-string-for-regexp name) "[ ]")))
(setq my-lineno 0)

(while (setq line (read-line fdi ()))
  (lappend lines line)
  (if (regexec re-my-line line)
    (setq my-lineno lineno)
  )
  (incf lineno)
)

(dolist (line (subseq lines my-lineno))
  (write-line line fdo)
)

(close fdi)
(close fdo)

(wait (system (list "fix-koalabot-log" cleaned-file)))
(print-format "Ok, log updated in %0\n" cleaned-file)

(setq fd (open  zirconlog :direction :output :if-exists :supersede))
(dolist (line (subseq lines my-lineno))
  (write-line line fd)
)

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