#!/usr/bin/env python
# encoding: utf-8

""" dfopt - Dwarf Fortress Options

Usage:
    dfopt [--directory DIR] set (<opt> <value>)...
    dfopt [--directory DIR] search (<opt>)
    dfopt [--directory DIR] defaults
    dfopt [--directory DIR] backup OPTSFILE
    dfopt [--directory DIR] restore OPTSFILE
    dfopt -h | --help
    dfopt -l | --license
    dfopt -v | --version

Options:
    -d --directory DIR  Path to Dwarf Fortress install.

    -h --help           Display the help text.
    -l --license        Display the license
    -v --version        Disaply the version

Examples:
    dfopt search population
        Search the configs for variables containing "population".

    dfopt set population_cap 80
        Set POPULATION_CAP to 80

    dfopt set baby_child_cap 0:10
        Set BABY_CHILD_CAP to 0:10

    dfopt set population_cap 80 baby_child_cap 0:10
        Set multiple values at once.

    dfopt restore file.json
        Read a json file containing saved options and values,
        then set the game's options to those values.
"""

license = """
Copyright (c) 2012, haesken
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of haesken nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL haesken BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

version = "0.4.0"

import sys
from os import path, getcwd
from docopt import docopt

sys.path.append("modules")
import dftlib


class optionsManager(object):
    def __init__(self, path_root_dir):
        self.df_paths = dftlib.make_df_paths(path_root_dir, "linux")

        self.inits = dftlib.read_lines(
                path.join(self.df_paths["init"], "init.txt"))
        self.d_inits = dftlib.read_lines(
                path.join(self.df_paths["init"], "d_init.txt"))

    def __parse_option__(self, option):
        return option.split(":")[0], option.split(":")[1:]

    def setopt(self, option, value):
        print option, value

    def search(self, option):
        for item in [self.inits, self.d_inits]:
            for line in item:
                if line.startswith("[") and option in line:
                    print(line)

    def backup(self, path_optsfile):
        pass

    def restore(self, path_optsfile):
        pass


def main(args):
    """ Run selected functions. """

    if not args["--directory"] == None:
        path_root_dir = args["--directory"]
    else:
        path_root_dir = getcwd()

    options = optionsManager(path_root_dir)

    if args["search"]:
        for term in args["<opt>"]:
            options.search(term.upper())

    if args["set"]:
        # Set one option at a time.
        for option in args["<opt>"]:
            # Unpack the pairs by using the option name's index
            # as the value's index.
            options.setopt(option, args["<value>"][args["<opt>"].index(option)])

    if args["defaults"]:
        options.restore("default")

    if args["backup"]:
        options.backup(args["OPTSFILE"])

    if args["restore"]:
        options.restore(args["OPTSFILE"])

    if args["--license"]:
        print(license)

    if args["--version"]:
        print(version)


if __name__ == '__main__':
    try:
        main(docopt(__doc__))
    except KeyboardInterrupt:
        sys.exit()

@@ -3026,17 +3026,16 @@
     def 
-_
 _parse_o
@@ -3039,18 +3039,16 @@
 e_option
-__
 (self, o
@@ -3369,22 +3369,174 @@
         
-print(
+if line in self.inits:%0A                        yield (%22inits%22, line)%0A                    elif line in self.d_inits:%0A                        yield (%22d_inits%22, 
 line)%0A%0A 
@@ -3926,32 +3926,46 @@
 %22%5D:%0A            
+for result in 
 options.search(t
@@ -3976,16 +3976,50 @@
 upper())
+:%0A                print(result%5B1%5D)
 %0A%0A    if

Make search more reusable.