#!/usr/bin/env python

import sys
from os.path import expanduser, dirname

import yaml
from fabric.main import main

import bosun


try:
    cfg_file = open(expanduser('~/.bosunrc'))
except IOError:
    configs = {}
else:
    configs = yaml.safe_load(cfg_file)

new_args = []
for opt in sys.argv[1:]:
    try:
        task, params = opt.split(':')
    except ValueError:
        new_args.append(opt)
    else:
        for new_conf in (c for c in configs if c in ('exp_repo')):
            params += "," + "=".join([new_conf, configs[new_conf]])
        new_task = task + ":" + ",".join([params])
        new_args.append(new_task)

sys.argv = ([sys.argv[0]] +
            ['='.join(['--' + opt, value]) for opt, value in configs.items()
             if opt not in ('exp_repo', 'name')] +
            new_args)
main(fabfile_locations=[dirname(bosun.__file__)])
