#!/usr/bin/python

import sys, os, time, imp
import yum
from ConfigParser import SafeConfigParser

class config_class:
  class defaults:
    logfile = "/var/log/yum-autoupdate.log"
    basic_args = ["update", "-y", "-q", "--skip-broken"]
    args = []
    disabled = False
    filter = [
      r'^\*+$'
    ]

  def __init__(self, fn='/etc/yum/autoupdate.conf'):
      try:
        self.config = imp.load_source('config', '/dev/null', open(fn, 'rt'))
      except IOError, e:
        print "Unable to read configuration file: %s. Using defaults." % fn
        self.config = None
  def __getattr__(self, attr):
      if hasattr(self.config, attr):
        return getattr(self.config, attr)
      return getattr(self.defaults, attr)
config = config_class(*sys.argv[1:])

# exit if disabled
if config.disabled:
  sys.exit()

# redirect stdout and stderr to logfile
logfile = open(config.logfile, "a", 0)
os.dup2(logfile.fileno(), 1) # stdout
os.dup2(logfile.fileno(), 2) # stderr

logfile.write(time.strftime("----- %c -----\n"))

sys.path.insert(0, '/usr/share/yum-cli')
import yummain
yummain.main(config.basic_args + config.args)
