using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace FindAndReplace
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 2 && args[0].ToUpper() == "#COPIER#")
            {
                string filename = args[1];
                if (File.Exists(filename))
                {
                    try
                    {
                        StringBuilder sb = new StringBuilder();
                        using (StreamReader reader = new StreamReader(filename, Encoding.UTF8))
                        {
                            string s;
                            while ((s = reader.ReadLine()) != null)
                            {
                                sb.AppendLine(string.Format("{0}###{0}", s));
                            }
                        }
                        using (StreamWriter writer = new StreamWriter(filename, false, Encoding.UTF8))
                        {
                            writer.Write(sb.ToString());
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Le processus a échoué: " + e.Message);
                    }
                }
                else
                    Console.WriteLine("Fichier introuvable");
            }
            else if (args.Length == 3)
            {
                string filename = args[2];
                if (File.Exists(filename))
                {
                    try
                    {
                        string s;
                        StringBuilder sb = new StringBuilder();
                        using (StreamReader reader = new StreamReader(filename, Encoding.UTF8))
                        {
                            if (args[0].ToUpper() == "#DEBUT#")
                                while ((s = reader.ReadLine()) != null)
                                {
                                    sb.AppendLine(string.Format("{0}{1}", args[1], s));
                                }
                            else if (args[0].ToUpper() == "#FIN#")
                                while ((s = reader.ReadLine()) != null)
                                {
                                    sb.AppendLine(string.Format("{0}{1}", s, args[1]));
                                }
                            else
                            {
                                s = reader.ReadToEnd();
                                Regex reg = new Regex(args[0], RegexOptions.IgnoreCase);
                                sb.Append(reg.Replace(s, args[1]));
                            }
                        }
                        using (StreamWriter writer = new StreamWriter(filename, false, Encoding.UTF8))
                        {
                            writer.Write(sb.ToString());
                        }
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Le processus a échoué: " + e.Message);
                    }
                }
                else
                    Console.WriteLine("Fichier introuvable");
            }
            else
                Console.WriteLine("Nombre d'arguments incorrect");
        }
    }
}

