#coding:utf-8
import os
import sys
import shutil
import subprocess
import sunhpc.commands
class Command(sunhpc.commands.create.command):
    """
    Create a Sunhpc local yum repos.

    <param type='Bool' name='file'>
    Enable the file repos.
    </param>

    <param type='Bool' name='web'>
    Enable the http web repos.
    </param>

    <param type='Bool' name='gpk'>
    Enable the gpk.
    </param>

    <example cmd='create repos'>
    Create a sunhpc local yum repos.
    </example>

    <example cmd='create repos file=1'>
    Enable yum repo for file.
    </example>

    <example cmd='create repos web=1'>
    Enable yum web repo for http.
    </example>
    """
    def run(self, param, args):

        (version, filerepo, webrepo, gpk, quiet) = self.fillParams([
                ('version', sunhpc.version),
                ('file', 'no'),
                ('web', 'no'),
                ('gpk', 'no'),
                ('quiet', 'no')
                ])

        q = self.str2bool(quiet)
        fil = self.str2bool(filerepo)
        web = self.str2bool(webrepo)
        gpk = self.str2bool(gpk)

        if fil:
            web = False

        if web:
            fil = False

        if not fil and not web:
            self.msg('must supply an file or web.', 'a')


        addr  = self.db.getHostAttr('localhost', 'Kickstart_PrivateAddress')
        base  = self.db.getHostAttr('localhost', 'Kickstart_BaseDir')
        dist  = self.db.getHostAttr('localhost', 'distribution')

        distro_base = self.command('report.distro').strip()
        distro_dirs = os.path.join(distro_base, dist, self.arch)

        sunhpc_repo = '/etc/yum.repos.d/sunhpc-local.repo'
        with open(sunhpc_repo, 'w') as f:
            f.write('#\n# Generated by "sunhpc create repos" command\n#\n')

            if web:
                f.write('[Sunhpc-%s]\n' % sunhpc.version)
                f.write('name = Sunhpc %s\n' % sunhpc.version)
                f.write('baseurl = http://%s/%s/%s/%s\n' % (addr, base, dist, self.arch))
                f.write('enabled = 1\n')

            if fil:
                f.write('[Sunhpc-File-%s]\n' % sunhpc.version)
                f.write('name = Sunhpc file %s\n' % sunhpc.version)
                f.write('baseurl = file://%s\n' % distro_dirs)
                f.write('enabled = 1\n')

            if gpk:
                f.write('gpgcheck = 1\n')
            else:
                f.write('gpgcheck = 0\n')



















