#!/usr/bin/env python3.4

"""
Manage OpenBSD userland, ports and kernel updates.
"""


import os
import sys
import subprocess
import argparse


def cmd(cmd):
    subprocess.call(cmd, shell=True)


def fetch():

    """ Fetch corresponding sources from CVS. """

    print('Fetching OpenBSD '+repo+' tree from CVS...')
    cmd('cd /usr && cvs -qd '+cvsroot+' get -r'+release+' -P '+repo)


def update():

    """ Update corresponding sources from CVS. """

    print('Updating OpenBSD '+repo+' tree from CVS...')
    cmd('cd /usr/'+repo+' && cvs -d '+cvsroot+' -q up -r'+release + ' -Pd')


def build():

    """ Build and install corresponding part of the system. """

    print('Rebuilding '+repo+'...')
    if args['kernel']:
        cmd('cd /usr/src/sys/arch/'+arch+'/conf && config GENERIC.MP')
        cmd('cd /usr/src/sys/arch/'+arch+'/compile/GENERIC.MP '
            '&& make clean && make && make install')
    elif args['userland']:
        cmd('rm -rf /usr/obj/* && cd /usr/src && make obj')
        cmd('cd /usr/src/etc && env DESTDIR=/ make distrib-dirs')
        cmd('cd /usr/src && make build')
    elif args['xenocara']:
        cmd('rm -rf /usr/xobj/*')
        cmd('cd /usr/xenocara && make bootstrap && make obj && make build')


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=('Manage OpenBSD userland, '
                                                 'ports and kernel updates.'),
                                     prog='obsdupdate')
    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-f', '--fetch', choices=['ports', 'src', 'xenocara'],
                        help='Fetch sources from CVS.')
    group.add_argument('-u', '--update', choices=['ports', 'src', 'xenocara'],
                        help='Update source tree from CVS.')
    group.add_argument('-b', '--build', choices=['kernel', 'userland', 'xenocara'],
                        help='Rebuild and install a part of the system.')
    parser.add_argument('-c', '--cvs', nargs=1, help='Define CVS root server.')
    args = parser.parse_args()

    # Configuration variables goes here.
    default_cvs_root = 'anoncvs@anoncvs.fr.openbsd.org:/cvs'
    cvsroot = args.cvs if args.cvs else default_cvs_root
    release = 'OPENBSD_' + os.uname()[2].replace('.', '_')
    repo = sys.argv[2]
    arch = os.uname()[4]

    # Process arguments
    if args.fetch:
        fetch()
    elif args.update:
        update()
    elif args.build:
        build()

    print('done')

@@ -94,19 +94,8 @@
  os%0A
-import sys%0A
 impo
@@ -1258,16 +1258,41 @@
 ain__%22:%0A
+    # Arguments parsing.%0A
     pars
@@ -1410,16 +1410,17 @@
         
+ 
 'ports a
@@ -1657,25 +1657,24 @@
             
- 
 help='Fetch 
@@ -1791,25 +1791,24 @@
             
- 
 help='Update
@@ -1935,25 +1935,24 @@
             
- 
 help='Rebuil
@@ -2036,16 +2036,59 @@
 nargs=1,
+ metavar='CVSROOT',%0A                       
  help='D
@@ -2177,18 +2177,8 @@
 bles
- goes here
 .%0A  
@@ -2367,18 +2367,78 @@
 o = 
-sys.argv%5B2
+%5Bvars(args)%5Bi%5D for i in %5B'fetch','update','build'%5D if vars(args)%5Bi%5D%5D%5B0
 %5D%0A  
@@ -2471,25 +2471,29 @@
   # 
-Process arguments
+Arguments processing.
 %0A   

remove sys import, cleaner is better