#!/usr/bin/python

import os
import shutil
import sys
import glob


def find_root():
    # expected path is in <top_dir>/packages/
    top_dir = os.environ.get("CLOUD_INIT_TOP_D", None)
    if top_dir is None:
       top_dir = os.path.dirname(os.path.dirname(os.path.abspath(sys.argv[0])))
    if os.path.isfile(os.path.join(top_dir, 'setup.py')):
        return os.path.abspath(top_dir)
    raise OSError(("Unable to determine where your cloud-init topdir is."
                       " set CLOUD_INIT_TOP_D?"))

# Use the util functions from cloudinit
sys.path.insert(0, find_root())

from cloudinit import templater
from cloudinit import util

import argparse

# Package names that will showup in requires to what we can actually
# use in our debian 'control' file, this is a translation of the 'requires'
# file pypi package name to a debian/ubuntu package name.
PKG_MP = {
    'boto': 'python-boto',
    'configobj': 'python-configobj',
    'oauth': 'python-oauth',
    'pyyaml': 'python-yaml',
    'prettytable': 'python-prettytable',
    'argparse': 'python-argparse',
    'cheetah': 'python-cheetah',
}


def write_debian_folder(root, version, revno):
    deb_dir = util.abs_join(root, 'debian')
    os.makedirs(deb_dir)
    
    # Fill in the change log template
    templater.render_to_file(util.abs_join(find_root(),
                             'packages', 'debian', 'changelog.in'),
                             util.abs_join(deb_dir, 'changelog'),
                             params={
                                 'version': version,
                                 'revision': revno,
                             })

    # Write out the control file template
    cmd = [util.abs_join(find_root(), 'tools', 'read-dependencies')]
    (stdout, _stderr) = util.subp(cmd)
    pkgs = [p.lower().strip() for p in stdout.splitlines()]

    # Map to known packages
    requires = []
    for p in pkgs:
        tgt_pkg = PKG_MP.get(p)
        if not tgt_pkg:
            raise RuntimeError(("Do not know how to translate pypi dependency"
                                " %r to a known package") % (p))
        else:
            requires.append(tgt_pkg)

    templater.render_to_file(util.abs_join(find_root(),
                                           'packages', 'debian', 'control.in'),
                             util.abs_join(deb_dir, 'control'),
                             params={'requires': requires})
 
    # Just copy the following directly
    for base_fn in ['dirs', 'copyright', 'compat', 'pycompat', 'rules']:
        shutil.copy(util.abs_join(find_root(),
                                  'packages', 'debian', base_fn),
                    util.abs_join(deb_dir, base_fn))


def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("-n", "--no-sign", dest="sign",
                        help=("attempt to sign "
                              "the package (default: %(default)s)"),
                        default=True,
                        action='store_false')
    parser.add_argument("-v", "--verbose", dest="verbose",
                        help=("run verbosely"
                              " (default: %(default)s)"),
                        default=False,
                        action='store_true')
    args = parser.parse_args()

    capture = True
    if args.verbose:
        capture = False

    with util.tempdir() as tdir:

        cmd = [util.abs_join(find_root(), 'tools', 'read-version')]
        (sysout, _stderr) = util.subp(cmd)
        version = sysout.strip()

        cmd = ['bzr', 'revno']
        (sysout, _stderr) = util.subp(cmd)
        revno = sysout.strip()
        
        # This is really only a temporary archive
        # since we will extract it then add in the debian
        # folder, then re-archive it for debian happiness
        print("Creating a temporary tarball using the 'make-tarball' helper")
        cmd = [util.abs_join(find_root(), 'tools', 'make-tarball')]
        (sysout, _stderr) = util.subp(cmd)
        arch_fn = sysout.strip()
        tmp_arch_fn = util.abs_join(tdir, os.path.basename(arch_fn))
        shutil.move(arch_fn, tmp_arch_fn)

        print("Extracting temporary tarball %r" % (tmp_arch_fn))
        cmd = ['tar', '-xvzf', tmp_arch_fn, '-C', tdir]
        util.subp(cmd, capture=capture)
        extracted_name = tmp_arch_fn[:-len('.tar.gz')]
        os.remove(tmp_arch_fn)

        xdir = util.abs_join(tdir, 'cloud-init')
        shutil.move(extracted_name, xdir)

        print("Creating a debian/ folder in %r" % (xdir))
        write_debian_folder(xdir, version, revno)

        # The naming here seems to follow some debian standard
        # so it will whine if it is changed...
        tar_fn = "cloud-init_%s~%s.orig.tar.gz" % (version, revno)
        print("Archiving the adjusted source into %r" %
              (util.abs_join(tdir, tar_fn)))
        cmd = ['tar', '-czvf', 
               util.abs_join(tdir, tar_fn), 
               '-C', xdir]
        cmd.extend(os.listdir(xdir))
        util.subp(cmd, capture=capture)

        # Copy it locally for reference
        shutil.copy(util.abs_join(tdir, tar_fn),
                    util.abs_join(os.getcwd(), tar_fn))
        print("Copied that archive to %r for local usage (if desired)." % 
              (util.abs_join(os.getcwd(), tar_fn)))

        print("Running 'debuild' in %r" % (xdir))
        with util.chdir(xdir):
            cmd = ['debuild', '--preserve-envvar', 'INIT_SYSTEM']
            if not args.sign:
                cmd.extend(['-us', '-uc'])
            util.subp(cmd, capture=capture)

        globs = [] 
        globs.extend(glob.glob("%s/*.deb" %
                     (os.path.join(tdir))))
        link_fn = os.path.join(os.getcwd(), 'cloud-init_all.deb')
        for fn in globs:
            base_fn = os.path.basename(fn)
            shutil.move(fn, base_fn)
            print("Wrote out debian package %r" % (base_fn))
            if fn.endswith('_all.deb'):
                # Add in the local link
                util.del_file(link_fn)
                os.symlink(base_fn, link_fn)
                print("Linked %r to %r" % (base_fn, link_fn))

    return 0


if __name__ == '__main__':
    sys.exit(main())