#!/usr/bin/env python
# vim: ft=python:

import sys
import os
import os.path
import argparse
import configparser
import sh
import stat
import glob
import shutil

cmdparser = None

def check_binary():
    """Ensure all needed binaries are available in the PATH"""
    binaries = ["git", "svn", "svnversion"]
    unavailable = []
    for i in binaries:
        try:
            sh.Command(i)
        except sh.CommandNotFound:
            unavailable.append(i)
    if len(unavailable) > 0:
        print("The following binaries are not available: " + str(unavailable))
        sys.exit(1)


def initcfg(args):
    cfg = configparser.ConfigParser()
    if os.path.exists(args.config_file):
        cfg.read(args.config_file)

    section = cfg['general']
    if args.svn_server:
        section['svn_server'] = args.svn_server
    if args.svn_dir:
        section['svn_dir'] = args.svn_dir
    if args.repository:
        section['repository'] = args.repository

    with open(args.config_file, 'w') as f:
        cfg.write(f)


def getcfg(args):
    """Complete the command line parameter with the content of the
    configuration file"""
    cfg = configparser.ConfigParser()
    cfg.read(args.config_file)
    if 'general' not in cfg.sections():
        return args
    for i in cfg['general']:
        if not getattr(args, i):