#!/usr/bin/python3
#
# Copyright (C) 2015 Colin Walters <walters@verbum.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import os
import sys

if os.name == 'nt':
    datadir = os.path.join(os.path.dirname(__file__), '..', 'share')
else:
    datadir = "/usr/share"
if sys.version_info[0] < 3:
    import __builtin__
    __builtin__.__dict__['DATADIR'] = datadir
    __builtin__.__dict__['PKGLIBDIR'] = '/usr/lib64/rpmdistro-gitoverlay'
else:
    import builtins
    builtins.__dict__['DATADIR'] = datadir
    builtins.__dict__['PKGLIBDIR'] = '/usr/lib64/rpmdistro-gitoverlay'

# This is a private directory, we don't want to pollute the global
# namespace.
path = os.path.join('/usr/lib64/rpmdistro-gitoverlay')
sys.path.insert(0, path)

from rdgo import task_init, task_resolve, task_build, task_clone

commands = {
    "init" : [lambda: task_init.TaskInit(), "Initialize the directory"],
    "build" : [lambda: task_build.TaskBuild(), "Build the packages"],
    "resolve" : [lambda: task_resolve.TaskResolve(), "Perform a git mirror"],
    "clone" : [lambda: task_clone.TaskClone(), "Create a new build directory, sharing source"],
}

def usage(iserr):
    stream = sys.stderr if iserr else sys.stdout
    stream.write("Builtins:\n")
    for i, j in commands.items():
        stream.write("%s: %s\n" % (i, j[1]))
    sys.exit(1 if iserr else 0)

def main():
    if len(sys.argv) <= 1 or sys.argv[1] == '--help':
        usage(False)
    cmd = commands.get(sys.argv.pop(1))
    if cmd is None:
        sys.stderr.write("""Unknown argument: %s\n""" % (cmd, ))
        usage(True)
    else:
        cmd[0]().run(sys.argv[1:])
if __name__ == '__main__':
    main()
    pass
