#!/usr/local/sickbeard/env/bin/python
import os
import subprocess
import pwd
import cgi
import configobj
from pyextdirect.configuration import create_configuration, expose, LOAD, SUBMIT
from pyextdirect.router import Router
from pyextdirect.api import create_api


sickbeard_sss = '/var/packages/sickbeard/scripts/start-stop-status'
sickbeard_path = '/usr/local/sickbeard/var/config.ini'
autoprocesstv_path = '/usr/local/sickbeard/share/SickBeard/autoProcessTV/autoProcessTV.cfg'
sabnzbd_path = '/usr/local/sabnzbd/var/config.ini'
nzbget_path = ''  #TODO


Base = create_configuration()


class SickBeardCfg(Base):
    def __init__(self):
        '''Load available configuration files'''
        self.configs = {'sickbeard': configobj.ConfigObj(sickbeard_path),
                        'autoprocesstv': configobj.ConfigObj(autoprocesstv_path)}
        if os.path.exists(sabnzbd_path):
            self.configs['sabnzbd'] = configobj.ConfigObj(sabnzbd_path)
        if os.path.exists(nzbget_path):
            self.configs['nzbget'] = configobj.ConfigObj(nzbget_path)

    @expose(kind=LOAD)
    def load(self):
        '''Read configuration files to determine current state'''
        autoprocesstv = False
        configure_for = self.configs['sickbeard']['General']['nzb_method']
        if 'sabnzbd' in self.configs and self.configs['sabnzbd']['misc']['script_dir'] == os.path.dirname(autoprocesstv_path):
            autoprocesstv = True
        if configure_for not in ['sabnzbd', 'nzbget']:
            configure_for = 'nothing'
        return {'configure_for': configure_for, 'autoprocesstv': autoprocesstv}

    @expose(kind=SUBMIT)
    def save(self, configure_for=None, autoprocesstv=None):
        '''Change configuration files as requested'''
        if configure_for == 'sabnzbd' and 'sabnzbd' in self.configs:
            devnull = open(os.devnull, 'w')
            subprocess.call([sickbeard_sss, 'stop'], stdout=devnull, stderr=devnull)
            self.configs['sickbeard']['General']['nzb_method'] = 'sabnzbd'
            self.configs['sickbeard']['SABnzbd']['sab_username'] = self.configs['sabnzbd']['misc']['username']
            self.configs['sickbeard']['SABnzbd']['sab_password'] = self.configs['sabnzbd']['misc']['password']
            self.configs['sickbeard']['SABnzbd']['sab_apikey'] = self.configs['sabnzbd']['misc']['api_key']
            self.configs['sickbeard']['SABnzbd']['sab_host'] = 'http://localhost:' + self.configs['sabnzbd']['misc']['port'] + '/'
            self.configs['sickbeard'].write()
            self.configs['autoprocesstv']['SickBeard']['username'] = self.configs['sickbeard']['General']['web_username']
            self.configs['autoprocesstv']['SickBeard']['password'] = self.configs['sickbeard']['General']['web_password']
            self.configs['autoprocesstv'].write()
            # Change ownership of autoProcessTV so SABnzbd doesn't fail opening it during postprocessing
            os.chown(autoprocesstv_path, pwd.getpwnam('sabnzbd')[2], -1)
            subprocess.call([sickbeard_sss, 'start'], stdout=devnull, stderr=devnull)
        elif configure_for == 'nzbget' and 'nzbget' in self.configs:
            #TODO
            pass
        if autoprocesstv and 'sabnzbd' in self.configs:
            self.configs['sabnzbd']['misc']['script_dir'] = os.path.dirname(autoprocesstv_path)
            self.configs['sabnzbd'].write()

    @expose
    def available_configs(self):
        '''List available configurations'''
        #TODO: Use this to disable radio butons/checkboxes
        return self.configs.keys()


if __name__ == '__main__':
    print 'Content-type: application/json'
    print
    router = Router(Base)
    fs = cgi.FieldStorage()
    request = fs.value
    if isinstance(request, list):
        request = dict((mfs.name, mfs.value) for mfs in request)
    print router.route(request)


@@ -1938,32 +1938,97 @@
 stderr=devnull)%0A
+            # Edit SickBeard's configuration to point to SABnzbd%0A
             self
@@ -2597,302 +2597,8 @@
 e()%0A
-            self.configs%5B'autoprocesstv'%5D%5B'SickBeard'%5D%5B'username'%5D = self.configs%5B'sickbeard'%5D%5B'General'%5D%5B'web_username'%5D%0A            self.configs%5B'autoprocesstv'%5D%5B'SickBeard'%5D%5B'password'%5D = self.configs%5B'sickbeard'%5D%5B'General'%5D%5B'web_password'%5D%0A            self.configs%5B'autoprocesstv'%5D.write()%0A
     
@@ -3033,125 +3033,658 @@
     
-self.configs%5B'sabnzbd'%5D%5B'misc'%5D%5B'script_dir'%5D = os.path.dirname(autoprocesstv_path)%0A            self.configs%5B'sabnzbd
+# Change SABnzbd script dir to the autoProcessTV directory%0A            #TODO: Create a script dir in var for SABnzbd and put autoProcessTV in there rather%0A            # than pointing to it%0A            self.configs%5B'sabnzbd'%5D%5B'misc'%5D%5B'script_dir'%5D = os.path.dirname(autoprocesstv_path)%0A            self.configs%5B'sabnzbd'%5D.write()%0A            # Setup autoProcessTV.cfg%0A            self.configs%5B'autoprocesstv'%5D%5B'SickBeard'%5D%5B'username'%5D = self.configs%5B'sickbeard'%5D%5B'General'%5D%5B'web_username'%5D%0A            self.configs%5B'autoprocesstv'%5D%5B'SickBeard'%5D%5B'password'%5D = self.configs%5B'sickbeard'%5D%5B'General'%5D%5B'web_password'%5D%0A            self.configs%5B'autoprocesstv
 '%5D.w

Improve DSM UI controller in sickbeard SPK