#!/usr/bin/env python
# Copyright 2011 (C) Adam Greig
#
# This file is part of habitat.
#
# habitat is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# habitat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with habitat.  If not, see <http://www.gnu.org/licenses/>.

"""
Upload all the design docs in habitat/couchdb/_design to the database specified
on the command line. You may have to be a database administrator to do this;
specify user credentials in the database URI.
"""

import sys
import os.path
import couchdbkit

if len(sys.argv) != 3:
    print "Usage: {0} database_url database_name".format(sys.argv[0])
    sys.exit(1)

server = couchdbkit.Server(sys.argv[1])
db = server[sys.argv[2]]

path = os.path.abspath(__file__)
path = os.path.split(path)[0]
path = os.path.join(path, "..", "couchdb", "_design")

if not os.path.isdir(path):
    print "Could not find _design folder"

loader = couchdbkit.loaders.FileSystemDocsLoader(path)
loader.sync(db, verbose=True)
