#!/usr/local/bin/munger

; Copyright (c) 2009-2011 James Bailie.
; All rights reserved.
;
; Redistribution and use in source form, with or without modification, are
; permitted provided that the following conditions are met:
;
;     * Redistributions of source code must retain the above copyright
; notice, this list of conditions and the following disclaimer.
;     * The name of James Bailie may not be used to endorse or promote
; products derived from this software without specific prior written permission.
;
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
; POSSIBILITY OF SUCH DAMAGE.

; -------------------------------------------------------------------------

; Ephemera Replace Article Script.

; This script replaces the title and body of one article in the blog
; database.  It looks for a filename command-line argument to read from,
; and it this is not present, then it reads from stdin.

; The first line must be the Id field of the article to be affected.  When
; ephemera sends articles it puts an HTML commment before each article body,
; containing the Id for that article.  After reading the article Id, The
; script then skips one or more blank lines.

; The second non-blank line read is set as the title of the article.  The
; title must not be spread over multiple lines.  The script then skips one
; or more blank lines succeeding the title, and sets all text from that
; point on to the end of the file as the body of the article, enclosing
; groups of lines separated with blank lines in <p> elements.

; IMPORTANT!

; Markup may be embedded in the title and body text, as the user desires.
; Remember to use &amp; in place of the ampersand character (&), and &lt;
; in place of the open angle bracket (<).  The script will not escape those
; characters for you, because it cannot know which are part of intended
; markup and which are not.

; MORE IMPORTANT!

; This script gets the name of the SQLite database file from
; /usr/local/etc/ephemera.config.
; That file must be customized to configure ephemera before you run this
; script for the first time.

; This script must also be run as root, or the owner of the database file.

; ------------------------------------------------------------------------

(fatal)
(setq ephemera_version "2.9")

(load "/usr/local/etc/ephemera.config")
(setq db_file (join "/" db_path db_name))

; ------------------------------------------------------------------------

(unless (exists db_file)
   (die "Database file does not exist: " db_file))

(setq db (sqlite_open db_file))

(when (stringp db)
   (die "sqlite_open: " db))

(setq blank_rx (regcomp (concat "^[\b\t" (char 13) (char 10) "]*$")))

(next)
(when (next)
   (unless (> (redirect 0 (current)) 0)
      (die "could not open file")))

(setq id
   (catch
      (while (setq line (getline))
         (unless (match blank_rx line)
            (throw (chomp line))))))

(setq title
   (catch
      (while (setq line (getline))
         (unless (match blank_rx line)
            (throw (chomp line))))))

(setq body
   (catch
      (while (setq line (getline))
         (unless (match blank_rx line)
            (throw line)))))

(while (setq line (getline))
   (setq body (concat body line)))

(unless (and title body)
   (die "fatal error: either the article title or body, or both, are empty."))

(setq title (chomp title))

(setq term_rx (regcomp (concat (char 10) "{2}")))

(setq body
   (concat "<p>"
           (substitute term_rx "</p><p>" (chomp body) 0)
           "</p>"))

(unless body
   (die "malformed article"))

(setq apos_rx (regcomp "'"))

(setq result
   (sqlite_exec db
      (stringify "UPDATE Articles SET Title='"
            (substitute apos_rx "''" title 0) "',Body ='"
            (substitute apos_rx "''" body 0) "' WHERE Id='" id "';")))

(when (stringp result)
   (die "sqlite_exec: " result))

(die (exec "/usr/local/bin/ephemera-generate"))
