#!/bin/sh
#
# planet2wordpress.sh V1.0
# Copyright (c) 2005 Aubrey Kilian
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
#

########### Edit these values
DB_NAME=wordpress
DB_USER=wp
DB_PASS=wp
DB_HOST=localhost
DB_TABLE=wp_posts
DB_ID=97
PLANETDIR=/home/aubrey/planet/
PLANETCONFIG=myplanet/config.ini

########### Optionally edit there
PYTHONEXE=/usr/local/bin/python
MYSQLEXE=/usr/local/bin/mysql

########### Edit below this line at your own risk
INPUTFILE=$1
OUTPUTFILE=`mktemp /tmp/p2w.XXXXXX`

cd ${PLANETDIR}
${PYTHONEXE} planet.py "${PLANETCONFIG}"

# Create the sql file, wrapping the UPDATE and WHERE clauses around the
# content

# SQL 'header'
echo "UPDATE ${DB_TABLE} SET post_content = '" > "${OUTPUTFILE}"
# Sanitise the actual content, don't want no single ticks breaking SQL
cat ${INPUTFILE} | sed -e "s/'/\\\'/g;" >> "${OUTPUTFILE}"
# SQL 'footer'
echo "' WHERE ID=${DB_ID}" >> "${OUTPUTFILE}"

# Execute the SQL
${MYSQLEXE} -u "${DB_USER}" -p"${DB_PASS}" -h "${DB_HOST}" "${DB_NAME}" < "${OUTPUTFILE}"

# Cleanup, we don't want to end up with tons of ugly tmp files
rm "${OUTPUTFILE}"

