#!/usr/local/bin/munger

; Copyright (c) 2009-2011x 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 Extract Article Script.

; This script extracts the Id, Title, and Body of one articles in the blog
; database.  It writes these items to stdout.  This data may be redirected
; to a file, edited and reinserted into the database with the ephemera-replace
; script.

; The script requires the article Id of the desired article to be passed
; as a command-line argument.  If you look at the source of a web page 
; displaying the article, the Id is included before the body text as an
; HTML comment, resembling this:  <!-- 359021242161354530239 -->  The
; long number is the article Id for that article.

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

(next)
(unless (next)
   (die "Article Id not provided"))

(setq db (sqlite_open db_file))

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

(setq result
   (sqlite_exec db
      (stringify "SELECT Title,Body FROM Articles WHERE Id='" (current) "';")))

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

(println (current))
(newline)

(println (car (cadr result)))
(newline)

(setq p_rx (regcomp "</?p>"))
(setq pp_rx (regcomp "</p><p>"))
(setq break (concat (char 10) (char 10)))

(print (substitute p_rx "" (substitute pp_rx break (cadr (cadr result)) 0) 0))
(quit)
