'''
Make me some salt!
'''
# Import python libs
import os
import optparse
# Import salt libs
import salt.master
import salt.minion
import salt.config
import salt.utils

class Master(object):
    '''
    Creates a master server
    '''
    def __init__(self):
        self.cli = self.__parse_cli()
        self.opts = salt.config.master_config(self.cli['config'])

    def __parse_cli(self):
        '''
        Parse the cli for options passed to a master daemon
        '''
        parser = optparse.OptionParser()
        parser.add_option('-f',
                '--foreground',
                dest='foreground',
                default=False,
                action='store_true',
                help='Run the master in the foreground')
        parser.add_option('-c',
                '--config',
                dest='config',
                default='/etc/salt/master',
                help='Pass in an alternative configuration file')
        
        options, args = parser.parse_args()
        cli = {'foreground': options.foreground,
               'config': options.config}

        return cli

    def _verify_env(self):
        '''
        Verify that the named direcotries are in place and that the environment
        can shake the salt
        '''
        if not os.path.isdir(self.opts['cachedir']):
            os.makedirs(self.opts['cachedir'])

    def start(self):
        '''
        Run the sequence to start a salt master server
        '''
        self._verify_env()
        master = salt.master.Master(self.opts)
        master.start()


class Minion(object):
    '''
    Create a minion server
    '''
    def __init__(self):
        self.cli = self.__parse_cli()
        self.opts = salt.config.minion_config(self.cli['config'])

    def __parse_cli(self):
        '''
        Parse the cli input
        '''
        parser = optparse.OptionParser()
        parser.add_option('-f',
                '--foreground',
                dest='foreground',
                default=False,
                action='store_true',
                help='Run the minion in the foreground')
        parser.add_option('-c',
                '--config',
                dest='config',
                default='/etc/salt/minion',
                help='Pass in an alternative configuration file')
        
        options, args = parser.parse_args()
        cli = {'foreground': options.foreground,
               'config': options.config}

        return cli

    def start(self):
        '''
        Execute this method to start up a minion.
        '''
        minion = salt.Minion(self.opts)
        minion.tune_in()

@@ -529,33 +529,33 @@
 er.add_option('-
-f
+d
 ',%0A             
@@ -556,34 +556,30 @@
          '--
-foreground
+daemon
 ',%0A         
@@ -587,34 +587,30 @@
       dest='
-foreground
+daemon
 ',%0A         
@@ -704,38 +704,32 @@
 e master in 
-the foreground
+a daemon
 ')%0A        p
@@ -985,34 +985,30 @@
     cli = %7B'
-foreground
+daemon
 ': options.f
@@ -1002,34 +1002,30 @@
 n': options.
-foreground
+daemon
 ,%0A          
@@ -1501,32 +1501,99 @@
 ster(self.opts)%0A
+        if self.opts%5B'daemon'%5D:%0A            salt.utils.daemonize()%0A
         master.s
@@ -1943,17 +1943,17 @@
 ption('-
-f
+d
 ',%0A     
@@ -1966,26 +1966,22 @@
      '--
-foreground
+daemon
 ',%0A     
@@ -1997,26 +1997,22 @@
   dest='
-foreground
+daemon
 ',%0A     
@@ -2115,25 +2115,19 @@
 ion 
-in the foreground
+as a daemon
 ')%0A 
@@ -2395,26 +2395,22 @@
 cli = %7B'
-foreground
+daemon
 ': optio
@@ -2416,18 +2416,14 @@
 ons.
-foreground
+daemon
 ,%0A  
@@ -2569,32 +2569,32 @@
 on.%0A        '''%0A
-
         minion =
@@ -2609,32 +2609,99 @@
 nion(self.opts)%0A
+        if self.opts%5B'daemon'%5D:%0A            salt.utils.daemonize()%0A
         minion.t

write in code so that salt components can run as daemons!