#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH
GRAPHITE_PATH=/opt/graphite

CONFFILES="graphite.wsgi"
for i in $CONFFILES; do
    if [ ! -f ${GRAPHITE_PATH}/conf/$i ]; then
        echo "No pre-existing $i - creating from example."
        cp ${GRAPHITE_PATH}/conf/$i.example ${GRAPHITE_PATH}/conf/$i
    fi
done

if [ ! -f /etc/httpd/conf.d/graphite.conf ]; then 
    cat <<-EOF
	--IMPORTANT--
	Graphite VirtualHost configuration not found in default
	location. Graphite won't work without it. See sources for
	an example vhost configuration for WSGI!
	This script will now attempt to install the vHost file needed.
	--IMPORTANT--
	EOF
    cp ${GRAPHITE_PATH}/examples/example-graphite-vhost.conf /etc/httpd/conf.d/graphite.conf
fi

PYTHONPATH=${GRAPHITE_PATH}/webapp/ django-admin.py syncdb --noinput --settings=graphite.settings

chown -R apache.apache ${GRAPHITE_PATH}/storage
chown -R apache.apache ${GRAPHITE_PATH}/webapp
