context: using System.IO;
  
  namespace Timok.Rbr.Core.Config {
  	public interface IFoldersConfiguration {
  		string RbrConsumingFolderName { get; }
  		string ArchiveFolder { get; }
  		string CurrentFolder { get; }
  		string SqlDbFolder { get; }
  		string PayphoneNumbersFilePath { get; }
  		string IVRFolder { get; }
  		string IVRProcessFilePath { get; }
  		string AuditFolder { get; }
  		string CdrExportFolder { get; }
  		string ExportFolder { get; }
  		string FtpFolder { get; }
  		string OrigRoutingFolder { get; }
  		string FtpCdrExportFolder { get; }
  		string FtpReplicationFolder { get; }
  		string FtpNumberPortabilityFolder { get; }
  		string EmailFolder { get; }
  		string CdrAggrPublishingFolder { get; }
  		string RbrPublishingFolder { get; }
  		string RbrSequenceFilePath { get; }
  		string CdrPublishingFolder { get; }
  		string InventoryFolder { get; }
  		string ImportFilesPath { get; }
  		string NumberPortabilityDbPath { get; }
  		string LogFolder { get; }
  		string GkIniFile { get; }
  		string GetInventoryServiceFolder(short pServiceId);
  		string GetInventoryLotFolder(short pServiceId, decimal pDenomination);
  		string GetInventoryBatchFileName(short pServiceId, decimal pDenomination, int pBatchId);
  		string GetInventoryBatchFilePath(short pServiceId, decimal pDenomination, int pBatchId);
  	}
  
  	internal class FoldersConfiguration : IFoldersConfiguration {
  		readonly IMainConfiguration main;
  		readonly IDbConfiguration db;
  
  		public string GkIniFile {
  			get {
  				if (string.IsNullOrEmpty(H323Folder)) {
  					return null;
  				}
  				return Path.Combine(H323Folder, "Gatekeeper.ini");
  			}
  		}
  
  		public string H323Folder {
  			get {
  				if (string.IsNullOrEmpty(AppConstants.RbrRoot)) {
  					return null;
  				}
  				return Path.Combine(AppConstants.RbrRoot, "H323");
  			}
  		}
  		
  		public FoldersConfiguration(IMainConfiguration pMain, IDbConfiguration pDb) {
  			main = pMain;
  			db = pDb;
  			LogFolder = Path.Combine(main.RbrRoot, "Log");
  		}
  
  		//-- top level folder: Archive
  		string archiveFolder;
  		public string ArchiveFolder {
  			get {
  				if (archiveFolder == null) {
  					archiveFolder = Path.Combine(main.ArchiveDrive, @"Timok\Rbr\Archive");
  					if (!Directory.Exists(archiveFolder)) {
  						Directory.CreateDirectory(archiveFolder);
  					}
  				}
  				return archiveFolder;
  			}
  		}
  
  		//-- top level folder: Current
  		string currentFolder;
  		public string CurrentFolder {
  			get {
  				if (currentFolder == null) {
  					currentFolder = Path.Combine(main.RbrRootParent, "Current");
  					if (!Directory.Exists(currentFolder)) {
  						Directory.CreateDirectory(currentFolder);
  					}
  				}
  				return currentFolder;
  			}
  		}
  
  		//-- top level folder: SqlDb
  		string sqlDbFolder;
  		public string SqlDbFolder {
  			get {
  				if (sqlDbFolder == null) {
  					sqlDbFolder = Path.Combine(main.RbrRootParent, "SqlDb");
  					if (!Directory.Exists(sqlDbFolder)) {
  						Directory.CreateDirectory(sqlDbFolder);
  					}
  				}
  				return sqlDbFolder;
  			}
  		}
  
  		public string PayphoneNumbersFilePath { get { return Path.Combine(SqlDbFolder, "PayphoneNumbers.txt"); } }
  
  		public string IVRFolder { get { return main.RbrRoot; } }
  		public string IVRProcessFilePath {
  			get {
  				return string.IsNullOrEmpty(IVRFolder) ? null : Path.Combine(IVRFolder, "Timok.Rbr.IVR.exe");
  			}
  		}
  
  		public string LogFolder { get; private set; }
  
  		//-- gui Audit Publishing Folder
  		string auditFolder;
  		public string AuditFolder {
  			get {
  				if (auditFolder == null) {
  					auditFolder = Path.Combine(CurrentFolder, "Audit");
  					if (!Directory.Exists(auditFolder)) {
  						Directory.CreateDirectory(auditFolder);
  					}
  				}
  				return auditFolder;
  			}
  		}
  
  		string cdrExportFolder;
  		public string CdrExportFolder {
  			get {
  				if (cdrExportFolder == null) {
  					cdrExportFolder = Path.Combine(Path.Combine(CurrentFolder, "Export"), "Cdr");
  					if (!Directory.Exists(cdrExportFolder)) {
  						Directory.CreateDirectory(cdrExportFolder);
  					}
  				}
  				return cdrExportFolder;
  			}
  		}
  
  		string exportFolder;
  		public string ExportFolder {
  			get {
  				if (exportFolder == null) {
  					exportFolder = Path.Combine(CurrentFolder, "Export");
  					if (!Directory.Exists(exportFolder)) {
  						Directory.CreateDirectory(exportFolder);
  					}
  				}
  				return exportFolder;
  			}
  		}
  
  		//-- ftp folder
  		string ftpFolder;
  		public string FtpFolder {
  			get {
  				if (ftpFolder == null) {
  					ftpFolder = Path.Combine(main.RbrRootParent, "Ftp");
  					if (!Directory.Exists(ftpFolder)) {
  						Directory.CreateDirectory(ftpFolder);
  					}
  				}
  				return ftpFolder;
  			}
  		}		
  		
  		//-- orig routing folder
  		string origRoutingFolder;
  		public string OrigRoutingFolder {
  			get {
  				if (origRoutingFolder == null) {
  					origRoutingFolder = Path.Combine(main.RbrRootParent, "OrigRouting");
  					if (!Directory.Exists(origRoutingFolder)) {
  						Directory.CreateDirectory(origRoutingFolder);
  					}
  				}
  				return origRoutingFolder;
  			}
  		}
  
  		string ftpCdrExportFolder;
  		public string FtpCdrExportFolder {
  			get {
  				if (ftpCdrExportFolder == null) {
  					ftpCdrExportFolder = Path.Combine(FtpFolder, "CdrExport");
  					if (!Directory.Exists(ftpCdrExportFolder)) {
  						Directory.CreateDirectory(ftpCdrExportFolder);
  					}
  				}
  				return ftpCdrExportFolder;
  			}
  		}
  
  		//-- Replication folder
  		string replicationFolder;
  		public string FtpReplicationFolder {
  			get {
  				if (replicationFolder == null) {
  
gt: 					replicationFolder = Path.Combine(FtpFolder, "Replication");