#!/usr/bin/python
import argparse,os
from minpower import solve,config
from minpower.tests import test

parser = argparse.ArgumentParser(description='Minpower command line interface')
parser.add_argument('directory', type=str, 
                   help='the direcory of the problem you want to solve (or name of minpower demo case)')
parser.add_argument('--solver','-s', metavar='S', type=str, default=config.optimization_solver,
                   help='the solver name used to solve the problem (e.g. cplex, gurobi, glpk)')
parser.add_argument('--breakpoints','-b', metavar='B', type=int, default=config.default_num_breakpoints,
                   help='number of breakpoints to use in piece-wise linearization of polynomial costs')
parser.add_argument('--commitment_hours','-c', metavar='C', type=int, default=config.default_hours_commitment,
                   help='number hours per commitment in a rolling UC (exclusive of overlap)')
parser.add_argument('--overlap_hours','-o', metavar='O', type=int, default=config.default_hours_commitment_overlap,
                   help='number hours to overlap commitments in a rolling UC')      

#figure out the command line arguments
args = parser.parse_args()

directory=args.directory

if not os.path.isdir(directory):
    #check to see if the name is a minpower test case
    test_dir=os.path.join(os.path.dirname(test.__file__),directory)
    if os.path.isdir(test_dir):
        print 'solving test case: {}'.format(directory)
        directory = test_dir
    else:
        #otherwise raise error
        msg='There is no folder named "{}".'.format(directory)
        raise OSError(msg)

#solve the problem with those arguments
solve.problem(directory,
              solver=args.solver,
              num_breakpoints=args.breakpoints,
              hours_commitment=args.commitment_hours,
              hours_commitment_overlap=args.overlap_hours,
              )
