cmConst

cmConst

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using PdMagic.Windows;

namespace cmIISConfig
{
    
    internal class CmConst
    {

        public string WindowsProgramFiles => Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);
        public string WindowsProgramData => Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);


        public string ApplicationName => "Crash Magic Online";
        public string VersionsListUrl => "http://www.GovernmentTools.com/CrashMagicAppResources/versions/Versions.txt";
        public string VersionsUrl => "http://www.GovernmentTools.com/CrashMagicAppResources/versions";
        public string CompanyFolder => "PdMagic";
        public string ApplicationFolderBaseName => "CrashMagicOnline";
        public string DefaultInstanceName => "[Default]";
        public string DllFileName => "CrashMagicOnline_ISAPI.dll";


        public string FullInstanceName => FullInstanceNameP(InstanceName);  //CrashMagicOnline<InstanceName>
        public string ProgramFiles => $@"{WindowsProgramFiles}\{CompanyFolder}\{FullInstanceName}";
        public string IsapiFolder => $@"{ProgramFiles}\Bin\ISAPI";
        public string ProgramData => $@"{WindowsProgramData}\{CompanyFolder}\{FullInstanceName}";
        public string PubFolder => $@"{ProgramData}\Pub";
        public string FilesFolder => $@"{PubFolder}\files";
        public string CacheFolder => $@"{PubFolder}\Default\Cache\$";
        public string DefaultInstallersFolder => $@"{WindowsProgramData}\{CompanyFolder}\Installers\CrashMagicOnline";


        public string InstanceUserName => NameWithInstanceP("cmUser", InstanceName);
        public string InstanceUserGroupName => NameWithInstanceP("cmUserGroup", InstanceName);


        public string InstanceSiteName => NameWithInstanceP("CrashMagic", InstanceName);
        public string InstanceAppName => NameWithInstanceP("/cm", InstanceName);
        public string InstanceAppPoolName => NameWithInstanceP("cmISAPI32", InstanceName);


        public string InstanceName { get; set; }
        public string InstallersFolder { get; set; }


        public List<VirtualFolder> VirtualFolders { get; } = new List<VirtualFolder>();
        public List<MimeEntry> MimeEntries { get; } = new List<MimeEntry>();

        public CmConst(string aInstanceName)
        {
            InstanceName = aInstanceName;
            PopulateVirtualFolders();
            PopulateMimeEntries();
            InstallersFolder = DefaultInstallersFolder;
        }

        private void PopulateMimeEntries()
        {
            MimeEntries.Add(new MimeEntry(".svg", "image/svg+xml"));
            MimeEntries.Add(new MimeEntry(".xap", "application/x-silverlight-app"));
            MimeEntries.Add(new MimeEntry(".xaml", "application/xaml+xml"));
            MimeEntries.Add(new MimeEntry(".pdxaml", "application/pdxaml+xml"));
        }

        private void PopulateVirtualFolders()
        {
            VirtualFolders.Add(new VirtualFolder("CMFiles", FilesFolder));
            VirtualFolders.Add(new VirtualFolder("CMCache", CacheFolder));
        }

        public string NameWithInstanceP(string aName, string aInstanceName)
        {
            return aInstanceName == DefaultInstanceName ? aName : $"{aName}{aInstanceName}";
        }

        public string FullInstanceNameP(string aInstanceName)
        {
            return NameWithInstanceP( ApplicationFolderBaseName, aInstanceName);
        }

        public string ProgramFilesP(string aInstanceName)
        {
           return $@"{WindowsProgramFiles}\{CompanyFolder}\{FullInstanceNameP(aInstanceName)}";
        }

        public string IsapiFolderP(string aInstanceName)
        {
            return $@"{ProgramFilesP(aInstanceName)}\Bin\ISAPI";
        }

        public bool InstanceExistsP(string aInstanceName)
        {
            return Directory.Exists($@"{WindowsProgramFiles}\{CompanyFolder}\{FullInstanceNameP(aInstanceName)}") |
                   Directory.Exists($@"{WindowsProgramData}\{CompanyFolder}\{FullInstanceNameP(aInstanceName)}");
        }

        public bool ValidNewInstanceNameP(string aInstanceName)
        {

            if (aInstanceName == DefaultInstanceName)
            {
                aInstanceName = "";
            }

            if (InstanceExistsP(aInstanceName))
            {
                return false;
            }
            else
            {
                var r = new Regex(@"^[a-zA-Z0-9]+$");
                return aInstanceName == string.Empty || r.IsMatch(aInstanceName);
            }
        }

    }
}