#!/usr/bin/python
import os
import sys
import optparse

import varch

def parse():
    '''
    Parse arguments
    '''
    parser = optparse.OptionParser()
    parser.add_option('-c',
            '--config',
            '--conf',
            dest='conf',
            default='/etc/varch/base.aif',
            help='The location to the Archlinux Instalation Framework file; default=/etc/varch/base.aif')
    parser.add_option('-f',
            '--format',
            dest='format',
            default='raw',
            help='The disk image format to use, raw, qcow2 or vdi; default=raw')
    parser.add_option('-s',
            '--size',
            dest='size',
            default='20G',
            help='The size of the disk image, default=20G')
    parser.add_option('-i',
            '--image',
            dest='image',
            default='arch',
            help='The location of the image after it has been created, if not supplied an extension based on the format type will be appended; default=arch')
    parser.add_option('-g',
            '--generic',
            dest='generic',
            default=False,
            action='store_true',
            help='Generate a generic virtual machine image without support for virtio block drivers')
    parser.add_option('-p',
            '--pacman-conf',
            dest='pacman_conf',
            default='',
            help='Normally varch will use a generic set of pacman repos, set this an alternative pacman configuration file')
    parser.add_option('-o',
            '--overlay',
            dest='overlay',
            default='',
            help='The directory containig files to be coppied into the virtual machine')
    parser.add_option('-k',
            '--pull-boot',
            dest='pull_boot',
            default=False,
            action='store_true',
            help='Instruct varch to copy out the kernel and initrd before finalizing the vm')
    parser.add_option('-K',
            '--kvm-conf',
            dest='kvm_conf',
            default=False,
            action='store_true',
            help='This flag instructs varch to generate a kvm shell script to start the generated virtual machine')
    parser.add_option('-L',
            '--libvirt-conf',
            dest='libvirt_conf',
            default=False,
            action='store_true',
            help='This flag instructs varch to generate a libvirt xml configuration file')
            
            
    options, args = parser.parse_args()

    opts = {}
    opts['conf'] = options.conf
    opts['format'] = options.format
    opts['size'] = options.size
    opts['image'] = options.image
    opts['generic'] = options.generic
    opts['pacman_conf'] = options.pacman_conf
    opts['overlay'] = options.overlay
    opts['pull_boot'] = options.pull_boot
    opts['kvm_conf'] = options.kvm_conf
    opts['libvirt_conf'] = options.libvirt_conf

    if opts['format'] == 'vdi':
        opts['generic'] = True

    return opts


if __name__ == '__main__':
    opts = parse()
    if os.getuid():
        print('Sorry, the creation of virtual machines requires root access')
        sys.exit(1)
    v_arch = varch.VArch(opts)
    v_arch.make_vm()
