2016-08-25 10:20:04 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2016-08-24 20:33:35 -07:00
|
|
|
|
using SabreTools.Helper;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SabreTools
|
|
|
|
|
|
{
|
|
|
|
|
|
public class TGZTest
|
|
|
|
|
|
{
|
2016-08-25 10:20:04 -07:00
|
|
|
|
// User-defined variables
|
|
|
|
|
|
private List<string> _inputs;
|
|
|
|
|
|
private string _outdir;
|
|
|
|
|
|
private bool _delete;
|
|
|
|
|
|
private Logger _logger;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a new TGZTest object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="inputs">List of all inputted files and folders</param>
|
|
|
|
|
|
/// <param name="outdir">Output directory (empty for default directory)</param>
|
|
|
|
|
|
/// <param name="delete">True if input files should be deleted, false otherwise</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for file and console output</param>
|
|
|
|
|
|
public TGZTest(List<string> inputs, string outdir, bool delete, Logger logger)
|
|
|
|
|
|
{
|
|
|
|
|
|
_inputs = inputs;
|
|
|
|
|
|
_outdir = (String.IsNullOrEmpty(outdir) ? "tgz" : outdir);
|
|
|
|
|
|
_delete = delete;
|
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Main entry point for the program
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="args">List of arguments to be parsed</param>
|
2016-08-24 20:33:35 -07:00
|
|
|
|
public static void Main(string[] args)
|
|
|
|
|
|
{
|
2016-08-25 10:20:04 -07:00
|
|
|
|
// If output is being redirected, don't allow clear screens
|
|
|
|
|
|
if (!Console.IsOutputRedirected)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Perform initial setup and verification
|
2016-08-24 20:33:35 -07:00
|
|
|
|
Logger logger = new Logger(true, "tgztest.log");
|
|
|
|
|
|
logger.Start();
|
|
|
|
|
|
|
2016-08-25 10:20:04 -07:00
|
|
|
|
// Credits take precidence over all
|
|
|
|
|
|
if ((new List<string>(args)).Contains("--credits"))
|
|
|
|
|
|
{
|
|
|
|
|
|
Build.Credits();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If there's no arguments, show help
|
|
|
|
|
|
if (args.Length == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
Build.Help();
|
|
|
|
|
|
logger.Close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Output the title
|
|
|
|
|
|
Build.Start("TGZTest");
|
|
|
|
|
|
|
|
|
|
|
|
// Set all default values
|
|
|
|
|
|
bool help = false,
|
|
|
|
|
|
delete = false,
|
|
|
|
|
|
tgz = true;
|
|
|
|
|
|
string outdir = "";
|
|
|
|
|
|
List<string> inputs = new List<string>();
|
|
|
|
|
|
|
|
|
|
|
|
// Determine which switches are enabled (with values if necessary)
|
2016-08-24 20:33:35 -07:00
|
|
|
|
foreach (string arg in args)
|
|
|
|
|
|
{
|
|
|
|
|
|
string temparg = arg.Replace("\"", "").Replace("file://", "");
|
|
|
|
|
|
|
2016-08-25 10:20:04 -07:00
|
|
|
|
switch (temparg)
|
2016-08-24 20:33:35 -07:00
|
|
|
|
{
|
2016-08-25 10:20:04 -07:00
|
|
|
|
case "-?":
|
|
|
|
|
|
case "-h":
|
|
|
|
|
|
case "--help":
|
|
|
|
|
|
help = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case "-d":
|
|
|
|
|
|
case "--delete":
|
|
|
|
|
|
delete = true;
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
if (temparg.StartsWith("-out=") || temparg.StartsWith("--out="))
|
|
|
|
|
|
{
|
|
|
|
|
|
outdir = temparg.Split('=')[1];
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (File.Exists(temparg) || Directory.Exists(temparg))
|
|
|
|
|
|
{
|
|
|
|
|
|
inputs.Add(temparg);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error("Invalid input detected: " + arg);
|
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
Build.Help();
|
|
|
|
|
|
logger.Close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
break;
|
2016-08-24 20:33:35 -07:00
|
|
|
|
}
|
2016-08-25 10:20:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If help is set, show the help screen
|
|
|
|
|
|
if (help)
|
|
|
|
|
|
{
|
|
|
|
|
|
Build.Help();
|
|
|
|
|
|
logger.Close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If a switch that requires a filename is set and no file is, show the help screen
|
|
|
|
|
|
if (inputs.Count == 0 && tgz)
|
|
|
|
|
|
{
|
|
|
|
|
|
logger.Error("This feature requires at least one input");
|
|
|
|
|
|
Build.Help();
|
|
|
|
|
|
logger.Close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we are doing a simple sort
|
|
|
|
|
|
if (tgz)
|
|
|
|
|
|
{
|
|
|
|
|
|
InitTGZTest(inputs, outdir, delete, logger);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If nothing is set, show the help
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
Build.Help();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logger.Close();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Wrap sorting files using an input DAT
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="inputs">List of all inputted files and folders</param>
|
|
|
|
|
|
/// <param name="outdir">Output directory (empty for default directory)</param>
|
|
|
|
|
|
/// <param name="delete">True if input files should be deleted, false otherwise</param>
|
|
|
|
|
|
/// <param name="logger">Logger object for file and console output</param>
|
|
|
|
|
|
public static bool InitTGZTest(List<string> inputs, string outdir, bool delete, Logger logger)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Get all individual files from the inputs
|
|
|
|
|
|
List<string> newinputs = new List<string>();
|
|
|
|
|
|
foreach (string input in inputs)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (File.Exists(input))
|
2016-08-24 20:33:35 -07:00
|
|
|
|
{
|
2016-08-25 10:20:04 -07:00
|
|
|
|
newinputs.Add(Path.GetFullPath(input));
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (Directory.Exists(input))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (string file in Directory.EnumerateFiles(input, "*", SearchOption.AllDirectories))
|
2016-08-24 20:33:35 -07:00
|
|
|
|
{
|
2016-08-25 10:20:04 -07:00
|
|
|
|
newinputs.Add(Path.GetFullPath(input));
|
2016-08-24 20:33:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-25 10:20:04 -07:00
|
|
|
|
TGZTest tgztest = new TGZTest(newinputs, outdir, delete, logger);
|
|
|
|
|
|
return tgztest.Process();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Process all input files
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if processing was a success, false otherwise</returns>
|
|
|
|
|
|
public bool Process()
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (string input in _inputs)
|
|
|
|
|
|
{
|
|
|
|
|
|
ArchiveTools.WriteTorrentGZ(input, _outdir, _logger);
|
|
|
|
|
|
if (_delete)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
File.Delete(input);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_logger.Error(ex.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
2016-08-24 20:33:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|