#!/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 Add Article Script.

; This script which incorporates one article into 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 read is set as the title of the article, therefore 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.  The script
; will ignore any number of blank lines before or after the title line.

; 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))

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

(next)
(setq result (exists db_file))

(setq db (sqlite_open db_file))

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

(unless result
   (setq sql 
      (concat "CREATE TABLE Articles (Id TEXT PRIMARY KEY UNIQUE, Date TEXT, Title TEXT, "
         "Body TEXT, Time INTEGER );"
         "CREATE INDEX t_idx ON Articles ( Time )"))

   (when (stringp (setq result (sqlite_exec db sql)))
      (die "sqlite_exec: " result))

   (chown "www" "www" db_file)
   (chmod "600" db_file))

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

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

(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 (and title body)
   (die "malformed article"))

(setq apos_rx (regcomp "'"))

(setq result
   (sqlite_exec db 
      (stringify "INSERT INTO Articles VALUES ('" (random 1000000) (time) (random 1000000) "','"
         (date) "','" 
         (substitute apos_rx "''" title 0) "','" 
         (substitute apos_rx "''" body 0) "'," (time) ")")))

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

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