#!/usr/bin/python
#
# Copyright (c) 2010 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
#

import os
import re
import platform
import sys
import optparse
import glob
import shutil
import simplejson as json
import rhsm.config as config
from instnum import InstallationNumber, InstallationNumberError

_LIBPATH = "/usr/share/rhsm"
# add to the path if need be
if _LIBPATH not in sys.path:
    sys.path.append(_LIBPATH)

from subscription_manager.i18n import configure_i18n
configure_i18n()

import gettext
_ = gettext.gettext

cfg = config.initConfig()
PRODUCT_CERT_DIR = cfg.get('rhsm', 'productcertdir')

# Error messages in instnum that need to be translated.
_("Not a valid hex string")
_("Not a string")
_("Unsupported string length")
_("Checksum verification failed")

# Set up command line options
parser = optparse.OptionParser()
parser.add_option('-i', '--instnumber', help=_("Install number to run against"))
parser.add_option('-d', '--dryrun', action="store_true", default=False, help=_("Only print the files which would be copied over"))
(options, args) = parser.parse_args()


def getRelease():
    f = open('/etc/redhat-release')
    lines = f.readlines()
    f.close()
    release = "RHEL-" + str(lines).split(' ')[6].split('.')[0]
    return release


def writeMigrationFacts():
    FACT_FILE = "/etc/rhsm/facts/migration.facts"
    if not os.path.exists(FACT_FILE):
        f = open(FACT_FILE, 'w')
        json.dump({"migration.migrated_from": "install_number"}, f)
        f.close()


def main():
    #quick check to see if you are a super-user.
    if os.getuid() != 0:
        print _("Must be root user to execute\n")
        sys.exit(8)

    #get the instnum
    instnumber = options.instnumber
    if not instnumber:
        try:
            f = open('/etc/sysconfig/rhn/install-num', 'r')
            instnumber = f.readline()
            f.close()
        except IOError:
            print _("Could not read installation number from /etc/sysconfig/rhn/install-num.  Aborting.")
            sys.exit(1)

    #parse the instnum
    try:
        instnum = InstallationNumber(instnumber)
    except InstallationNumberError, e:
        print _("Could not parse the installation number: %s") % (e)
        sys.exit(1)

    repo_dict = instnum.get_repos_dict()

    # make all x86 arches map to i386 to match the proper cert
    platform_machine = platform.machine()
    parser = re.compile(r'i[\d]86')
    if parser.match(platform_machine):
      platform_machine = 'i386'

    sourcepath = "/usr/share/rhsm/product/" + getRelease()
    globs = []
    #generate globs to potentially match the name of the certs under sourcepath
    for key in repo_dict.keys():
        globs.append(repo_dict['Base'] + '-' + repo_dict[key] + '-' + platform_machine + '*')

    paths = [glob.glob(sourcepath + '/' + x) for x in globs]

    #flatten the list of lists
    paths = [item for sublist in paths for item in sublist]

    for path in paths:
        dest_file = path.split('-')[-1]
        dest_file = os.path.join(PRODUCT_CERT_DIR, dest_file)
        print _("Copying %s to %s") % (path, dest_file)
        if not options.dryrun:
            shutil.copyfile(path, dest_file)

    if not options.dryrun:
        writeMigrationFacts()

if __name__ == "__main__":
    main()
