Taylohtio/MapDriveScriptUpdate/MapDriveScriptUpdateTallier/Fixer.cs

115 lines
3.6 KiB
C#

using System;
using System.Diagnostics;
using System.IO;
namespace Tallier.MapDriveScriptUpdate
{
public class Fixer
{
private Logger log;
public Fixer(Logger log)
{
this.log = log;
}
public void Fix()
{
try
{
this.log.Info("Start process");
this.log.Info("Resolving startup folder..");
var folder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
this.log.Info("Resolved startup folder: '{0}'", folder);
if (!Directory.Exists(folder))
{
this.log.Error("Can't fix map script: startup folder doesn't exist");
return;
}
var files = Directory.GetFiles(folder);
if (files == null || files.Length == 0)
{
this.log.Error("Can't fix map script: no files found in startup folder");
return;
}
string scriptPath = string.Empty;
foreach (var filePath in files)
{
if (string.IsNullOrEmpty(filePath))
{
continue;
}
var fi = new FileInfo(filePath);
this.log.Info(" Checking '{0}'", fi.Name);
if (this.isScript(fi.Name, filePath))
{
scriptPath = filePath;
break;
}
}
if (string.IsNullOrEmpty(scriptPath))
{
this.log.Info("Can't fix map script: script not found in startup folder or it was already fixed");
return;
}
this.log.Info("Mapping script found: '{0}'", scriptPath);
this.log.Info("Fix script content..");
string content = File.ReadAllText(scriptPath);
if (string.IsNullOrEmpty(content))
{
this.log.Error("Can't fix map script: script content is empty");
return;
}
content = content.Replace(@"\\tallier.mobimus.com\", @"\\tallier.mobimus.com@SSL\");
File.WriteAllText(scriptPath, content);
this.log.Info("Script is successfully fixed");
this.log.Info("Run script..");
var startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = scriptPath;
using (var exeProcess = Process.Start(startInfo))
{
exeProcess.WaitForExit();
}
this.log.Info("Script is finished");
}
catch (Exception x)
{
this.log.Error("Error occured: {0}\n{1}", x.Message, x.StackTrace);
}
}
private bool isScript(string fileName, string filePath)
{
if (string.IsNullOrEmpty(fileName))
{
return false;
}
fileName = fileName.ToLower();
if (!fileName.Contains("tallier") || !fileName.EndsWith(".cmd"))
{
return false;
}
string content = File.ReadAllText(filePath);
if (string.IsNullOrEmpty(content))
{
return false;
}
return content.ToLower().Contains("tallier.mobimus.com\\davwwwroot");
}
}
}