using System;
using System.Globalization;
using System.Threading;
using System.IO;
using System.Diagnostics;

// gdrive.exe
// ทำงานแบบ Windows Application 

namespace Original
{
    static class Program
    {
        public static string MyProg="gdrive".ToLower();
        public static string cFleWorkAct = "";
        private static string MyApp = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
        public static int nForWork = 1,ntemp=1;
        public static string cTemp = "", cTemp2 = "";  // ตัวแปรดัมมี่
        public static bool insource =false;  // 
        public static bool inharddisk = false;
        public static bool App4Console = false; // true ทำงานแบบ Console Application ,fause ทำงานแบบ Windows Application 
        public static string MyFolderApp = System.IO.Directory.GetCurrentDirectory();  // โพลเดอร์ทำงาน
        // ตัวแปรเพิ่มเติ่ม
        // private static int var 
        private static int myStandalone = 0;
        private static string myGdrive= @"G:\My Drive\Backup\Utility\";
        private static string myProgramn = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

        static void Readme()
        {
            string Fle4Readme=myGdrive+MyProg+"Readme.txt";
            TextWriter Logtxt = new StreamWriter(Fle4Readme);
            // การใช้งาน
            Logtxt.WriteLine("*" + MyProg + ".exe  ,DT : "+ File.GetLastWriteTime(MyApp).ToString("dd/MM/yy HH:mm"));
            Logtxt.WriteLine(MyApp);
            Logtxt.WriteLine(""); 
            Logtxt.WriteLine("ชุดคำสั่ง ");
            Logtxt.WriteLine("-" + MyProg + " " + "0 --> หรือช่องว่างทำงานตามที่ฝั่งตัวไว้ รอ 3 นาที");
            Logtxt.WriteLine("-" + MyProg + " " + "1 --> คล้าย 0 แต่ไม่รอ");
            Logtxt.WriteLine("-" + MyProg + " " + "S --> สร้างลิงค์ตอน BOOT");
            Logtxt.WriteLine("-" + MyProg + " " + "U --> คำสั่ง Subst U: "+ @"G:\My Drive");
            Logtxt.WriteLine("-" + MyProg + " " + "20 --> ตอนเข้าWindows");
            Logtxt.WriteLine("-" + MyProg + " " + "21 --> ตอนเข้าโปรแกรม");
            Logtxt.WriteLine("-" + MyProg + " " + "22 --> ระหว่างทำงาน");
            Logtxt.WriteLine("-" + MyProg + " " + "23 --> ออกโปรแกรม");
            Logtxt.WriteLine(""); 
            Logtxt.WriteLine("myGdrive = "+myGdrive);
            cTemp = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Gdrive Library.lnk";
            if (File.Exists(cTemp)) { Logtxt.WriteLine("Found "+cTemp); } else { Logtxt.WriteLine("No Found " + cTemp); }
            Logtxt.WriteLine("");
            string[] readText;

            cTemp = "customer.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("customer.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }


           cTemp = myGdrive + "Gdrive_Act.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("Gdrive_Act.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }


            cTemp = myGdrive + "ProcessAuto.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("ProcessAuto.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
            }
            else { Logtxt.WriteLine(cTemp+" ทำงานอัตโนมัติ ไฟล์  *.exe *.bat"); }
            Logtxt.WriteLine(""); Logtxt.WriteLine("");

            cTemp = myGdrive + "Welcome.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("Welcome.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = myGdrive + "Login.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("Login.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = myGdrive + "Logprocess.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("Logprocess.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = myGdrive + "Temp\\Windowlogin.jpg";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("Windowlogin.jpg ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = myGdrive + "Logout.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("Logout.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }


            cTemp = cFleWorkAct;
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine(cTemp + " ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = myGdrive + "ScanFolder.txt";
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("ScanFolder.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }

            cTemp = MyFolderApp + "\\GdriveSetup.txt"; 
            if (File.Exists(cTemp))
            {
                Logtxt.WriteLine("GdriveSetup.txt ,DT:" + File.GetLastWriteTime(cTemp).ToString("dd/MM/yy HH:mm"));
                Logtxt.WriteLine(cTemp);
                readText = File.ReadAllLines(cTemp);
                foreach (string s in readText) { if (s != "") { Logtxt.WriteLine(s.Trim()); } }
                Logtxt.WriteLine(""); Logtxt.WriteLine("");
            }


            Logtxt.WriteLine(""); Logtxt.WriteLine("");	
            Logtxt.Close();Logtxt.Dispose();
            if (Layout.Program.Password.Input_Password("Open Readme.txt")) 
            {  Util.Program.OpenWebSite(Fle4Readme);  } 
            cTemp = "";cTemp2 = "";
        }

        
        static void SoftwareLogin(int nOption)
        {
            if (myStandalone == 1) { return; }
            string cFile = "Log.txt";
            switch (nOption)  {
               case (0): // ตอนเข้า Windows 
                    cFile = "Welcome.txt";
                    if (insource) { cTemp = myProgramn.ToLower() ; ntemp = 0; }
                    else { cTemp = ""; ntemp = 4; }
                    if (insource) { Layout.Program.LayOut.ScreenShow(ntemp, "WelCome To Windows"
                        , Environment.GetEnvironmentVariable("USERDOMAIN")
                        ,Environment.GetEnvironmentVariable("USERNAME"), "", cTemp);  }
                    break;  
                case (1): cFile = "Login.txt"; break; // ตอนเข้าโปรแกรม
                case (2): cFile = "Logprocess.txt"; break; // ตอนโปรแกรมกำลังทำงาน
                case (3): cFile = "Logout.txt"; break; // ตอนออกจากโปรแกรม
            }
            cFile = cFile.Trim();
            TextWriter Logtxt = new StreamWriter(myGdrive + cFile);
            Logtxt.WriteLine("PC  : " + Environment.GetEnvironmentVariable("USERDOMAIN"));
            Logtxt.WriteLine("USER: " + Environment.GetEnvironmentVariable("USERNAME"));
            Logtxt.WriteLine("DATE:"+DateTime.Now.ToString("dd/MM/yyyy H:mm"));
            if (nOption==0) // เข้าWindows 
            {
                Logtxt.WriteLine("");
                DriveInfo[] allDrives = DriveInfo.GetDrives();
                double nPercent = 0, dAvilable = 0, dTotalsize = 0;
                foreach (DriveInfo hd in allDrives)
                {
                    if (hd.IsReady)
                    {
                        dTotalsize = hd.TotalSize;
                        dAvilable = hd.AvailableFreeSpace;
                        nPercent = dAvilable / dTotalsize;
                        Logtxt.WriteLine("ไดร์ "+hd.Name+" " +hd.VolumeLabel+" พื้นที่ " + Util.Program.TransformFileSize(dTotalsize) 
                            + " เหลือ " + Util.Program.TransformFileSize(dAvilable)
                            + "  คิดเป็น " + nPercent.ToString("P"));
                    }
                }
                Logtxt.WriteLine("");
                cTemp = @"D:\BdPosVFP\HoVFP.exe";  if (File.Exists(cTemp)) { Logtxt.WriteLine(Util.Program.FileDecrip(cTemp)); }
                cTemp = @"D:\BdPosVFP\PosVFP.exe"; if (File.Exists(cTemp)) { Logtxt.WriteLine(Util.Program.FileDecrip(cTemp)); }
            }
            cTemp = "";cTemp2 = "";
            Logtxt.Close();
            Logtxt.Dispose();
        }

        static void GdriveSetup()  
        {
            TextWriter Logtxt = new StreamWriter("GdriveSetup.txt");
            Logtxt.WriteLine("Start " + " " + DateTime.Now.ToString("dd/MM/yyyy H:mm"));
            // Copy โปรแกรม
            if (File.Exists("Gdrive.exe") && !File.Exists(@"C:\Program Files (x64)\Gdrive\Gdrive.exe"))    
                {
                if (!Directory.Exists(@"C:\Program Files (x64)\Gdrive"))
                { Directory.CreateDirectory(@"C:\Program Files (x64)\Gdrive"); Logtxt.WriteLine("สร้างโพลเดอร์ C:\\Program Files (x64)\\Gdrive");  }
                File.Copy("Gdrive.exe",@"C:\Program Files (x64)\Gdrive\Gdrive.exe", true); Logtxt.WriteLine("Copy Gdrive.exe ");
            }
            // Copy ไอคอน 
            if (Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup)))
            {
                if (File.Exists("Gdrive Library.ln_") && !File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Gdrive Library.lnk"))
                {
                    File.Copy("Gdrive Library.ln_",Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Gdrive Library.lnk");
                    Logtxt.WriteLine("Copy "+ Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Gdrive Library.lnk");
                }
                // ตรวจสอบอีกครั้ง กันพลาด
                if (File.Exists("Gdrive.ln_") && !File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Gdrive Library.lnk"))
                {
                    File.Copy("Gdrive.ln_", Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Gdrive Library.lnk");
                    Logtxt.WriteLine("Copy " + Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Gdrive Library.lnk รอบ 2");
                }
            }
            cTemp = @"C:\Program Files (x64)\Gdrive\Gdrive.exe";
            if (File.Exists(cTemp)) { cTemp="พบไฟล์ "+cTemp; } else { cTemp="ไม่พบไฟล์ " + cTemp; }
            cTemp2 = cTemp;
            Logtxt.WriteLine(cTemp);

            cTemp = Environment.GetFolderPath(Environment.SpecialFolder.Startup) + "\\Gdrive Library.lnk";
            if (File.Exists(cTemp)) { cTemp = "พบไฟล์ " + cTemp; } else { cTemp = "ไม่พบไฟล์ " + cTemp; }
            cTemp2 = cTemp2+" ,"+cTemp;
            Logtxt.WriteLine(cTemp);

            cTemp = "Gdrive Library.ln_";
            if (!File.Exists(cTemp))
            {
                cTemp="ไม่พบไฟล์ " + cTemp+" สำหรับสร้าง Shortcut";
                cTemp2 = cTemp2 + " ," + cTemp;
                Logtxt.WriteLine(cTemp);
            }
            Logtxt.Close();Logtxt.Dispose();  // Pipop
            Util.Program.Pok_Process("util_pok2", "1 sA6IH6vxMDv7lgUbW8KlCPHRFfAKy8UOBg6rLt1em3P " + cTemp2);
            cTemp = "";cTemp2 = "";
        }

        
        // สั่งรันโปรแกรม อัตโนมัติ
        static void ProcessRun()  
        {
            if (!Directory.Exists(myGdrive + "Temp")) { return; }
            int nSend2Line = 1;
            if (File.Exists(myGdrive + "Temp\\send2line.txt"))
            {
                string[] txtsend2line = File.ReadAllLines(myGdrive + "Temp\\send2line.txt");
                cTemp = txtsend2line[0].Trim(); cTemp = cTemp.Substring(0,1);
                int.TryParse(cTemp, out nSend2Line);
            }
            cTemp = "lock_" + Environment.MachineName + "_" + Environment.UserName+".rar"; 
			if (myStandalone == 1) // ให้Download แทนการไปใส่ในคลาวด์   
			{
                if (!File.Exists("Environment.txt") && File.Exists("util_pok2.exe")) { Util.Program.Pok_Process("util_pok2", "3 0 0"); }
                if (File.Exists("Utility\\temp\\" + cTemp)) {Util.Program.Pok_Process("unrar", "x -o+ Utility\\temp\\" + cTemp+ " Utility\\Temp"); }

                string[] lines;
                cTemp2 = "";
                if (File.Exists("customer.txt")) 
                {
                    if (File.Exists(myGdrive+ "Temp\\customer.txt")) { cTemp2 = myGdrive + "Temp\\customer.txt"; } else { cTemp2 = "customer.txt"; }
                    lines = File.ReadAllLines(cTemp2);
                    cTemp2 = "";
                    foreach (string line in lines)
                    { if (line.Trim()!="") { cTemp2 = cTemp2 + line.Trim() + "*"; } }
                    cTemp2 = cTemp2+"uyod.net/customer/" + cTemp;
                    Util.Program.Pok_Process("util_pok2", "1 sA6IH6vxMDv7lgUbW8KlCPHRFfAKy8UOBg6rLt1em3P " + cTemp2); 
                }
                cTemp2 = "Stand Alone = https://uyod.net/customer/" + cTemp;
                if (Util.Program.UyodDotNetDownload(2, "customer/"+cTemp, "Utility\\Temp\\" + cTemp))  // ส่งสำเร็จ
                {
                    if (nSend2Line == 1) { Util.Program.Pok_Process("util_pok2", "1 sA6IH6vxMDv7lgUbW8KlCPHRFfAKy8UOBg6rLt1em3P " + cTemp2 + " ส่งสำเร็จ"); }
                }
                else // ส่งไม่สำเร็จ
                {
                    if (nSend2Line == 1) { Util.Program.Pok_Process("util_pok2", "1 sA6IH6vxMDv7lgUbW8KlCPHRFfAKy8UOBg6rLt1em3P " + cTemp2); }
                    if (File.Exists("Environment.txt"))
                    {
                        cTemp2 = "";
                        lines = File.ReadAllLines("Environment.txt");
                        for (int i = 0; i <= 7; i++) { cTemp2 = cTemp2 + lines[i].Trim() + ","; }
                        if (nSend2Line == 1) { Util.Program.Pok_Process("util_pok2", "1 sA6IH6vxMDv7lgUbW8KlCPHRFfAKy8UOBg6rLt1em3P " + cTemp2); }
                    }
                }
            }
			cTemp = ""; cTemp2 = "";
            TextWriter Logtxt = new StreamWriter(myGdrive + "ProcessAuto.txt");
            Logtxt.WriteLine("Start "+" " + DateTime.Now.ToString("dd/MM/yyyy H:mm"));
            Logtxt.WriteLine("");
            DirectoryInfo dFolderList;
            FileInfo[] FileList;
            // 
            dFolderList = new DirectoryInfo(myGdrive + "Temp");
            FileList = dFolderList.GetFiles("*.exe", SearchOption.AllDirectories);
            foreach (FileInfo cFileList in FileList)
            {
                cTemp = cTemp + cFileList.Name.Trim();
                Logtxt.WriteLine(cFileList.FullName+" "+ DateTime.Now.ToString("dd/MM/yyyy H:mm"));
                Process.Start(cFileList.FullName);
            }
            dFolderList = new DirectoryInfo(myGdrive + "Temp");
            FileList = dFolderList.GetFiles("*.bat", SearchOption.AllDirectories);
            foreach (FileInfo cFileList in FileList)
            {
                cTemp = cTemp + cFileList.Name.Trim();
                Logtxt.WriteLine(cFileList.FullName + " " + DateTime.Now.ToString("dd/MM/yyyy H:mm"));
                Process.Start(cFileList.FullName);
            }
            Logtxt.WriteLine("");
            Logtxt.WriteLine("Finish " + " " + DateTime.Now.ToString("dd/MM/yyyy H:mm"));
            Logtxt.Close();
            Logtxt.Dispose();
            if (cTemp!="") { Util.Program.Pok_Process("util_pok2", "1 sA6IH6vxMDv7lgUbW8KlCPHRFfAKy8UOBg6rLt1em3P " + cTemp); } // Pipop
            cTemp ="";cTemp2="";
		}

        // Save รูป 
        static void SaveScreen(string cName)
        {
            if (myStandalone == 1) { return; }
            cTemp = "";
            if (File.Exists(@"D:\BdPosVFP\UtilPOK3.exe")) { cTemp = @"D:\BdPosVFP\"; }
            if (cTemp=="" && File.Exists(@"D:\BdPosVFP\UtilPOK3.exe")) { cTemp = @"D:\BdPos\"; }
            if (cTemp!="")
            {
                cName = cName.Trim();
                Util.Program.Pok_Process(cTemp+"UtilPOK3.exe", "1 Temp Temp1");
                if (File.Exists(@"Temp\Temp1.jpg"))
                { File.Copy(@"Temp\Temp1.jpg", myGdrive + "Temp\\"+cName+".jpg", true); }
            }
            cTemp = "";
        }

        static bool Repoway()  // ตรวจสอบกับระบบเก่า Dbfrun\Repoway
        {
            // E:\_CLOUD\TONKHAM\BACKUP\
            bool lRepoway = false;
            cTemp = @"D:\BdPos123456\Repopok.txt";  // ไม่ต้องทำงาน ส่วนเวอร์ชั่นเก่า ทำเป็น Standalone แทน
            if (File.Exists(cTemp))
            {
                string[] lines = File.ReadAllLines(cTemp);
                cTemp2 = lines[0].Trim();
                if (cTemp2!="" && Directory.Exists(cTemp2))
                {
                    myGdrive = cTemp2;
                    lRepoway = true;
                }
            }
            return lRepoway;
        }


        static void ScanFolder() // สแกนDrive ว่ามี Folder อะไรบ้าง
        {
            if (myStandalone == 1) { return; }
            TextWriter Logtxt = new StreamWriter(myGdrive + "ScanFolder.txt");
            Logtxt.WriteLine("Start " + " " + DateTime.Now.ToString("dd/MM/yyyy H:mm"));
            DirectoryInfo dFolderList;
            DirectoryInfo[] DirList;
            FileInfo[] FileList;
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            foreach (DriveInfo hd in allDrives)
            {
                if (hd.IsReady)
                {
                    dFolderList = new DirectoryInfo(hd.Name);
                    Logtxt.WriteLine("");
                    Logtxt.WriteLine(hd.Name.Substring(0,2) + " " + hd.VolumeLabel);
                    DirList = dFolderList.GetDirectories("*.*", SearchOption.TopDirectoryOnly);
                    foreach (DirectoryInfo cDirList in DirList)
                    {
                        Logtxt.WriteLine("  "+cDirList.FullName + " ,Attrib = " + cDirList.Attributes + " ,DT = " + cDirList.CreationTime.ToString("dd/MM/yyyy H:mm"));
                    }
                }
            }
            // หาใน 3  Folder โปรแกรม 
            for (int i = 1; i <= 3; i++)
            {
                switch (i)
                {
                    case (1): cTemp = @"C:\Program Files"; break;
                    case (2): cTemp = @"C:\Program Files (x64)"; break;
                    case (3): cTemp = @"C:\Program Files (x86)"; break;
                }
                dFolderList = new DirectoryInfo(cTemp);
                Logtxt.WriteLine("");
                Logtxt.WriteLine("Folder : "+cTemp);
                DirList = dFolderList.GetDirectories("*.*", SearchOption.TopDirectoryOnly);
                foreach (DirectoryInfo cDirList in DirList)
                {
                    Logtxt.WriteLine("  " + cDirList.FullName + " ,Attrib = " + cDirList.Attributes + " ,DT = " + cDirList.CreationTime.ToString("dd/MM/yyyy H:mm"));
                }
            }
            Logtxt.Close();Logtxt.Dispose();
            cTemp = "";cTemp2 = "";
        }


        // ตรวจหาโพลเดอร์ใน G Drive
        private static string BackupUTL(string cParFolder)
        {
            string cbackFolder = "";
            string[] dirs = Directory.GetDirectories(@"G:\My Drive",cParFolder , SearchOption.AllDirectories);
            foreach (string dir in dirs)  { if (cbackFolder=="") { cbackFolder = dir.Trim()+@"\"; }  }
            return cbackFolder;
        }


        static void init()
        {
            if (!File.Exists("customer.txt"))
            {
                TextWriter initTxt = new StreamWriter("customer.txt");
                initTxt.WriteLine("บริษัท ไม่ได้ระบุ จำกัด");
                initTxt.WriteLine("ที่อยู่ ตรงไหน ครับ");
                initTxt.Close(); initTxt.Dispose();
                Util.Program.Pok_Process("notepad", "customer.txt"); 
            }
        }

        static void GDrvMapU()
        {
            if (Directory.Exists("U:\\")) { Util.Program.Pok_InHarddisk("มีไดร์ U: แล้ว"); return; }
            if (Directory.Exists("G:\\My Drive"))
            {
                Subst.MapDrive('U', @"G:\My Drive");
                if (Directory.Exists("U:\\")) { Util.Program.Pok_InHarddisk("Subst.MapDrive สร้างสำเร็จ"); }
                else 
                {
                    if (!File.Exists("GDrvMapU.bat"))
                    {
                        TextWriter GDrv = new StreamWriter("GDrvMapU.bat");
                        GDrv.WriteLine("subst U: "+(char)34 +"G:\\My Drive" + (char)34);
                        GDrv.Close(); GDrv.Dispose();    // (char)34
                    }
                    Process.Start("GDrvMapU.bat"); Util.Program.Pok_InHarddisk("GDrvMapU.bat สร้างสำเร็จ");
                }
            }
            if (Directory.Exists("U:\\")) { Util.Program.Pok_InHarddisk("Map U: สำเร็จ"); } else { Util.Program.Pok_InHarddisk("Map U: ไม่สำเร็จ"); }
        }


        static void Test_Prog()
        {   // แสดงว่า เป็น การทดสอบ 
            // Readme(); // Pipop
            GdriveSetup();  
        }


        static void Main(string[] args)
        {
            init();
            if (MyApp.Substring(0, 2) == "V:") { insource = true; } // Run กับ Source
            if (Directory.Exists("R:\\Temp")) { inharddisk = true; }  // Run กับ Harddisk   
            string cpFlag;            
			if (args.Length == 0) { if (insource) { cpFlag = "T"; } else { cpFlag = "0"; } }  // Pipop T เป็นการทดสอบ
            else { cpFlag = args[0].ToUpper().Trim(); }

            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB"); // เปลี่ยนรูปแบบวันที่ เป็น GB
            if (!Directory.Exists("Temp")) { Directory.CreateDirectory("Temp"); }
            if (Directory.Exists(@"G:\My Drive")) { myGdrive = BackupUTL(@"Backup\Utility"); if (myGdrive == "") { return; } } // 1 เจอGdrive
            else { if (Directory.Exists(@"V:\_My Cloud\My Drive")) { myGdrive = @"V:\_My Cloud\My Drive\Backup\Utility\"; } // 2 ทำงานกับ Source
                else { if (Repoway()) { myGdrive = myGdrive + @"Utility\"; } // 3 เจอ BdPos 
                    else { myStandalone = 1; } // ไม่เจอให้ออกเลย 
                }
            }

            if (myStandalone == 1 && inharddisk==false) // Run แบบ Standalone 
            {
                myGdrive = MyFolderApp + @"\Utility\";
                if (!File.Exists("UnRAR.exe")) {Util.Program.UyodDotNetDownload(1, "exe/UnRAR.exe", "UnRAR.exe"); }
				if (!File.Exists("Rar.exe")) {Util.Program.UyodDotNetDownload(1, "exe/Rar.exe", "Rar.exe"); }
                if (!Directory.Exists(myGdrive)) { Directory.CreateDirectory(myGdrive); }
                if (!Directory.Exists(myGdrive + "Temp")) { Directory.CreateDirectory(myGdrive+"Temp"); }
				if (!File.Exists("uyod_s1.exe")) 
				{
					Util.Program.UyodDotNetDownload(1, "rar/c.rar","c.rar");
					Util.Program.Pok_Process("unrar", "x -o+ c.rar");
				}
			}

            TextWriter Acttxt = new StreamWriter(myGdrive + "Gdrive_Act.txt");
            Acttxt.WriteLine("DT : " + DateTime.Now.ToString("dd/MM/yy HH:mm"));
            Acttxt.WriteLine("insource : " + insource);
            Acttxt.WriteLine("inharddisk : " + inharddisk);
            Acttxt.WriteLine("cpFlag : " + cpFlag);

            // 3 นาที Run ผ่าน Start Up Menu ให้รอด้วย
            if (cpFlag == "0") {
                Util.Program.Pok_InHarddisk("อาจรอ 3 นาที");
                MyTime.Program.Pok_TimeWait(1, 3 * 60);
                Acttxt.WriteLine("รอ 3 นาที");  }  

            Util.Program.Pok_InSource(MyProg+".exe -->cpFlag : " + cpFlag + Environment.NewLine + Environment.NewLine
                + "myGdrive : " + myGdrive + Environment.NewLine + Environment.NewLine
                + "myStandalone : " + myStandalone);

            Acttxt.WriteLine("myGdrive : " + myGdrive);
            Acttxt.WriteLine("myStandalone : " + myStandalone);
            Acttxt.Close(); Acttxt.Dispose();

            Util.Program.FolderStartupShow();
            if (!Directory.Exists(myGdrive)) { Directory.CreateDirectory(myGdrive); }
            if (!Directory.Exists(myGdrive+"Temp")) { Directory.CreateDirectory(myGdrive+"Temp"); }
			nForWork=1;  //    PipopYou ปรกติ  1 nForWork=0 ไม่ทำอะไรเลย ,1 ทำงานตามปรกติ 2 ไม่Runโปรแกรมและมีเตือน  
            cFleWorkAct = myGdrive + "Gdrive2Activate.txt";
            switch (cpFlag) // Switch
            {
                case ("1"): 
                case ("0"): SoftwareLogin(0); ProcessRun();SaveScreen("Windowlogin");ScanFolder(); break;
                case ("?"): Readme(); break;
                case ("S"): GdriveSetup(); break;
                case ("U"): GDrvMapU(); break;  
                case ("T"): Test_Prog(); break;  
                case ("20"): SoftwareLogin(0);break; //  เข้าวินโดร์
                case ("21"): SoftwareLogin(1); break; // เข้าโปรแกรม
                case ("22"): SoftwareLogin(2); break; // ระหว่างทำงาน
                case ("23"): SoftwareLogin(3); break; // ออกจากโปรแกรม 
                default:  break;  
            } 
        }
    }
}

