#!/usr/bin/env python
import os, sys
from os.path import join, dirname
from optparse import OptionParser

from useless.base import Error
from useless.base.util import makepaths

from paella.base import PaellaConfig
from paella.db import DefaultEnvironment
from paella.db.machine import MachineHandler
from paella.installer.base import InstallerConnection
from paella.installer.util.misc import get_mac_addresses
from paella.installer.machine import MachineInstaller

parser = OptionParser()
parser.add_option('--mirror', action='store', type='string', dest='remote',
                  default='http://paella/debian')

opts, args = parser.parse_args(sys.argv[1:])

if os.getuid():
    raise RuntimeError, "Need to be root to run this script."

config = PaellaConfig()
conn = InstallerConnection(config)

env = DefaultEnvironment(conn)

if env.has_option('installer', 'default_target'):
    target = env.get('installer', 'default_target')
else:
    print "WARNING: using hardcoded target directory /tmp/target"
    print "Set the default_target in the default environment."
    target = '/tmp/target'
    

# identify the machine
macs = get_mac_addresses()
machine = None
for mac in macs:
    if machine is None:
        if env.has_option('machines', mac):
            machine = env.get('machines', mac)


if machine is None:
    msg = 'This is an unknown machine, use paella-submit-machine '
    msg += 'to tell paella about it.'
    print msg
    sys.exit(0)

autoinstall = None
installed = False

section = 'autoinstall'
if env.has_section(section) and env.has_option(section, machine):
    autoinstall = env.is_it_true(section, machine)

if autoinstall is not None:
    section = 'already_installed'
    if env.has_section(section) and env.has_option(section, machine):
        installed = env.is_it_true(section, machine)



if autoinstall is None:
    msg = "Machine is %s but has no entry in " % machine
    msg += "autoinstall section of default environment."
    print msg
    sys.exit(0)

if installed:
    print machine, "is listed as already installed.  Aborting install."
    sys.exit(0)

if autoinstall:
    print 'auto installing machine %s' % machine
    # we used to use os.system to call paella-machine-installer
    # but that creates another connection to the database, and
    # wasn't really necessary.
    installer = MachineInstaller(conn)
    installer.set_target(target)
    installer.set_machine(machine)
    installer.run_all_processes()
    installer.save_logfile_in_target()
    installed = True
else:
    print 'skipping auto installation of machine %s' % machine
    sys.exit(0)
    

if installed:
    print "paella-init-nfsinstaller has finished successfully!"
    print machine, "has been installed into", target
    section = 'already_installed'
    if not env.has_section(section):
        env.add_section(section)
    env.set(section, machine, 'True')
    
sys.exit(0)

