#!/usr/bin/env python

# Deejayd, a media player daemon
# Copyright (C) 2007-2008 Mickael Royer <mickael.royer@gmail.com>
#                         Alexandre Rossi <alexandre.rossi@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""
This is a deejayd test server executable
"""

import sys, os

from twisted.internet import reactor
from twisted.python import log
import twisted.internet.error

from deejayd.ui.config import DeejaydConfig
from deejayd.net.deejaydProtocol import DeejaydFactory
from deejayd.core import DeejayDaemonCore

# argv[1] should be port
# argv[2] should be music dir
# argv[3] should be video dir
# argv[4] should be db filename
if len(sys.argv) != 5:
    sys.exit('Incorrect invocation')

port = sys.argv[1]
music_dir = sys.argv[2]
video_dir = sys.argv[3]
dbfilename = sys.argv[4]

log.startLogging(open('/tmp/testdeejayd.log', 'a'))

config = DeejaydConfig()
config.set('general', 'activated_modes', 'playlist,webradio,video,dvd')
config.set('net', 'port', port)
config.set('mediadb', 'music_directory', music_dir)
config.set('mediadb', 'video_directory', video_dir)
config.set('database', 'db_type', 'sqlite')
config.set('database', 'db_name', dbfilename)
config.set('general', 'video_support', 'yes')
config.set('general', 'media_backend', 'xine')
config.set('xine','audio_output',"none")
config.set('xine','video_output',"none")

# init translation
from deejayd.ui.i18n import DeejaydTranslations
t = DeejaydTranslations()
t.install()

# start core
deejayd_core = DeejayDaemonCore(config)
deejayd_core.update_audio_library(sync = True, objanswer = False)
deejayd_core.update_video_library(sync = True, objanswer = False)

factory = DeejaydFactory(deejayd_core)
try:
    reactor.listenTCP(int(port), factory)
except twisted.internet.error.CannotListenError:
    # This is to avoid locking the test suite when the server used in the
    # previous test has not stopped listenning.
    deejayd_core.close()
    os.write(2, 'stopped\n')
    sys.exit()

def printReady():
    os.write(2, 'ready\n')

reactor.addSystemEventTrigger('after', 'startup', printReady)
reactor.addSystemEventTrigger('after', 'shutdown', deejayd_core.close)
reactor.run()


# vim: ts=4 sw=4 expandtab
