#! /usr/bin/env python
#
# The script to create empty issue database.
#
# Copyright (c) 2006-2007 The DITrack Project, www.ditrack.org.
#
# $Id: dt-createdb 1847 2007-08-04 11:59:23Z gli $
# $HeadURL: https://127.0.0.1/ditrack/src/tags/0.7/dt-createdb $
#
# Redistribution and use in source and binary forms, 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.
#  * 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 COPYRIGHT HOLDERS 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.
#

import os
import sys

def mkdir(path):
    try:
        os.mkdir(path)
    except:
        sys.stdout.write("Failed to mkdir '%s'\n" % path)
        sys.exit(1)

#############################################################################
# ENTRY POINT                                                               #
#############################################################################

if len(sys.argv) != 4:
    sys.stdout.write(
        """Syntax:
    %s <repository-url> <issue-db-dir> <local-wc>

The script non-recursively checks out <repository-url> into <local-wc> (which
should not exist), creates <issue-db-dir> there and schedules it for addition.

""" % os.path.basename(sys.argv[0]))
    sys.exit(1)

(repo, dbdir, localwc) = sys.argv[1:4]

if os.path.exists(localwc):
    sys.stdout.write("'%s' already exists\n" % localwc)
    sys.exit(1)

if os.system("svn co -Nq %s %s" % (repo, localwc)):
    sys.stdout.write("Can't check out working copy to %s\n" % localwc)
    sys.exit(1)

dbdir = os.path.join(localwc, dbdir)

mkdir(dbdir)
mkdir("%s/data" % dbdir)
mkdir("%s/etc" % dbdir)
mkdir("%s/meta" % dbdir)

open("%s/README.txt" % dbdir, "w").write(
"""This is an issue database maintained by DITrack.
Check out http://www.ditrack.org/ for more information.
"""
)

open("%s/etc/categories" % dbdir, "w").write(
"""Category: sample
Default-owner: sample-user
Version-set: sample-versions-set
"""
)

open("%s/etc/filters" % dbdir, "w").write(
"""sample-filter: Status=open,Owned-by=sample-user
"""
)

open("%s/etc/listing-format" % dbdir, "w").write(
"""[listing-formats]
default: %(id)-4s %(Owned-by)-8s %(Due-in)-8s %(Status)-6s %(Title)s
"""
)

open("%s/etc/users" % dbdir, "w").write(
"""sample-user
"""
)

open("%s/etc/versions" % dbdir, "w").write(
"""sample-versions-set: 0.1 0.2 / 0.3 0.4 / 0.5 0.6
"""
)

open("%s/meta/next-id" % dbdir, "w").write("1\n")

if os.system("svn add -q %s" % dbdir):
    sys.stdout.write("Can't schedule '%s' for addition\n" % dbdir)
    sys.exit(1)


if os.system("svn ps ditrack:format 3 %s" % dbdir):
    sys.stdout.write("Can't initialize format field\n")
    sys.exit(1)


if os.system("svn ps svn:ignore .ditrack %s" %dbdir):
    sys.stdout.write("Can't set the ignore list\n")
    sys.exit(1)



sys.stdout.write("""
Empty issue database created at:
'%s'

Now you should probably type something like:

svn commit %s

""" % (dbdir, dbdir))
