//  
//   Rebex Sample Code License
// 
//   Copyright 2026, REBEX CR s.r.o.
//   All rights reserved.
//   https://www.rebex.net/
// 
//   Permission to use, copy, modify, and/or distribute this software for any
//   purpose with or without fee is hereby granted.
// 
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//   OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//   OTHER DEALINGS IN THE SOFTWARE.
// 

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

using Rebex.IO;
using Rebex.IO.Compression;

namespace Rebex.Samples
{
    /// <summary>
    /// Simple command line utility that compresses files and/or directories into a ZIP archive.
    /// </summary>
    class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// <param name="args">Program arguments.</param>
        static void Main(string[] args)
        {
            // set the trial license key
            Rebex.Licensing.Key = Rebex.Samples.TrialKey.Key;


            if (args.Length == 0)
            {
                ShowHelp();
                return;
            }

            try
            {
                // parse the command line
                Arguments config = new Arguments(args);

                // You can execute the whole operation in a single line of code as follows:
                //	ZipArchive.Add(config.ZipArchivePath, config.SourcePath, null, 0, 0, ActionOnExistingFiles.OverwriteAll);
                
                // If you need to perform multiple ZIP archive operations, 
                // then the following pattern is more efficient:

                // open or create a ZIP archive and add files to it
                using (ZipArchive archive = new ZipArchive(config.ZipArchivePath))
                {
                    ArchiveOperationResult result = archive.Add(
                        config.SourcePath,
                        null,
                        TraversalMode.Recursive,
                        TransferMethod.Copy,
                        ActionOnExistingFiles.OverwriteAll);

                    Console.WriteLine("Added {0} file(s), {1} byte(s) to {2}.",
                        result.FilesAffected,
                        result.FilesUncompressedLength,
                        archive.FilePath
                    );
                }
            }
            catch (SampleException e)
            {
                // display an error reported by arguments parser class
                Console.WriteLine("Error: {0}", e.Message);

                // display help
                ShowHelp();
            }
            catch (ZipException e)
            {
                // display an error reported by the ZIP component
                Console.WriteLine(e.Message);
            }
            catch (DirectoryNotFoundException e)
            {
                // display directory-not-found error
                Console.WriteLine(e.Message);
            }
            catch (FileNotFoundException e)
            {
                // display file-not-found error
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                // display all other errors
                Console.WriteLine("Error: {0}", e);
            }
        }

        private static void ShowHelp()
        {
            string applicationName = AppDomain.CurrentDomain.FriendlyName;
            Console.WriteLine("=====================================================================");
            Console.WriteLine(" Rebex {0} - Adds files to a ZIP archive", applicationName);
            Console.WriteLine("=====================================================================");
            Console.WriteLine();
            Console.WriteLine("The program is a sample for Rebex ZIP for .NET component.");
            Console.WriteLine("For more info, see http://www.rebex.net/zip.net/");
            Console.WriteLine();
            Console.WriteLine("Syntax: {0} zip-file-path [path-or-mask]", applicationName);
            Console.WriteLine(@"Example: {0} C:\archive.zip ", applicationName);
            Console.WriteLine(@"Example: {0} C:\archive.zip C:\file.txt", applicationName);
            Console.WriteLine(@"Example: {0} C:\archive.zip C:\data", applicationName);
            Console.WriteLine(@"Example: {0} C:\archive.zip C:\data\*", applicationName);
            Console.WriteLine(@"Example: {0} C:\archive.zip C:\data\*.txt", applicationName);
        }
    }
}
