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

(if (= *machine* 'amiga)
  (setqn temp-dir (+ "T:Show" (String *current-process-id*))
    inbox "W:Show"
    inbox-radix (expand-filename "W:Show")
    more-command "ppmore"
  )
  (setqn temp-dir (+ "/tmp/Show" (String *current-process-id*))
    inbox (expand-filename "~/Mail/inbox")
    inbox-radix (expand-filename "~/Mail")
    more-command "more"
  )
)

(setq inbox-orig inbox)
(trap-signal 2 (+ "rm -rf " temp-dir))
(trap-signal 1 (+ "rm -rf " temp-dir))
(setq re-blank (regcomp "^[ \t]*$"))
(setq printable-headers '("Date" "From" "To" "Subject" "Cc"))
(setq re-folder (regcomp "^[+](.*)$"))

(defun main (&aux (files (list)))
  (setq *current-directory* inbox)
  (wait (system (list "rm" "-rf" temp-dir)))
  (wait (system (list "mkdir" temp-dir)))
  (dolist (file (subseq *arguments* 1))
    (if (regexec re-folder file)
      (setq *current-directory* (+ inbox-radix "/" (regsub re-folder 1)))
      (if (copy-file file temp-dir) (put files -1 file))
    )
  )
  (setq *current-directory* temp-dir)
  (wait (system (+ (list more-command) files)))
  (Exit 0)
)

(defun copy-file (file dir &aux
    header-item
    line
    (previous-line-blank t)
    (fd (open file :error
	'(progn (print-format "Show: cannot open %0, aborting.\n" file)
	  (Exit 1)
    )))
    (fo (open (+ dir "/" file) :direction :output :error
	'(progn (print-format "Show: cannot create %0, aborting.\n"
	    (+ dir "/" file))
          (Exit 1)
    )))
  )
  (print-format fo
    "********************************************************************* %0\n"
    file
  )
  (while (setq header-item (read-header-item fd))
    (if (seek printable-headers (getn header-item 0))
      (write-line (getn header-item 1) fo)
  ))
  (write-line "" fo)
  (while (setq line (read-line fd ()))
    (if (regexec re-blank line) (progn
	(if previous-line-blank () (write-line "" fo))
	(setq previous-line-blank t)
    ))
    (write-line line fo)
  )
  t
)

(setq read-line-buf:buffer ())
(defun read-line-buf (fd err &aux res)
  (if read-line-buf:buffer (progn
      (setq res read-line-buf:buffer)
      (setq read-line-buf:buffer ())
      res
    )
    (read-line fd err)
))
(defun push-line-buf (line)
  (setq read-line-buf:buffer line)
)

(setq re-header-name (regcomp "^[^: \t]+"))
(setq re-header-continuation (regcomp "^[ \t]+[^: \t]+"))

(defun read-header-item (fd &aux
    header-name
    contents
  )
  (if (not (regexec re-blank (setq line (read-line-buf fd "")))) (progn
      (regexec re-header-name line)
      (setq header-name (regsub re-header-name 0))
      (setq contents line)
      (while (regexec re-header-continuation (setq line (read-line-buf fd "")))
	(setq contents (+ contents "\n" line))
      )
      (push-line-buf line)
      (list header-name contents)
)))

(defun Exit (n)
  (setq *current-directory* inbox)
  (if (= *machine* 'amiga)
    (wait (system (list "wait" "1")))
  )
  (wait (system (+ "rm -rf " temp-dir)))
  (exit n)
)

(main)
      
