#!/usr/bin/env python3

from util import randomSAT
from util import kSAT
import argparse
import configparser
import os

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-d", "--base_directory", help="the base directorey of the new dataset; should contain a dataset.config file", type=str)
    parser.add_argument("-i", "--instances", help="number of random kSAT instances", type=int)
    parser.add_argument("-v", "--variables", help="number of variables in ksat instances", type=int)
    parser.add_argument("-c", "--clauses", help="number of clauses in ksat instances", type=int)
    parser.add_argument("--variables_per_clause", help="variables per clause in ksat instances", type=int, default=3)
    parser.add_argument("-o", "--output", help="output directory", type=str)
    args = parser.parse_args()
    
    baseDir = args.base_directory
    if baseDir != None:
        config = __readConfig(os.path.join(baseDir, "dataset.config"));
    
    numberOfVariables = args.variables
    if numberOfVariables == None:
        numberOfVariables = int(input("Number of variables per instance: "))
        
    numberOfClauses = args.clauses
    if numberOfClauses == None:
        numberOfClauses = int(input("Number of clauses per instance: "))
    
    numberOfInstances = args.instances
    if numberOfInstances == None:
        numberOfInstances = int(input("Number of instances: "))
    
    instanceDir = None
    if "instance_dir" in config["STRUCTURE"]:
        instanceDir = os.path.join(baseDir, config["STRUCTURE"]["instance_dir"])
    elif args.output != None:
        instanceDir = args.output
    elif args.output == None:
        instanceDir = str(input("output directory: "))
        
    for i in range(numberOfInstances):
        ksatInstance = randomSAT.generateRandomKSAT(numberOfClauses,
                                                    numberOfVariables,
                                                    args.variables_per_clause)
                                                    
        instanceFilePath = os.path.join(instanceDir, "instance_%d.dimacs" % (i))
        
        ksatInstance.writeDIMACS(instanceFilePath)

def __readConfig(configFilePath):
    config = configparser.ConfigParser()
    
    if os.path.isfile(configFilePath):
        config.read(configFilePath)

    return config

if __name__ == "__main__":
    main()
