using System;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Timers;
using System.Threading;
// uyodload.exe
// ทำงานแบบ Console Application 

namespace Original
{
    static class Program
    {
        public class G
        {
            public static string cpDir;
            public static string cpFlag;
            public static string cpText1;
            public static string cpText2;
            public static string FileDownloadName = "";
            public static bool lpDworking = true;
            public static string txtafterload="";
        }
        public static string MyProg = "uyodload".ToLower();
        public static bool App4Console = true; // true ทำงานแบบ Console Application ,fause ทำงานแบบ Windows Application 
        private static System.Timers.Timer aTimer;
        private static DateTime StartDate;
        public static bool insource=false;
        public static bool inharddisk = false;
        private static string cTemp = "";
        public static int nForWork = 1;
        public static string cFleWorkAct = "";  // ไว้ในในอนาคต ต่อไป 
        private static string MyApp=System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
        public static string MyFolderApp = System.IO.Directory.GetCurrentDirectory();  // โพลเดอร์ทำงาน
        private static bool lDebug = false; // สำหรับ Debug ขณะรันโปรแกรม

        static void ReadMe()
        {
            var nYesno = MessageBox.Show("Uyodload 1 ---> Download Source Code" + System.Environment.NewLine
        + "Uyodload 2 ---> Download Tools" + System.Environment.NewLine
        + "Uyodload B ---> Install D:\\Bdpos" + System.Environment.NewLine 
        + "Uyodload V ---> Install D:\\BdposVFP" + System.Environment.NewLine 
        + "Uyodload C ---> Check Environment" + System.Environment.NewLine
        + "Uyodload S ---> Show Environment" + System.Environment.NewLine + System.Environment.NewLine
        + "Uyodload U ---> Update D:\\Bdpos" + System.Environment.NewLine + System.Environment.NewLine
        + "Uyodload A A ---> Download Hotel ชุด A" + System.Environment.NewLine
        + "Uyodload A B ---> Download Hotel ชุด B" + System.Environment.NewLine
        + "Uyodload A C ---> Download Hotel ชุด C" + System.Environment.NewLine + System.Environment.NewLine
        + "Uyodload P B ---> Download POS BUDDY" + System.Environment.NewLine
        + "Uyodload P S ---> Download POS Supplier" + System.Environment.NewLine + System.Environment.NewLine
        + "Uyodload F B ---> Download FOOD BUDDY" + System.Environment.NewLine
        + "Uyodload F S ---> Download FOOD Supplier" + System.Environment.NewLine + System.Environment.NewLine
        + "Uyodload Z 1 ---> Download และติดตั้ง bdposvfp_update.zip" + System.Environment.NewLine
        + "Uyodload Z 11 ---> Download และติดตั้ง bdposvfp_new_exe.rar" + System.Environment.NewLine
        + "Uyodload Z 2 ---> Zip To bdposvfp_update.zip" + System.Environment.NewLine 
        + "Uyodload Z 3 ---> Download และติดตั้ง bdposvfp.rar" + System.Environment.NewLine
        + "Uyodload Z 4 ---> Download และติดตั้ง samplevfp.rar" + System.Environment.NewLine + System.Environment.NewLine
        + "Uyodload Z 5 ---> Download และติดตั้ง bdposvfp_hit.rar" + System.Environment.NewLine + System.Environment.NewLine
        + "Uyodload Z 9 ---> Download และติดตั้งโปรแกรมช่วยงาน" + System.Environment.NewLine + System.Environment.NewLine
        + "Uyodload W 0 ---> Download วินโดร์เถื่อน" + System.Environment.NewLine
        + "Uyodload W 1 ---> Download วินโดร์แท้" + System.Environment.NewLine
        + "Uyodload Y ชื่อไฟล์ ---> upload ขึ้นเวบ" + System.Environment.NewLine
        + "Uyodload R ลงทะเบียนโปรแกรมชุด Bdpos"
        + "" + System.Environment.NewLine + System.Environment.NewLine
        + "เลือก Yes เพื่อเข้า DOS"
        , "", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button2);
        if (nYesno == DialogResult.Yes) { Process.Start("Uyodloadbat.bat");}
        }


        static void UyodMess(string cfWord)
        {
            MessageBox.Show(cfWord + System.Environment.NewLine
            + "" + System.Environment.NewLine
            + "" + System.Environment.NewLine
            , "", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }


        private static void Process2Arg(string mArg_1, String mArg_2)  
        {
            mArg_1 = mArg_1.Trim();
            switch (mArg_1.ToLower())
            {
                case ("unrar.exe"): Console.WriteLine("Extract File  :  " + mArg_2); break;
                case ("rar.exe"): Console.WriteLine("Zip File  :  " + mArg_2); break;
                default: Console.WriteLine("Command  : " + mArg_1 + " " + mArg_2); break;
            }
            Process PokProcess = new Process();
            PokProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            PokProcess.StartInfo.FileName = mArg_1;
            PokProcess.StartInfo.Arguments = mArg_2;
            PokProcess.StartInfo.UseShellExecute = false;
            PokProcess.StartInfo.CreateNoWindow = true;
            PokProcess.StartInfo.RedirectStandardOutput = true;
            PokProcess.StartInfo.RedirectStandardInput = true;
            PokProcess.Start();
        }


        static void cr_dir()
        {
            if (G.cpDir == "") { return; }
            if (!Directory.Exists(G.cpDir))
            {
                Directory.CreateDirectory(G.cpDir);
                if (insource) { UyodMess("สร้างโพลเดอร์ " + G.cpDir); }
            }
        }

        private static void OpenWebSite(string cAdressOpen)
        {
            string FleBrowser = "iexplore.exe";
            if (File.Exists(@"C:\Program Files\Mozilla Firefox\firefox.exe"))
            { FleBrowser = "firefox.exe"; }
            if (File.Exists(@"C:\Program Files\Google\Chrome\Application\chrome.exe"))
            { FleBrowser = "chrome.exe"; }
            var opentwebthis = new ProcessStartInfo(FleBrowser);
            opentwebthis.Arguments = cAdressOpen;
            Process.Start(opentwebthis);
        }


        private static void OpenFolder(int nOpen)
        {
            // nOpen = 0 , 1 , 2
			string cfFolder = G.cpDir.Trim();
            if (cfFolder == "") { return; }
            if (!Directory.Exists(cfFolder)) { Directory.CreateDirectory(cfFolder); }
            if (nOpen==1 || nOpen==2)
			{
			System.Diagnostics.Process process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = @"explorer";
            process.StartInfo.Arguments = cfFolder;
            process.Start();
			}
			if (nOpen !=2) {WhileLoad(2,"Folder : " +cfFolder);} // ส่งมาจาก download.txt มาต้องทำ 
		}


        private static void FLEError(string Error4Text,string ErrFLE1, string ErrFLE2)
        {
            Console.WriteLine(Error4Text);
            Console.WriteLine(""); Console.WriteLine("");
            Console.WriteLine("ERROR File Download:");
            Console.WriteLine("File Form Web "+ErrFLE1);
            Console.WriteLine("File Save " + ErrFLE2);
            Console.WriteLine(""); Console.WriteLine("");
        }


        static void DownloadFromWeb(int nFlag, int nDup, string myStringWebResource, string fileName)   
        {
            fileName = fileName.Trim();
            G.FileDownloadName = fileName;
            cr_dir();
            fileName = G.cpDir.Trim() + fileName.ToLower().Trim();
            string file_name = fileName.Substring(0, fileName.Length - 1) + "r";
            // nDup=0 เจอแล้ว ให้ออกทันที
            if (nDup == 0 && (File.Exists(fileName) || File.Exists(file_name))) { if (insource) { UyodMess("พบไฟล์ " + fileName); }; return; }

            try 
            { if (nDup == 1 && File.Exists(fileName)) {if (insource) { UyodMess("โหลดไฟล์ " + fileName + " ซ้ำ"); }; File.Delete(@fileName); } }
            catch (Exception ex) { FLEError(ex.ToString(), myStringWebResource, @fileName); return; }

            if (insource) { UyodMess("Download " + myStringWebResource+ "" + System.Environment.NewLine + "===>" + fileName); }
            // เริ่มทำการ Download 
            try
            { if (insource) 
                { UyodMess("From " + myStringWebResource + " To " + @fileName); } 
                else {Util.Program.UyodDotNetDownload(2, myStringWebResource, @fileName); }
				WhileLoad(1," From "+myStringWebResource+" To "+fileName);}
            catch (Exception ex) { FLEError(ex.ToString(),myStringWebResource, @fileName); return; }

            if (nFlag == 1) //  ให้มีการเตือนไฟล์ที่ Download 
            {
                if (File.Exists(@fileName))
                { MessageBox.Show("Download จากเวบ " + myStringWebResource + " สำเร็จ"
                    + System.Environment.NewLine + Application.StartupPath
                    , "", MessageBoxButtons.OK, MessageBoxIcon.Error);  }
                else
                { MessageBox.Show("Download จากเวบ " + myStringWebResource + " ไม่สำเร็จ"
                    + System.Environment.NewLine + Application.StartupPath
                    + System.Environment.NewLine + "กรุณาออกจากโปรแกรมแล้วเข้าใหม่"
                    , "", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            }
        }

        static void SourceDownload()
        {
            SetTimer();
            G.cpDir = @"\Source\"; cr_dir();
            OpenFolder(0);
            SourceFLList(1, "AP");
            SourceFLList(1, "AR");
            SourceFLList(1, "GL");
            SourceFLList(1, "HO");
            SourceFLList(1, "IV");
            SourceFLList(1, "PO");
            SourceFLList(1, "POSWIN");
            SourceFLList(1, "PS001");
            SourceFLList(1, "PSDBFRUN");
            SourceFLList(1, "LS");
            SourceFLList(1, "MA");
            SourceFLList(1, "SC");
            SourceFLList(1, "BMPPOS7");
            SourceFLList(1, "BMPHO7");
            SourceFLList(1, "DATA");
            SourceFLList(1, "TRANSAC");
            SourceFLList(1, "ORGDATA");
            SourceFLList(1, "ORGTRAN");
            SourceFLList(1, "DATA001");
            SourceFLList(1, "AMPHAN");
            SourceFLList(1, "HODBFRUN");
            SourceFLList(1, "UTIL");
            SourceFLList(1, "SME");
            SourceFLList(2, "UNSOURCE.BAT");
            SourceFLList(2, "PKUNZIP.EXE");
            G.cpDir = @"\Source\Original\";
            DownloadFromWeb(0, 0, "source/ORIGINAL/ALLPOS.zip", "allpos.zip");
            FinishDownload();
        }



        static void SourceFLList(int nFlag, string cfFilename)
        {
            G.FileDownloadName = cfFilename;
            string uYodweb, cFileCopy;
            uYodweb = "source/";
            cFileCopy = G.cpDir;
            string uYodweb2, cFileCopy2;
            if (nFlag == 2)
            {
                uYodweb2 = uYodweb + cfFilename.ToUpper();
                cFileCopy2 = cFileCopy + cfFilename.ToLower();
            }
            else
            {
                uYodweb2 = uYodweb + cfFilename.ToUpper() + ".ZP";
                cFileCopy2 = cFileCopy + cfFilename.ToLower() + ".zp";
            }
            if (File.Exists(cFileCopy2)) { File.Delete(cFileCopy2); }
            Util.Program.UyodDotNetDownload(2, uYodweb2, cFileCopy2);
        }


        static void SoftwareToolDownload()  
        {
            SetTimer();
            G.cpDir = @"\Software Setup\";
            // OpenFolder(0);
            DownloadFromWeb(0, 0, "rar/pipop_tools.rar", "pipop_tools.rar");
            DownloadFromWeb(0, 0, "toolsetup/installbackupandsync.exe", "installbackupandsync.exe");
            DownloadFromWeb(0, 0, "toolsetup/ChromeSetup.exe", "ChromeSetup.exe");
            DownloadFromWeb(0, 0, "toolsetup/AnyDesk/AnyDesk.exe", "AnyDesk.exe");
            DownloadFromWeb(0, 0, "toolsetup/acrobat/ar812lite_us.exe", "ar812lite_us.exe");
            if (File.Exists("C:\\Temp\\w64.bit")) { DownloadFromWeb(0, 0, "toolsetup/pdfmachine/pdfMachine64.exe", "pdfMachine.exe"); }
            else { DownloadFromWeb(0, 0, "toolsetup/pdfmachine/pdfMachine.exe", "pdfMachine.exe"); }
            DownloadFromWeb(0, 0, "toolsetup/dopdf/dopdf-7.exe", "dopdf-7.exe");
            DownloadFromWeb(0, 0, "toolsetup/wrar36b2.exe", "wrar36b2.exe");
            DownloadFromWeb(0, 0, "toolsetup/npp.7.4.2.Installer.exe", "npp.7.4.2.Installer.exe");

            DownloadFromWeb(0, 0, "rar/PDFAnnotator.rar", "PDFAnnotator.ra_");
            DownloadFromWeb(0, 0, "rar/teamviewer13crack.rar", "teamviewer13crack.ra_");
            DownloadFromWeb(0, 0, "rar/ultraviewer_setup_6.2_en.rar", "ultraviewer_setup_6.2_en.rar");

            DownloadFromWeb(0, 0, "toolsetup/avast_crack.rar", "avast_crack.ra_");
            DownloadFromWeb(0, 0, "toolsetup/winrar_crack.rar", "winrar_crack.ra_");
            DownloadFromWeb(0, 0, "toolsetup/filezilla_crack.rar", "filezilla_crack.ra_");
            DownloadFromWeb(0, 0, "toolsetup/FileZilla_3.13.0_win32-setup.exe", "FileZilla_3.13.0_win32-setup.exe");
            DownloadFromWeb(0, 0, "setup/fc_setup.exe", "fc_setup.exe");
            DownloadFromWeb(0, 0, "toolsetup/office2003.exe", "office2003.exe");

            DownloadFromWeb(0, 0, "exe/UnRAR.exe", "UnRAR.exe");
            DownloadFromWeb(0, 0, "rar/zip.rar", "zip.rar");
            DownloadFromWeb(0, 0, "zip/win8.rar", "win8crack.rar");
            DownloadFromWeb(0, 0, "zip/win7-2.rar", "win7crack.rar");
            DownloadFromWeb(0, 0, "zip/win10.rar", "win10crack.rar");

            DownloadFromWeb(0, 0, "toolsetup/LineInst.exe", "LineInst.exe");
			
            UNRar(G.cpDir);
            RenameCrackFile(G.cpDir);

            FinishDownload();

            Directory.SetCurrentDirectory(G.cpDir);  
            if (Directory.Exists(@"D:\BdPos") && !File.Exists(@"D:\BdPos\UnRAR.exe"))
            { Process2Arg("UnRAR.exe", "x zip.rar D:\\BdPos\\"); }
            if (Directory.Exists(@"D:\BdPosVFP") && !File.Exists(@"D:\BdPosVFP\UnRAR.exe"))
            { Process2Arg("UnRAR.exe", "x zip.rar D:\\BdPosVFP\\"); }

            // สร้างระบบสำรองข้อมูล   
            Console.WriteLine("Type Company Name : ");
            string cComName = Console.ReadLine();
            string FleLoad, FleTosave;
            cComName = cComName.Trim();
            if (cComName != null && cComName != "" && cComName.Length >= 2)
            {
                FleLoad = @"\_Cloud\" + cComName;
                if (!Directory.Exists(FleLoad)) { Directory.CreateDirectory(FleLoad); }
                FleTosave = FleLoad + "\\Backup";
                if (!Directory.Exists(FleTosave)) { Directory.CreateDirectory(FleTosave); }
                FleTosave = FleLoad + "\\Pdf";
                if (!Directory.Exists(FleTosave)) { Directory.CreateDirectory(FleTosave); }
                FleTosave = FleLoad + "\\Screen";
                if (!Directory.Exists(FleTosave)) { Directory.CreateDirectory(FleTosave); }
                FleTosave = FleLoad + "\\Txt";
                if (!Directory.Exists(FleTosave)) { Directory.CreateDirectory(FleTosave); }
                FleTosave = FleLoad + "\\UyodTeam";
                if (!Directory.Exists(FleTosave)) { Directory.CreateDirectory(FleTosave); }

                FleLoad = FleLoad + "\\Backup\\DEC";
                if (!Directory.Exists(FleLoad)) { Directory.CreateDirectory(FleLoad); }
                FleLoad = FleLoad + "\\Win019.exe";
                FleTosave = @"D:\BdPos\WinRG.exe";

                if (File.Exists(FleTosave) && !File.Exists(FleLoad))  { File.Copy(FleTosave, FleLoad); }

                FleLoad = @"\Software Setup\pipop_tools\Bat\backup2c.bat";
                FleTosave = @"\_Cloud\" + cComName + @"\backup2c.bat";
                if (File.Exists(FleLoad) && !File.Exists(FleTosave))
                { File.Copy(FleLoad, FleTosave); }
                FleLoad = @"\Software Setup\pipop_tools\Bat\upnet2c.bat";
                FleTosave = @"\_Cloud\" + cComName + @"\upnet2c.bat";
                if (File.Exists(FleLoad) && !File.Exists(FleTosave)) { File.Copy(FleLoad, FleTosave); }
                FleLoad = @"\Software Setup\pipop_tools\PRG\UDT_C.TXT";
                FleTosave = @"\_Cloud\" + cComName + @"\Backup\UDT_C.TXT";
                if (File.Exists(FleLoad) && !File.Exists(FleTosave)) { File.Copy(FleLoad, FleTosave); }
                FleLoad = @"\Software Setup\pipop_tools\PRG\UDT_C.FXP";
                FleTosave = @"\_Cloud\" + cComName + @"\Backup\UDT_C.FXP";
                if (File.Exists(FleLoad) && !File.Exists(FleTosave)) { File.Copy(FleLoad, FleTosave); }
                if (File.Exists(@"D:\BdPos\Util_Pok.exe"))
                {
                    Directory.SetCurrentDirectory(@"\_Cloud\" + cComName);
                    Process2Arg("D:\\BdPos\\Util_Pok.exe", "2 0 0");
                }
                if (Directory.Exists("D:\\BdPos"))
                {
                    string cCurrentDRV = Environment.CurrentDirectory.Substring(0, 3);
                    TextWriter BdPosTxt1 = new StreamWriter("D:\\BdPos\\RepoTxt.txt");
                    BdPosTxt1.WriteLine(cCurrentDRV + "_Cloud\\" + cComName + "\\Txt\\");
                    BdPosTxt1.Close();
                    TextWriter BdPosTxt2 = new StreamWriter("D:\\BdPos\\repoPOK.Txt");
                    BdPosTxt2.WriteLine(cCurrentDRV + "_Cloud\\" + cComName + "\\Backup\\");
                    BdPosTxt2.Close();
                    TextWriter BdPosTxt3 = new StreamWriter("D:\\BdPos\\Txt2Cloud.Txt");
                    BdPosTxt3.WriteLine(cCurrentDRV + "_Cloud\\" + cComName + "\\Backup\\");
                    BdPosTxt3.WriteLine(cCurrentDRV + "_Cloud\\" + cComName + "\\Pdf\\");
                    BdPosTxt3.WriteLine(cCurrentDRV + "_Cloud\\" + cComName + "\\Screen\\");
                    BdPosTxt3.WriteLine(cCurrentDRV + "_Cloud\\" + cComName + "\\Txt\\");
                    BdPosTxt3.Close();
                }
            }

            // ติดตั้งโปรแกรมที่ Download 
            if (pressYesNO("Install From EXE File"))
            {
                SetUpFromExe(G.cpDir + "ChromeSetup.exe", @"C:\Program Files\Google\Chrome\Application\chrome.exe");
                SetUpFromExe(G.cpDir + "AnyDesk.exe", @"C:\Program Files\AnyDesk\AnyDesk.exe");
                SetUpFromExe(G.cpDir + "ultraviewer_setup_6.2_en\\ultraviewer_setup_6.2_en.exe", @"C:\Program Files\UltraViewer\UltraViewer_Desktop.exe");
                SetUpFromExe(G.cpDir + "ar812lite_us.exe", @"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe");
                SetUpFromExe(G.cpDir + "wrar36b2.exe", @"C:\Program Files\WinRAR\WinRAR.exe");
                SetUpFromExe(G.cpDir + "FileZilla_3.13.0_win32-setup.exe", @"C:\Program Files\FileZilla FTP Client\filezilla.exe");
                SetUpFromExe(G.cpDir + "dopdf-7.exe", @"C:\Program Files\Softland\doPDF 7\dopdf.exe");
                SetUpFromExe(G.cpDir + "installbackupandsync.exe", @"C:\Program Files\Google\Drive\googledrivesync.exe");
                SetUpFromExe(G.cpDir + "pdfMachine.exe", @"C:\Windows\System32\spool\drivers\w32x86\3\bgsview.exe");
            }
        }


        static void HitDownload()
        {
            G.cpDir = @"\Software Setup\Uyod\";
            OpenFolder(0);
            DownloadFromWeb(0, 0, "setup/runtime4bd.exe", "runtime4bd.exe");
            DownloadFromWeb(0, 0, "setup/buddytools.exe", "buddytools.exe");
            UpdateDownload();
        }


        static void UpdateDownload()
        {
            G.cpDir = @"\Software Setup\Uyod\Update\";
            OpenFolder(0);
            DownloadFromWeb(0, 1, "setup/uyod-update.exe", "uyod-update.exe");
            DownloadFromWeb(0, 1, "zip/update.zip", "update.zip");
            DownloadFromWeb(0, 1, "zip/master.zip", "master.zip");
            DownloadFromWeb(0, 1, "zip/poswin.zip", "poswin.zip");
            DownloadFromWeb(0, 1, "zip/c.zip", "c.zip");
        }


        static void HotelDownload() 
        {
            G.cpDir = @"\Software Setup\Uyod\";
            DownloadFromWeb(0, 0, "setup/systemhosetup.exe", "systemhosetup.exe");
            DownloadFromWeb(0, 0, "setup/toolhosetup.exe", "toolhosetup.exe");
            DownloadFromWeb(0, 0, "setup/reporthosetup.exe", "reporthosetup.exe");
        }


        static void PosFoodDownload()
        {
            SetTimer();
            G.cpDir = @"\Software Setup\Uyod\";
            switch (G.cpFlag)
            {
                case "A":  // สำหรับ Amphan 
                    G.cpDir = @"\Software Setup\Uyod\Stock\";
                    DownloadFromWeb(0, 0, "setup/poswin-dhotel.exe", "poswin-dhotel.exe");
                    break;
                case "P": // สำหรับ POS
                    switch (G.cpText1)
                    {
                        case "B": DownloadFromWeb(0, 0, "setup/poswinsetup.exe", "poswinsetup.exe"); break;
                        case "S": DownloadFromWeb(0, 0, "setup/poswinsetup2.exe", "poswinsetup2.exe"); break;
                    }
                    break;
                case "F": // สำหรับร้านอาหาร
                    switch (G.cpText1)
                    {
                        case "B": DownloadFromWeb(0, 0, "setup/foodwinsetup.exe", "foodwinsetup.exe"); break;
                        case "S": DownloadFromWeb(0, 0, "setup/foodwinsetup2.exe", "foodwinsetup2.exe"); break;
                    }
                    break;
            }
            DownloadFromWeb(0, 0, "setup/systempossetup.exe", "systempossetup.exe");
            DownloadFromWeb(0, 0, "setup/toolpossetup.exe", "toolpossetup.exe");
            DownloadFromWeb(0, 0, "setup/reportpossetup.exe", "reportpossetup.exe");
            G.cpDir = @"\Software Setup\Uyod\";
            FinishDownload();
        }



        static void AmphanDownload()
        {
            G.cpDir = @"\Software Setup\Uyod\SetupSelect\";
            DownloadFromWeb(0, 1, "setup/setup2room.exe", "setup2room.exe");
            if (G.cpText1 == "") { G.cpText1 = "A"; }
            switch (G.cpText1)
            {
                case ("A"):
                    DownloadFromWeb(0, 0, "setup/beer2hd.exe", "beer2hd.exe");
                    DownloadFromWeb(0, 0, "setup/hdsetup.exe", "hdsetup.exe");
                    break;
                case ("B"):
                    DownloadFromWeb(0, 0, "setup/beer2dm.exe", "beer2dm.exe");
                    DownloadFromWeb(0, 0, "setup/dmsetup.exe", "dmsetup.exe");
                    break;
                case ("C"):
                    DownloadFromWeb(0, 0, "setup/beer2ho.exe", "beer2ho.exe");
                    DownloadFromWeb(0, 0, "setup/hosetup.exe", "hosetup.exe");
                    break;
            }
        }


        private static void PreparVar()
        {
            // ให้โปรแกรมทำงานที่ Drive E: เป็นหลัก 
            if (Directory.Exists(@"E:\")) { Directory.SetCurrentDirectory(@"E:\"); Console.Title="Drive E"; }
            else { if (Directory.Exists(@"V:\")) { Directory.SetCurrentDirectory(@"V:\"); Console.Title = "Drive V"; }
            else { if (Directory.Exists(@"D:\")) { Directory.SetCurrentDirectory(@"D:\"); Console.Title = "Drive D"; } } }
            G.cpDir = @"\_Cloud\";
            if (!Directory.Exists(G.cpDir)) { Directory.CreateDirectory(G.cpDir); }
            if (Directory.Exists(@"D:\") && !Directory.Exists(@"D:\BdPos")) { Directory.CreateDirectory(@"D:\BdPos"); }
            if (Directory.Exists(@"D:\") && !Directory.Exists(@"D:\BdPosVFP")) { Directory.CreateDirectory(@"D:\BdPosVFP"); }
            if (Directory.Exists(@"C:\") && !Directory.Exists(@"C:\Temp")) { Directory.CreateDirectory(@"C:\Temp"); }
            // เปลี่ยนตำแหน่งสำหรับ Download 
            if (!Directory.Exists(@"\Hardware Setup")) { Directory.CreateDirectory(@"\Hardware Setup"); }
            if (!Directory.Exists(@"\Hardware Setup\Printer")) { Directory.CreateDirectory(@"\Hardware Setup\Printer"); }
            if (!Directory.Exists(@"\Hardware Setup\Mainboard")) { Directory.CreateDirectory(@"\Hardware Setup\Mainboard"); }
            if (!Directory.Exists(@"\Hardware Setup\Card")) { Directory.CreateDirectory(@"\Hardware Setup\Card"); }
            if (!Directory.Exists(@"\Hardware Setup\Software")) { Directory.CreateDirectory(@"\Hardware Setup\Software"); }
            if (!Directory.Exists(@"\Hardware Setup\Other")) { Directory.CreateDirectory(@"\Hardware Setup\Other"); }
            if (!Directory.Exists(@"\Windows Setup")) { Directory.CreateDirectory(@"\Windows Setup"); }
            if (!Directory.Exists(@"\Windows Setup\Win7")) { Directory.CreateDirectory(@"\Windows Setup\Win7"); }
            if (!Directory.Exists(@"\Windows Setup\Win8")) { Directory.CreateDirectory(@"\Windows Setup\Win8"); }
            if (!Directory.Exists(@"\Windows Setup\Win10")) { Directory.CreateDirectory(@"\Windows Setup\Win10"); }
            if (!Directory.Exists(@"\Software Setup\")) { Directory.CreateDirectory(@"\Software Setup\"); }
            if (!Directory.Exists(@"\Software Setup\Uyod\")) { Directory.CreateDirectory(@"\Software Setup\Uyod\"); }
            if (!Directory.Exists(@"\Software Setup\Uyod\Update\")) { Directory.CreateDirectory(@"\Software Setup\Uyod\Update\"); }
            // Win32Or64BIT(); // ตรวจสอบว่าเป็น วินโดร์ 32 หรือ 64 Bit 
        }


        private static void Win32Or64BIT() // ตรวจสอบว่าเป็น วินโดร์ 32 หรือ 64 Bit 
        {
            if (!Directory.Exists("C:\\Temp")) { return; }
            if (!System.IO.File.Exists("Dummy.txt"))
            {
                TextWriter WriteTxt = new StreamWriter("Dummy.txt");
                WriteTxt.WriteLine("32or64");
                WriteTxt.Close();
            }
            if (Environment.Is64BitOperatingSystem) // เป็นวินโดร์ 64 บิต 
            {
                if (!System.IO.File.Exists("C:\\Temp\\w64.bit")) { System.IO.File.Copy("Dummy.txt", "C:\\Temp\\w64.bit"); }
                if (System.IO.File.Exists("C:\\Temp\\w32.bit")) { System.IO.File.Delete("C:\\Temp\\w32.bit"); }
            }
            else // เป็นวินโดร์ 32 บิต 
            {
                if (!System.IO.File.Exists("C:\\Temp\\w32.bit")) { System.IO.File.Copy("Dummy.txt", "C:\\Temp\\w32.bit"); }
                if (System.IO.File.Exists("C:\\Temp\\w64.bit")) { System.IO.File.Delete("C:\\Temp\\w64.bit"); }
            }
        }


        private static void WindowsDownload()
        {
            G.cpDir = "C:\\users\\" + Environment.UserName + "\\Downloads\\";
            OpenFolder(0);
            string vWindows = "";
            switch (G.cpText1)
            {
                case ("1"):
                    vWindows = "https://drive.google.com/drive/folders/1tD6unbB5bUptffcdA4PgWBUT0NZGqpye";
                    vWindows = vWindows + " https://drive.google.com/drive/folders/1tvVSKEfuZb25TeLZ0fsFnfupoE_LaE5q";
                    break;
                default:
                    vWindows = "https://drive.google.com/drive/folders/11E4zgQRsnImGipW5gwp5seucNL36VpU0";
                    vWindows = vWindows + " https://drive.google.com/drive/folders/1a8CKkj0fe0a3vF7s8-LMWrcU0PlvPbYc";
                    break;
            }
            vWindows = vWindows + " https://docs.google.com/document/d/1ZTCbZOQ0pA_ID4OmAw-nB-Q3QtiCyTpun0r-77bM9JA/edit?usp=sharing";
            if (vWindows != "") { OpenWebSite(vWindows); }
        }


        private static void SetTimer() 
        {
            StartDate = DateTime.Now;
            aTimer = new System.Timers.Timer(2000);
            aTimer.Elapsed += OnTimedEvent;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
            cTemp = "Start Download " + " @" + StartDate.ToString("dd/MM/yy HH:mm:ss");
            Console.WriteLine(cTemp); 
            Console.WriteLine("");
            // เริ่มสร้าง download.txt
            TextWriter Print2Txt = new StreamWriter("download.txt");
            Print2Txt.WriteLine("WEB : uyod.net/webonpc.html");            
            Print2Txt.WriteLine("");            
			Print2Txt.WriteLine(cTemp);
            Print2Txt.Close();
        }

        private static void OnTimedEvent(Object source, ElapsedEventArgs e)  
        {
            if (G.lpDworking) { Console.WriteLine("-Download " + G.FileDownloadName + " @" + Util.Program.DTN(4)); }
            else { Console.WriteLine(G.txtafterload + " @" + Util.Program.DTN(4));  }

        }


        private static void FinishDownload() 
        {
            aTimer.Stop();
            aTimer.Dispose();
            Console.WriteLine("");
            cTemp = "Finish Download" + " @" + DateTime.Now.ToString("dd/MM/yy HH:mm:ss");
            Console.WriteLine(cTemp); WhileLoad(0,cTemp);
            TimeSpan ts = (DateTime.Now - StartDate);
            cTemp = "Calculate = " + ts.ToString(@"hh\:mm\:ss");
            Console.WriteLine(cTemp);WhileLoad(0,cTemp);
            Console.WriteLine(""); Console.WriteLine("");
            if (File.Exists("setup.txt") && !insource)
            {
                if (G.cpFlag!="Z")
                {
                    Console.WriteLine("Open setup.txt");
                    if (Util.Program.Pok_YesNO2("เปิดไฟล์ setup.txt คลิก YES"))
                    {
                        System.IO.FileInfo fileObj = new System.IO.FileInfo("setup.txt");
                        System.Diagnostics.Process.Start(fileObj.FullName);
                    }
                }
            }
        }

        private static void SetTimer2()
        {
            // StartDate = DateTime.Now;
            aTimer = new System.Timers.Timer(2000);
            aTimer.Elapsed += OnTimedEvent2;
            aTimer.AutoReset = true;
            aTimer.Enabled = true;
        }

        private static void OnTimedEvent2(Object source, ElapsedEventArgs e)
        {
            // Console.WriteLine("-Download " + G.FileDownloadName + " @" + DateTime.Now.ToString("dd/MM/yy HH:mm"));
        }


        private static void FinishDownload2()
        {
            aTimer.Stop();
            aTimer.Dispose();
            Console.WriteLine("");
            cTemp = "Finish Download" + " @" + DateTime.Now.ToString("dd/MM/yy HH:mm:ss");
            Console.WriteLine(cTemp); WhileLoad(0, cTemp);
            TimeSpan ts = (DateTime.Now - StartDate);
            cTemp = "Calculate = " + ts.ToString(@"hh\:mm\:ss");
            Console.WriteLine(cTemp); WhileLoad(0, cTemp);
            Console.WriteLine(""); Console.WriteLine("");
        }


        private static void SetUpFromExe(string fSetup, string fsuccess) 
        {
            if (File.Exists(fSetup) && !File.Exists(fsuccess) && !insource)
            {
                Console.WriteLine("Install  " + fSetup);
                Process.Start(fSetup);
                uYodTimeWait(30);
            }
        }


        private static void SetupAllExe()
        {
            string cTargetFolder = @"\Software Setup\Uyod\";
            double lFileHvSize;
            Console.WriteLine("Folder: " + cTargetFolder);
            if (!pressYesNO("Install Form Download Press Y")) { return; }
            DirectoryInfo dExe = new DirectoryInfo(cTargetFolder);
            FileInfo[] FilesEXE = dExe.GetFiles("*.exe", SearchOption.TopDirectoryOnly);
            foreach (FileInfo file4Exe in FilesEXE)
            {
                lFileHvSize = file4Exe.Length;
                Console.WriteLine("Install " + file4Exe.FullName + " Size: " + lFileHvSize.ToString("N0") + " Byte");
                if (lFileHvSize < 1000000) { Process.Start(file4Exe.FullName); }
                else { ExecuteAsAdmin(file4Exe.FullName); }
                uYodTimeWait(30);
            }
        }


        private static void ExecuteAsAdmin(string fileName)
        {
            Process proc = new Process();
            proc.StartInfo.FileName = fileName;
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.Verb = "runas";
            proc.Start();
        }



        static void UNRar(string cTargetFolder)
        {
            string cFolderForUR;
            if (!Directory.Exists(cTargetFolder)) { return; }
            DirectoryInfo dUNRar = new DirectoryInfo(cTargetFolder);
            FileInfo[] FilesUP = dUNRar.GetFiles("*.rar", SearchOption.TopDirectoryOnly);
            TextWriter Write2Txt = new StreamWriter("UnPack.bat");
            Write2Txt.WriteLine("CD" + cTargetFolder);
            foreach (FileInfo fileUNRar in FilesUP)
            {
                cFolderForUR = fileUNRar.FullName.Substring(0, fileUNRar.FullName.Length - 4);
                if (!Directory.Exists(cFolderForUR))
                {
                    Directory.CreateDirectory(cFolderForUR);
                    Write2Txt.WriteLine("UnRAR x " + fileUNRar.Name + " " + fileUNRar.Name.Substring(0, fileUNRar.Name.Length - 4));
                }
            }
            Write2Txt.WriteLine("Exit");
            Write2Txt.Close();
            File.Copy("UnPack.bat", cTargetFolder + "\\UnPack.bat", true);
            Process.Start("UnPack.bat");
        }



        static void RenameCrackFile(string cTargetFolder)
        {
            string cfRename;
            DirectoryInfo dUNRar = new DirectoryInfo(cTargetFolder);
            FileInfo[] FilesUP = dUNRar.GetFiles("*.ra_", SearchOption.TopDirectoryOnly);
            foreach (FileInfo fileUNRar in FilesUP)
            {
                cfRename = fileUNRar.FullName.Substring(0, fileUNRar.FullName.Length - 4) + ".rar";
                if (File.Exists(cfRename)) { File.Delete(fileUNRar.FullName); }
                else { File.Move(fileUNRar.FullName, cfRename); }
            }
        }


        static bool pressYesNO(string cSayText)
        {
            bool lYesno = false;
            Console.WriteLine("Press Y and Enter For: " + cSayText);
            string cYesno = Console.ReadLine();
            if (cYesno == null || cYesno == "") { cYesno = "N"; }
            cYesno = cYesno.ToUpper().Trim().Substring(0, 1);
            if (cYesno == "Y") { lYesno = true; } else { lYesno = false; }
            return lYesno;
        }

        static void CallInDOS()
        {
            if (!File.Exists("Uyodloadbat.bat"))
            {
                string cPath = Directory.GetCurrentDirectory().Trim().ToUpper();
                TextWriter Write2Txt = new StreamWriter("Uyodloadbat.bat");
                Write2Txt.WriteLine("prompt $G$G$G");
                Write2Txt.WriteLine("Cls");
                Write2Txt.WriteLine("Rem Sample");
                Write2Txt.WriteLine("Rem Uyodload 2  For Software And Tools");
                Write2Txt.WriteLine("Rem Uyodload B  For D:\\BdPos\\");
                Write2Txt.WriteLine("Rem Type Exit For Exit");
                Write2Txt.WriteLine("Cmd");
                Write2Txt.Close();
            }
        }



        static void ShowEnVironment()
        {
            Console.WriteLine("");
            foreach (System.Collections.DictionaryEntry env in Environment.GetEnvironmentVariables())
            {
                string name = (string)env.Key;
                string value = (string)env.Value;
                if (name.Substring(0, 1) == "U" || name.Substring(0, 1) == "H")
                { Console.WriteLine("{0} = {1}", name, value); }
            }
            Console.WriteLine("");
            var os = Environment.OSVersion;
            Console.Write(WindowVer(os.Version.Major, os.Version.Minor) + ":");
            if (Environment.Is64BitOperatingSystem) { Console.WriteLine(" 64 BITS"); }
            else { Console.WriteLine(" 32 BITS"); }
            Console.WriteLine("");
            Console.WriteLine("Current OS Information:");
            Console.WriteLine("Platform: {0:G}", os.Platform);
            Console.WriteLine("Version String: {0}", os.VersionString);
            Console.WriteLine("Version Information:");
            Console.WriteLine("   Major: {0}", os.Version.Major);
            Console.WriteLine("   Minor: {0}", os.Version.Minor);
            Console.WriteLine("Service Pack: '{0}'", os.ServicePack);
            Process2Arg("cmd.exe", "/C slmgr /xpr");
        }

        static string WindowVer(int nMajor, int nMinor)
        {
            string cWindowVer = "";
            bool isServer = false; // IsServerVersion()
            int nWindowApi = 0; // WindowsAPI.GetSystemMetrics(89)
            switch (nMajor)
            {
                case 3:
                    cWindowVer = "Windows NT 3.51";
                    break;
                case 4:
                    cWindowVer = "Windows NT 4.0";
                    break;
                case 5:
                    if (nMinor == 0)
                        cWindowVer = "Windows 2000";
                    else if (nMinor == 1)
                        cWindowVer = "Windows XP";
                    else
                    {
                        if (isServer)
                        {
                            if (nWindowApi == 0)
                                cWindowVer = "Windows Server 2003";
                            else
                                cWindowVer = "Windows Server 2003 R2";
                        }
                        else
                            cWindowVer = "Windows XP";
                    }
                    break;
                case 6:
                    if (nMinor == 0)
                    {
                        if (isServer)
                            cWindowVer = "Windows Server 2008";
                        else
                            cWindowVer = "Windows Vista";
                    }
                    else if (nMinor == 1)
                    {
                        if (isServer)
                            cWindowVer = "Windows Server 2008 R2";
                        else
                            cWindowVer = "Windows 7";
                    }
                    else if (nMinor == 2)
                        cWindowVer = "Windows 8";
                    else
                    {
                        if (isServer)
                            cWindowVer = "Windows Server 2012 R2";
                        else
                            cWindowVer = "Windows 8.1";
                    }
                    break;
            }
            return cWindowVer;
        }


        static void CheckEnVironment()
        {
            string Prog4Open;
            Prog4Open = "msconfig"; if (pressYesNO("Run Msconfig")) { Process.Start(Prog4Open); }
            Prog4Open = "Taskmgr"; if (pressYesNO("Run Task Manager")) { Process.Start(Prog4Open); }
            Prog4Open = "dxdiag"; if (pressYesNO("Run " + Prog4Open)) { Process.Start(Prog4Open); }
            Prog4Open = "msinfo32"; if (pressYesNO("Run " + Prog4Open)) { Process.Start(Prog4Open); }
            Prog4Open = "regedit"; if (pressYesNO("Run " + Prog4Open)) { Process.Start(Prog4Open); }
            Prog4Open = "services.msc"; if (pressYesNO("Run " + Prog4Open)) { Process.Start(Prog4Open); }
        }



        static void BdPosInstall()
        {
            SetTimer();
            G.cpDir = @"\Software Setup\BdPosSetup\";
            OpenFolder(0);
            DownloadFromWeb(0,0, "rar/bdpos.rar", "bdpos.rar");
            DownloadFromWeb(0,0, "setup/uyod-update.exe", "uyod-update.exe");
            DownloadFromWeb(0,0, "source/HO.ZP","HO.ZP");
            FinishDownload();
        }


		static void BdPosVFPInstall()  
        {
            SetTimer();
            G.cpDir = @"\Software Setup\BdPosVFPSetup\";
            OpenFolder(0);
            DownloadFromWeb(0, 0, "rar/bdposvfp.rar", "bdposvfp.rar");
            DownloadFromWeb(0, 0, "setup/bdposvfp-update.exe", "bdposvfp-update.exe");
            DownloadFromWeb(0, 0, "source/ORIGINAL/VFP6.rar", "VFP6.rar");
            DownloadFromWeb(0, 0, "source/HOV.ZP","HOV.ZP");
            DownloadFromWeb(0, 0, "rar/BdPosVFP_9.rar", "BdPosVFP_9.rar");
            FinishDownload();
        }


        static void BdPos4Register()  // ลงทะเบียนโปรแกรมชุด Bdpos 
        {
            SetTimer();
            G.cpDir = @"\Software Setup\BdPos4Register\";
            OpenFolder(0);
            DownloadFromWeb(0, 0, "exe/UnRAR.exe", "UnRAR.exe");
            DownloadFromWeb(0, 0, "rar/bdpos_register.rar", "bdpos_register.rar");
            Directory.SetCurrentDirectory(G.cpDir);
            if (!File.Exists("HOMUSE.DBF")) { Util.Program.Pok_Process("unrar", "x -o+ bdpos_register "); }
            FinishDownload();
            Util.Program.Pok_Mess("โพลเดอร์ " + G.cpDir);
        }



        static void uYodTimeWait(int ms) 
        {
            Console.WriteLine("Wait .....");
            ms = ms *1000;
            DateTime start = DateTime.Now;
            while ((DateTime.Now - start).TotalMilliseconds < ms)
                Application.DoEvents();
        }


        static void WhileLoad(int nOption,string cText)
        {
            if (!File.Exists("download.txt")) { return; }
            using (StreamWriter Print2Txt = File.AppendText("download.txt"))	
			{
                if (nOption == 1) {cText = " ->" + cText; }
                if (nOption != 2) {cText = cText + " " + DateTime.Now.ToString("dd/MM HH:mm:ss"); }
                Print2Txt.WriteLine(cText);
            }
		}


        static void VFPupLoad()  
		{
            /* 
			UyodLoad Z 1  = Download bdposvfp_update.rar และ update
            UyodLoad Z 11  = Download bdposvfp_new_exe.rar และ update
			UyodLoad Z 2  = Zip ตัวupdate 
			UyodLoad Z 3  = ติดตั้ง D:\BdPosVFP 
			UyodLoad Z 4  = ติดตั้ง D:\SampleVFP
			UyodLoad Z 5  = ติดตั้ง D:\BdPosVFP  HIT
			UyodLoad Z 9  = ติดตั้ง โปรแกรมช่วยงาน
            */
            // หาโพลเดอร์ Download 
            string p = "VFPupLoad";
            string userRoot = System.Environment.GetEnvironmentVariable("USERPROFILE");
			string downloadFolder = Path.Combine(userRoot, "Downloads");
            if (lDebug) { Special.DEBUG.D(p, " -01 -"+downloadFolder); }
            if (Directory.Exists(downloadFolder)) { G.cpDir = downloadFolder + "\\"; }
            else { G.cpDir = "C:\\Temp\\"; }
			if (G.cpText1!="2")  
			{Console.WriteLine("Connect Web: uyod.net");Console.WriteLine("Open Folder "+G.cpDir);}
            if (lDebug) { Special.DEBUG.D(p, " -02"); }
            string cFolder,cFileZip;
			switch (G.cpText1)
			{
                case ("11"): 
                case ("1"):  // Download Update D:\BdPosVFP , D:\SampleVFP  
                    SetTimer();
                    switch (G.cpText1)
                    { case ("11"): cFileZip = "bdposvfp_new_exe.rar"; break;
                      default: cFileZip = "bdposvfp_update.rar";  break;
                    }
                    DownloadFromWeb(0,0, "rar/"+cFileZip,cFileZip); 
                    FinishDownload();
	    			cFileZip =G.cpDir+cFileZip;  // ระบุตำแหน่งจริง
                    Directory.SetCurrentDirectory(MyFolderApp); // กลับมาโพลเดอร์เดิม
                    if (File.Exists(cFileZip))
                     {
                        // สำรองไฟล์เดิมก่อน 
                        Layout.Program.LayOut.ScreenShow(8, "", "Download ", cFileZip, "สำเร็จแล้วครับ", "ถ้าใช้โปรแกรมอยู่ให้ออกก่อนครับ");
                        if (File.Exists("hovfp.exe")) { File.Move("hovfp.exe", "hovfp" +Util.Program.DTN(6)+".exe"); }
                        if (File.Exists("posvfp.exe")) { File.Move("posvfp.exe", "posvfp" + Util.Program.DTN(6) + ".exe"); }
                        Thread.Sleep(3 * 1000);  // 3 วินาที
                        Util.Program.Pok_Process("unrar", " x -o+ " + cFileZip + " "); 
                        Thread.Sleep(3 * 1000);  // 3 วินาที
                        File.Delete(cFileZip);  
                        Layout.Program.LayOut.ScreenShow(6, "ปรับปรุงโปรแกรม",cFileZip, "สำเร็จแล้วครับ", "ขอบคุณครับในการปรับปรุงโปรแกรม", "");
                    }
                    else { UyodMess("ไม่พบไฟล์ "+cFileZip+" ลองดาว์นโหลดใหม่" + Environment.NewLine + cFileZip); }
                    break;
                case ("2"): // Zip ตัวupdate   
				    cFolder=@"R:\DrvD\BdPosVFP\_update";
                    cFileZip = @"V:\_My Cloud\Box Sync\Rar";  // bdposvfp_update.zip
                    if (!Directory.Exists(cFileZip)) { Util.Program.Pok_Mess("ไม่พบโพลเดอร์ " + cFileZip);return; }
                    cFileZip =  cFileZip+ @"\bdposvfp_update.rar"; 
                    if (File.Exists(cFileZip)) {File.Delete(cFileZip); }
                    Directory.SetCurrentDirectory(cFolder);
                    Util.Program.Pok_Process("Rar", "u " + (char)34 + cFileZip + (char)34+" *.*");
                    Thread.Sleep(3 * 1000);  // 3 วินาที
                    Util.Program.FTPtoUyoddotnet(0,cFileZip, "rar", "bdposvfp_update.rar");
                    break;
			case ("3"): // ติดตั้ง D:\BdPosVFP   
                    if (lDebug) { Special.DEBUG.D(p, " -03 -BdPosVFP"); }
                    VFPsetup(1,@"D:\BdPosVFP","bdposvfp.rar","Uyod Team",104800000);  
                    break;
			case ("4"): // ติดตั้ง D:\SampleVFP 
                    if (lDebug) { Special.DEBUG.D(p, " -03 -SampleVFP"); }
                    VFPsetup(2,@"D:\SampleVFP","samplevfp.rar","DEMO", 110400000); 
				    break;
            case ("5"): // ติดตั้ง D:\BdPosVFP เวอร์ชั่นคุมไฟ  
                    if (lDebug) { Special.DEBUG.D(p, " -03 -BdPosVFP_HIT"); }
                    VFPsetup(3,@"D:\BdPosVFP", "bdposvfp_hit.rar", "Uyod Team", 104800000);  
                    break;
           case ("9"): // ติดตั้งโปรแกรม แบบอัตโนมัติ
                    if (lDebug) { Special.DEBUG.D(p, " -03 -Tools "); }
                    VFPsetup(9, @"D:\SampleVFP", "", "",0);
                    break;
            }
            FontsInstall();  // ตรวจสอบ Font ที่ไม่มีบน Windows
            if (lDebug) { Special.DEBUG.D(p, " -04 -FINNISH"); }
        }


        static void VFPsetup(int nSoft,string cFSetup,string cFileRar, string cSubSortcut,double ApproxSize)   
		{
            string p = "VFPsetup";
            cFSetup = cFSetup.Trim();
            if (lDebug) { Special.DEBUG.D(p, " -01 -"+cFSetup); }
            if (!Util.Program.Pok_CheckDRV(cFSetup)) { Layout.Program.LayOut.ScreenShow(8, "ไม่พบ Drive D: ==> " + cFSetup, "ทำงานต่อไม่ได้", "", "", ""); return; }

            SetTimer();
            DownloadFromWeb(0, 0, "exe/UnRAR.exe", "UnRAR.exe");
            DownloadFromWeb(0, 0, "rar/pipop_tools.rar", "pipop_tools.rar");
            DownloadFromWeb(0, 0, "toolsetup/npp.7.4.2.Installer.exe", "npp.7.4.2.Installer.exe");
            DownloadFromWeb(0, 0, "rar/pdf_annotator.rar", "pdf_annotator.rar");
            DownloadFromWeb(0, 0, "toolsetup/UltraViewer_setup_6.2_en.exe", "UltraViewer_setup_6.2_en.exe");
            DownloadFromWeb(0, 0, "toolsetup/wrar36b2.exe", "wrar36b2.exe");
            Layout.Program.LayOut.ScreenShow(6, "Download โปรแกรมช่วยงาน", "", "", "คลิก Next เพื่อDownload ต่อไป", "");
            if (!File.Exists(cFSetup+@"\hovfp.exe"))
            {
                if (lDebug) { Special.DEBUG.D(p, " -02"); }
                if (lDebug) { Special.DEBUG.D(p, " -03 -"+nSoft.ToString().Trim()); }
                switch (nSoft)
                {
                    case (1): // bdposvfp.rar
                        DownloadFromWeb(0, 0, "rar/bdposvfp.rar", "bdposvfp.rar");  // ตัวโปรแกรม
                        DownloadFromWeb(0, 0, "rar/bdposvfp_shortcut.rar", "bdposvfp_shortcut.rar");
                        DownloadFromWeb(0, 1, "rar/bdposvfp_update.rar", "bdposvfp_update.rar");
                        DownloadFromWeb(0, 0, "rar/bdposvfp_reportupdate.rar", "bdposvfp_reportupdate.rar");
                        DownloadFromWeb(0, 0, "rar/c.rar", "c.rar");
                        break;
                    case (2): // samplevfp.rar
                        DownloadFromWeb(0, 0, "rar/samplevfp.rar", "samplevfp.rar");  // ตัวโปรแกรม
                        DownloadFromWeb(0, 0, "rar/samplevfp_shortcut.rar", "samplevfp_shortcut.rar");
                        DownloadFromWeb(0, 1, "rar/bdposvfp_update.rar", "bdposvfp_update.rar");
                        DownloadFromWeb(0, 0, "rar/bdposvfp_reportupdate.rar", "bdposvfp_reportupdate.rar");
                        DownloadFromWeb(0, 0, "rar/c.rar", "c.rar");
                        break; 
                    case (3): // bdposvfp_hit.rar
                        DownloadFromWeb(0, 0, "rar/bdposvfp_hit.rar", "bdposvfp_hit.rar");  
                        DownloadFromWeb(0, 1, "rar/bdposvfp_new_exe.rar", "bdposvfp_new_exe.rar");
                        DownloadFromWeb(0, 0, "rar/c_exe_v2.rar", "c_exe_v2.rar");
                        break; 
                }

                G.lpDworking = false; // สิ้นสุดการ Download 
                Console.WriteLine("Download In Folder = "+ System.IO.Directory.GetCurrentDirectory());
                Layout.Program.LayOut.ScreenShow(6, "Download โปรแกรมเรียบร้อยแล้วครับ", "", "", "คลิก Next เพื่อทำการติดตั้งระบบ", "");
            }
            else 
            {
                if (lDebug) { Special.DEBUG.D(p, " -04"); }
                Console.Beep();
                FinishDownload2();
                txtInYellow("Have File Setup " + cFSetup+" Is Ready"+Environment.NewLine+"Is Not Setup Again");
                Layout.Program.LayOut.ScreenShow(0, "", "โปรแกรมหลัก " + cFileRar, "ติดตั้งแล้วครับ", "**ไม่สามารถติดตั้งได้อีก", "");
            }
            // สร้าง Text File
            TextWriter txt = new StreamWriter("setup.txt");
            txt.WriteLine("DT : "+Util.Program.DTN(2));
            switch (nSoft)
            {
                case (1): 
                    txt.WriteLine(G.cpDir+ "bdposvfp.rar  ==> Extract D:");
                    txt.WriteLine(G.cpDir + "bdposvfp_update.rar  ==> Extract D:\\BdPosVFP");
                    txt.WriteLine(G.cpDir + "bdposvfp_reportupdate.rar   ==> Extract D:\\BdPosVFP");
                    txt.WriteLine(G.cpDir + "c.rar   ==> Extract D:\\BdPosVFP");
                    txt.WriteLine(G.cpDir + "bdposvfp_shortcut.rar  ==> Extract Desktop");
                    break;
                case (2):
                    txt.WriteLine(G.cpDir + "samplevfp.rar  ==> Extract D:");
                    txt.WriteLine(G.cpDir + "bdposvfp_update.rar  ==> Extract D:\\SampleVFP");
                    txt.WriteLine(G.cpDir + "bdposvfp_reportupdate.rar  ==> Extract D:\\SampleVFP");
                    txt.WriteLine(G.cpDir + "c.rar  ==> Extract D:\\SampleVFP");
                    txt.WriteLine(G.cpDir + "samplevfp_shortcut.rar  ==> Extract D:\\SampleVFP");
                    break;
                case (3):
                    txt.WriteLine(G.cpDir + "bdposvfp_hit.rar  ==> Extract D:");
                    txt.WriteLine(G.cpDir + "bdposvfp_new_exe.rar   ==> Extract D:\\BdPosVFP");
                    txt.WriteLine(G.cpDir + "c_exe_v2.rar   ==> Extract D:\\BdPosVFP");
                    txt.WriteLine("Copy Shortcut จาก D:\\BdPosVFP\\_Desktop ไปไว้หน้า Desktop");
                    break;
            }
            txt.WriteLine(""); txt.WriteLine("Software Utility");
            txt.WriteLine(G.cpDir + "npp.7.4.2.Installer.exe  ===>Setup");
            txt.WriteLine(G.cpDir + "UltraViewer_setup_6.2_en.exe  ===>Setup");
            txt.WriteLine(G.cpDir + "wrar36b2.exe  ===>Setup");
            txt.WriteLine(G.cpDir + "pdf_annotator.zip  ===>Extract And Setup");
            txt.WriteLine("      ");
            txt.Close(); txt.Dispose();

            // ติดตั้งโปรแกรมช่วยงานอื่นๆ อัตโนมัติ            
            G.txtafterload = "Setup Tools ";
            if (nSoft == 9)
            {
                Directory.SetCurrentDirectory(G.cpDir);
                if (!Directory.Exists("Pipop Tools") && File.Exists("pipop_tools.rar") ) { Process2Arg("UnRAR.exe", "x pipop_tools.rar"); }
                if (!Directory.Exists("PDF Annotator") && File.Exists("pdf_annotator.rar")) { Process2Arg("UnRAR.exe", "x pdf_annotator.rar"); }
                Layout.Program.LayOut.ScreenShow(6, "Download โปรแกรมช่วยงานแล้ว", "", "", "คลิก Next เพื่อติดตั้งต่อไป", "");
                SetUpFromExe("npp.7.4.2.Installer.exe", @"C:\Program Files (x86)\Notepad++\uninstall.exe");
                SetUpFromExe("UltraViewer_setup_6.2_en.exe", @"C:\Program Files (x86)\UltraViewer\UltraViewer_Desktop.exe");
                SetUpFromExe("wrar36b2.exe", @"C:\Program Files\WinRAR\Uninstall.exe");
                Directory.SetCurrentDirectory(G.cpDir+ "PDF Annotator");
                SetUpFromExe("PDFAnnotatorSetup.exe", @"C:\Program Files (x86)\PDF Annotator\PDFAnnotator.exe");
                Layout.Program.LayOut.ScreenShow(6, "ติดตั้งโปรแกรมช่วยงานแล้ว", "", "", "คลิก Next ทำงานต่อไป", "");
                Directory.SetCurrentDirectory(MyFolderApp);
            }
            G.txtafterload = "Software Download Complete";  
            if (lDebug) { Special.DEBUG.D(p, " -06"); }
            Console.Beep();
            if (lDebug) { Special.DEBUG.D(p, " -07"); }
            FinishDownload();
            txtInYellow("Download Complete");
            OpenFolder(1);
            Layout.Program.LayOut.ScreenShow(0, "", "Download และติดตั้งโปรแกรม", "ชุด " + cFileRar, "สำเร็จแล้วครับ", "ขอบคุณครับในการใช้โปรแกรม");
            if (lDebug) { Special.DEBUG.D(p, " -08 -FINNISH"); }
        }


        static void txtInYellow(string ctxt)
        {
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(ctxt);
            Console.ForegroundColor = ConsoleColor.White;
        }


        static void Waitsecond(int nwaitsecond)
        {
            Console.WriteLine("Wait ... " + nwaitsecond.ToString().Trim() + " second.");
            Thread.Sleep(nwaitsecond * 1000); // คอยเป็นวินาที
        }


        static void FontsInstall() // ตรวจสอบ Font ที่ไม่มีบน Windows
        {
            if (File.Exists(@"D:\BdPosVFP\Fonts\CORDIAUB.TTF") && !File.Exists(@"C:\Windows\Fonts\CORDIAUB.TTF"))
            { Util.Program.Pok_Mess("ให้ Copy Fonts "+Environment.NewLine+"จาก "+ @"D:\BdPosVFP\Fonts\"
            +Environment.NewLine+"ไปที่ "+ @"C:\Windows\Fonts\"); }
		}

        // Upload File
        static void FileUpLoad()
        {
            if (!File.Exists(G.cpText1)) { Util.Program.Pok_Mess("ไม่พบไฟล์ "+ G.cpText1); return; }
            bool lLoad = true;
            string cSubfix;
            FileInfo cFileLoad;
            cFileLoad = new FileInfo(G.cpText1);
            cSubfix = cFileLoad.Extension.Substring(1).ToLower();
            switch (cSubfix)
                {
                    case ("rar"):
                    case ("zip"): break;
                    case ("exe"): cSubfix = "setup"; break;
                    case ("html"): cSubfix = ""; break;
                    default: lLoad = false; break;
                }
            if (lLoad) { Util.Program.FTPtoUyoddotnet(0,cFileLoad.FullName, cSubfix, cFileLoad.Name); }
            else { Util.Program.Pok_Mess("รองรับนามสกุล rar zip exe html"); }
        }


        static void Test_Prog()  // PipopU
        {
            // G.cpText1 = "9";
            // VFPupLoad();
        }

        

        static void Main(string[] args)
        {
            if (MyApp.Substring(0,2)=="V:") { insource = true ; } // Run กับ Source  
            if (Directory.Exists("R:\\Temp")) { inharddisk = true; }  // Run กับ Harddisk   
            lDebug = Special.DEBUG.Cr_Debug(MyProg);
            string p = "Main";
            if (lDebug) { Special.DEBUG.D(p, " -01"); }
            G.cpDir = "";
            G.cpFlag = "";
            G.cpText1 = "";
            G.cpText2 = "";

            string cpTextActive = "XXX"; // SAMPLEVFP,BDPOSVFP,XXX  สำหรับการทำงานโดยอัตโนมัติ PipopYou 
            // UyodLoad Z 3 = ติดตั้ง D:\BdPosVFP    G.cpFlag = "Z"; G.cpText1 = "3"; G.cpText2 = "";
            // UyodLoad Z 4 = ติดตั้ง D:\SampleVFP  G.cpFlag = "Z"; G.cpText1 = "4"; G.cpText2 = "";
            if (lDebug) { Special.DEBUG.D(p, " -02 -"+cpTextActive); }
            switch (cpTextActive)
            {
                case ("BDPOSVFP"):  // ติดตั้งโปรแกรม Demo อัตโนมัติ 
                    G.cpFlag = "Z"; G.cpText1 = "3"; G.cpText2 = ""; break;
                case ("SAMPLEVFP"):  // ติดตั้งโปรแกรม Demo อัตโนมัติ 
                    G.cpFlag = "Z"; G.cpText1 = "4"; G.cpText2 = "";break;
                default:
                    if (args.Length == 0)
                    {
                        // พบไฟล์  bdposvfpsetup.txt เพือติดตั้งอัตโนมัติ
                        cTemp = "bdposvfpsetup.txt";
                        if (File.Exists(cTemp))  // bdposvfpsetup.txt
                        {
                            string[] FleTxt = File.ReadAllLines(cTemp);
                            string cLine0 = FleTxt[0].Trim();
                            G.cpFlag = cLine0.Substring(0, 1);
                            G.cpText1 = cLine0.Substring(1, 1);
                            G.cpText2 = cLine0.Substring(2);
                            Console.WriteLine("Read File : " + MyApp);
                            Console.WriteLine("Read File : " + cTemp);
                            Console.WriteLine("Option : " + G.cpFlag + " " + G.cpText1 + " " + G.cpText2);
                            if (pressYesNO("Install From Rar File")) { File.Delete(cTemp); }
                            else { return; } // bdposvfpsetup.txt					 
                        }
                        else //  args.Length == 0
                        {
                            if (insource) { G.cpFlag = "T"; } //  ทดลอง 
                            else { CallInDOS(); ReadMe(); return; }  // สร้างBatch File และ Readme
                        }
                    }
                    else
                    {
                        G.cpFlag = args[0]; G.cpText1 = "";
                        for (int i = 1; i < args.Length; i++)  //  เริ่มตั้งแต่ หลักที่ 2
                        { G.cpText1 = G.cpText1 + " " + args[i]; }
                        for (int i = 2; i < args.Length; i++)  //  เริ่มตั้งแต่ หลักที่ 3
                        { G.cpText2 = G.cpText2 + " " + args[i]; }
                    }
                    G.cpFlag = G.cpFlag.ToUpper().Trim();
                    G.cpText1 = G.cpText1.Trim();
                    G.cpText2 = G.cpText2.Trim();
                    break;
            }
            if (lDebug) { Special.DEBUG.D(p, " -03 -"+G.cpFlag); }
            PreparVar();
            switch (G.cpFlag)
            {
                case ("X"): return;
                case ("T"): Test_Prog(); break;
                case ("0"):
                case ("?"): CallInDOS(); ReadMe(); break;
                case ("S"): ShowEnVironment(); break; // สภาพแวดล้อม Windows
                case ("C"): CheckEnVironment(); break;// สภาพแวดล้อม Windows
                case ("1"): SourceDownload();break; // Download Source
                case ("2"): SoftwareToolDownload();break; // Download Software and Tools
                case ("A"): HitDownload(); AmphanDownload(); HotelDownload(); PosFoodDownload(); SetupAllExe();break;
                case ("B"): BdPosInstall();break; // สำหรับติดตั้ง D:\\BDPOS
                case ("U"): UpdateDownload();break; // ปรับปรุง D:\\BDPOS
                case ("P"): HitDownload();PosFoodDownload();SetupAllExe();break;
                case ("F"): HitDownload();PosFoodDownload();SetupAllExe();break;
				case ("V"): BdPosVFPInstall();break; // สำหรับติดตั้ง D:\\BdposVFP
                case ("W"): WindowsDownload(); break; // Download Windows จาก G-Drive 
                case ("Z"): VFPupLoad(); break; // ปรับปรุงและติดตั้งไฟล์อัตโนมัติ  
                case ("Y"): FileUpLoad(); break; // Upload ไฟล์ขึ้นเวบ uyod.net 
                case ("R"): BdPos4Register(); break; // ลงทะเบียนโปรแกรมชุด Bdpos
            }
        }
    }
}

