#!/usr/local/bin/emacs -script

(cd "/home/joneworlds/gopher")

(shell-command "rm fridgescan.txt ; ./fridgescan.sh ; ./cleanscan.sh fridgescan.txt")

(with-current-buffer (find-file "fridgescan.txt")
       (replace-string-in-region "~" "" (point-min) (point-max))
       (replace-string-in-region "
       (goto-char (point-max))
       (insert "###\n")
       (goto-char (point-min))
       (let ((count 0))
               (while (search-forward "###" nil t)
                       (let ((a (goto-char (point-at-bol))))
                               (search-forward "###" nil t)
                               (delete-backward-char 3)
                               (let ((s (buffer-substring-no-properties (point-at-bol) (point))))
                                       (delete-line)
                                       (when (search-forward "###" nil t)
                                               (beginning-of-line)
                                               (newline)
                                               (forward-line -2)
                                               (set-fill-column 30)
                                               (fill-paragraph)
                                               (search-forward "###" nil t)
                                               (forward-line -1)
                                               (insert "\n- ")
                                               (insert s)
                                               (newline 2)
                                               (let ((indent (if (> (% count 2) 0) (random 5) (+ 15 (random 5)))))
                                                       (dotimes (i indent)
                                                               (indent-rigidly-right a (point)))
                                                       (setq count (+ count 1))
                                                       ))))))
       (untabify (point-min) (point-max))
       (goto-char (point-min))
       (insert "--== CIRCUMLUNAR FRIDGE ==--")
       (newline)
       (insert "refreshed ")
       (insert (current-time-string))
       (newline)
       (newline)
       (insert "Circumlunar colonists: to stick something on the fridge,\n")
 (insert "create a file named fridge in your gopher directory.\n\n")
       (write-file "fridgescan_j1.txt"))