2011-10-09 04:16:11 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2012-01-17 08:12:22 +01:00
|
|
|
|
using System.IO;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
using System.Linq;
|
2011-10-13 04:24:30 +02:00
|
|
|
|
using NLog;
|
2011-10-23 07:26:43 +02:00
|
|
|
|
using NzbDrone.Common;
|
2011-10-09 19:45:08 +02:00
|
|
|
|
using NzbDrone.Model;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone
|
|
|
|
|
{
|
2011-10-13 04:24:30 +02:00
|
|
|
|
public class Router
|
2011-10-09 04:16:11 +02:00
|
|
|
|
{
|
2011-10-13 04:24:30 +02:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetLogger("Host.Router");
|
|
|
|
|
|
2011-10-11 09:11:05 +02:00
|
|
|
|
private readonly ApplicationServer _applicationServer;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
private readonly ServiceProvider _serviceProvider;
|
|
|
|
|
private readonly ConsoleProvider _consoleProvider;
|
2011-10-14 03:22:51 +02:00
|
|
|
|
private readonly EnviromentProvider _enviromentProvider;
|
2012-01-17 08:12:22 +01:00
|
|
|
|
private readonly DiskProvider _diskProvider;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
|
2012-01-17 08:12:22 +01:00
|
|
|
|
public Router(ApplicationServer applicationServer, ServiceProvider serviceProvider, ConsoleProvider consoleProvider, EnviromentProvider enviromentProvider, DiskProvider diskProvider)
|
2011-10-09 04:16:11 +02:00
|
|
|
|
{
|
2011-10-11 09:11:05 +02:00
|
|
|
|
_applicationServer = applicationServer;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
_serviceProvider = serviceProvider;
|
2011-10-13 04:24:30 +02:00
|
|
|
|
_consoleProvider = consoleProvider;
|
2011-10-14 03:22:51 +02:00
|
|
|
|
_enviromentProvider = enviromentProvider;
|
2012-01-17 08:12:22 +01:00
|
|
|
|
_diskProvider = diskProvider;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-14 03:22:51 +02:00
|
|
|
|
public void Route(IEnumerable<string> args)
|
2011-10-09 04:16:11 +02:00
|
|
|
|
{
|
2012-01-17 08:12:22 +01:00
|
|
|
|
|
2011-10-14 03:22:51 +02:00
|
|
|
|
Route(GetApplicationMode(args));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Route(ApplicationMode applicationMode)
|
|
|
|
|
{
|
|
|
|
|
Logger.Info("Application mode: {0}", applicationMode);
|
|
|
|
|
|
2012-01-17 08:12:22 +01:00
|
|
|
|
var batFiles = _diskProvider.GetFiles(_enviromentProvider.ApplicationPath, SearchOption.TopDirectoryOnly)
|
|
|
|
|
.Where(c => c.EndsWith(".bat", StringComparison.InvariantCultureIgnoreCase)).ToList();
|
|
|
|
|
|
|
|
|
|
foreach (var batFile in batFiles)
|
|
|
|
|
{
|
|
|
|
|
if (new FileInfo(batFile).Name.StartsWith("service", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
_diskProvider.DeleteFile(batFile);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-15 03:38:15 +01:00
|
|
|
|
//TODO:move this outside, it should be one of application modes (ApplicationMode.Service?)
|
2011-10-15 02:41:09 +02:00
|
|
|
|
if (!_enviromentProvider.IsUserInteractive)
|
2011-10-14 03:22:51 +02:00
|
|
|
|
{
|
2011-10-15 02:41:09 +02:00
|
|
|
|
_serviceProvider.Run(_applicationServer);
|
2011-10-09 04:16:11 +02:00
|
|
|
|
}
|
2011-10-14 03:22:51 +02:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (applicationMode)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
case ApplicationMode.Console:
|
|
|
|
|
{
|
|
|
|
|
_applicationServer.Start();
|
|
|
|
|
_consoleProvider.WaitForClose();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ApplicationMode.InstallService:
|
|
|
|
|
{
|
2011-10-26 19:15:47 +02:00
|
|
|
|
if (_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
2011-10-14 03:22:51 +02:00
|
|
|
|
{
|
|
|
|
|
_consoleProvider.PrintServiceAlreadyExist();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-10-26 19:15:47 +02:00
|
|
|
|
_serviceProvider.Install(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
2012-01-17 08:12:22 +01:00
|
|
|
|
_serviceProvider.Start(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
2011-10-14 03:22:51 +02:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case ApplicationMode.UninstallService:
|
|
|
|
|
{
|
2011-10-26 19:15:47 +02:00
|
|
|
|
if (!_serviceProvider.ServiceExist(ServiceProvider.NZBDRONE_SERVICE_NAME))
|
2011-10-15 02:41:09 +02:00
|
|
|
|
{
|
|
|
|
|
_consoleProvider.PrintServiceDoestExist();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2011-10-26 19:15:47 +02:00
|
|
|
|
_serviceProvider.UnInstall(ServiceProvider.NZBDRONE_SERVICE_NAME);
|
2011-10-15 02:41:09 +02:00
|
|
|
|
}
|
2012-01-17 08:12:22 +01:00
|
|
|
|
|
2011-10-14 03:22:51 +02:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
{
|
|
|
|
|
_consoleProvider.PrintHelp();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ApplicationMode GetApplicationMode(IEnumerable<string> args)
|
|
|
|
|
{
|
|
|
|
|
if (args == null) return ApplicationMode.Console;
|
|
|
|
|
|
|
|
|
|
var cleanArgs = args.Where(c => c != null && !String.IsNullOrWhiteSpace(c)).ToList();
|
|
|
|
|
if (cleanArgs.Count == 0) return ApplicationMode.Console;
|
|
|
|
|
if (cleanArgs.Count != 1) return ApplicationMode.Help;
|
|
|
|
|
|
|
|
|
|
var arg = cleanArgs.First().Trim('/', '\\', '-').ToLower();
|
|
|
|
|
|
|
|
|
|
if (arg == "i") return ApplicationMode.InstallService;
|
|
|
|
|
if (arg == "u") return ApplicationMode.UninstallService;
|
|
|
|
|
|
|
|
|
|
return ApplicationMode.Help;
|
2011-10-09 04:16:11 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|