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 MyGlobal
        {
            public static string cpDir;
            public static string cpFlag;
            public static string cpText1;
            public static string cpText2;
            public static string FileDownloadName = "";
        }
        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;
        private static string MyFolderApp = System.IO.Directory.GetCurrentDirectory();  // โพลเดอร์ทำงาน

        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 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 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 (MyGlobal.cpDir == "") { return; }
            if (!Directory.Exists(MyGlobal.cpDir))
            {
                Directory.CreateDirectory(MyGlobal.cpDir);
                if (insource) { UyodMess("สร้างโพลเดอร์ " + MyGlobal.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 = MyGlobal.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();
            MyGlobal.FileDownloadName = fileName;
            cr_dir();
            fileName = MyGlobal.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
            {
                // nDup=1 เจอแล้ว Download ใหม่ 
                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
            {
                // Create a new WebClient instance.
                WebClient myWebClient = new WebClient();
                
                // Download the Web resource and save it into the current filesystem folder.
                if (insource) { UyodMess("Form " + myStringWebResource + " To " + @fileName); } 
                else { myWebClient.DownloadFile(myStringWebResource, @fileName); }
                myWebClient.Dispose();
				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();
            MyGlobal.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");
            MyGlobal.cpDir = @"\Source\Original\";
            DownloadFromWeb(0, 0, "http://uyod.net/source/ORIGINAL/ALLPOS.zip", "allpos.zip");
            FinishDownload();
        }



        static void SourceFLList(int nFlag, string cfFilename)
        {
            MyGlobal.FileDownloadName = cfFilename;
            string uYodweb, cFileCopy;
            uYodweb = "http://uyod.net/source/";
            cFileCopy = MyGlobal.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); }
            WebClient myWebClient = new WebClient();
            myWebClient.DownloadFile(uYodweb2, cFileCopy2);
            myWebClient.Dispose();
        }


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

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

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

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

            DownloadFromWeb(0, 0, "http://uyod.net/toolsetup/LineInst.exe", "LineInst.exe");
			
            UNRar(MyGlobal.cpDir);
            RenameCrackFile(MyGlobal.cpDir);

            FinishDownload();

            Directory.SetCurrentDirectory(MyGlobal.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(MyGlobal.cpDir + "ChromeSetup.exe", @"C:\Program Files\Google\Chrome\Application\chrome.exe");
                SetUpFromExe(MyGlobal.cpDir + "AnyDesk.exe", @"C:\Program Files\AnyDesk\AnyDesk.exe");
                SetUpFromExe(MyGlobal.cpDir + "ultraviewer_setup_6.2_en\\ultraviewer_setup_6.2_en.exe", @"C:\Program Files\UltraViewer\UltraViewer_Desktop.exe");
                SetUpFromExe(MyGlobal.cpDir + "ar812lite_us.exe", @"C:\Program Files\Adobe\Reader 8.0\Reader\AcroRd32.exe");
                SetUpFromExe(MyGlobal.cpDir + "wrar36b2.exe", @"C:\Program Files\WinRAR\WinRAR.exe");
                SetUpFromExe(MyGlobal.cpDir + "FileZilla_3.13.0_win32-setup.exe", @"C:\Program Files\FileZilla FTP Client\filezilla.exe");
                SetUpFromExe(MyGlobal.cpDir + "dopdf-7.exe", @"C:\Program Files\Softland\doPDF 7\dopdf.exe");
                SetUpFromExe(MyGlobal.cpDir + "installbackupandsync.exe", @"C:\Program Files\Google\Drive\googledrivesync.exe");
                SetUpFromExe(MyGlobal.cpDir + "pdfMachine.exe", @"C:\Windows\System32\spool\drivers\w32x86\3\bgsview.exe");
            }
        }


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


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


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


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



        static void AmphanDownload()
        {
            MyGlobal.cpDir = @"\Software Setup\Uyod\SetupSelect\";
            DownloadFromWeb(0, 1, "http://uyod.net/setup/setup2room.exe", "setup2room.exe");
            if (MyGlobal.cpText1 == "") { MyGlobal.cpText1 = "A"; }
            switch (MyGlobal.cpText1)
            {
                case ("A"):
                    DownloadFromWeb(0, 0, "http://uyod.net/setup/beer2hd.exe", "beer2hd.exe");
                    DownloadFromWeb(0, 0, "http://uyod.net/setup/hdsetup.exe", "hdsetup.exe");
                    break;
                case ("B"):
                    DownloadFromWeb(0, 0, "http://uyod.net/setup/beer2dm.exe", "beer2dm.exe");
                    DownloadFromWeb(0, 0, "http://uyod.net/setup/dmsetup.exe", "dmsetup.exe");
                    break;
                case ("C"):
                    DownloadFromWeb(0, 0, "http://uyod.net/setup/beer2ho.exe", "beer2ho.exe");
                    DownloadFromWeb(0, 0, "http://uyod.net/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"; } } }
            MyGlobal.cpDir = @"\_Cloud\";
            if (!Directory.Exists(MyGlobal.cpDir)) { Directory.CreateDirectory(MyGlobal.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"); }
            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()
        {
            MyGlobal.cpDir = "C:\\users\\" + Environment.UserName + "\\Downloads\\";
            OpenFolder(0);
            string vWindows = "";
            switch (MyGlobal.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)
        {
            Console.WriteLine("-Download " + MyGlobal.FileDownloadName + " @" + DateTime.Now.ToString("dd/MM/yy HH:mm"));
        }


        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("download.txt") && !insource)
            {
                if (MyGlobal.cpFlag!="Z")
                {
                    Console.WriteLine("Open download.txt");
                    Console.ReadLine();
                    System.IO.FileInfo fileObj = new System.IO.FileInfo("download.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 " + MyGlobal.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();
            MyGlobal.cpDir = @"\Software Setup\BdPosSetup\";
            OpenFolder(0);
            DownloadFromWeb(0,0, "http://uyod.net/rar/bdpos.rar", "bdpos.rar");
            DownloadFromWeb(0,0, "http://uyod.net/setup/uyod-update.exe", "uyod-update.exe");
            DownloadFromWeb(0,0, "http://uyod.net/source/HO.ZP","HO.ZP");
            FinishDownload();
        }


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


        static void BdPos4Register()  // ลงทะเบียนโปรแกรมชุด Bdpos 
        {
            SetTimer();
            MyGlobal.cpDir = @"\Software Setup\BdPos4Register\";
            OpenFolder(0);
            DownloadFromWeb(0, 0, "http://uyod.net/exe/UnRAR.exe", "UnRAR.exe");
            DownloadFromWeb(0, 0, "http://uyod.net/rar/bdpos_register.rar", "bdpos_register.rar");
            Directory.SetCurrentDirectory(MyGlobal.cpDir);
            if (!File.Exists("HOMUSE.DBF")) { Util.Program.Pok_Process("unrar", "x -o+ bdpos_register "); }
            FinishDownload();
            Util.Program.Pok_Mess("โพลเดอร์ " + MyGlobal.cpDir);
            /* พบปัญหาในการหาไฟล์ใน Folder ที่เป็น Hidden  PipopU
            SetTimer2();
            DirectoryInfo MainFolder;
            FileInfo[] MainFile;
            MainFolder = new DirectoryInfo(MyGlobal.cpDir);
            MainFile = MainFolder.GetFiles("*.dbf", SearchOption.TopDirectoryOnly);
            string txt2bat = MyFolderApp+"\\dir4muse.bat";
            if (!File.Exists(txt2bat)) 
            {
                TextWriter dir4muse = new StreamWriter(txt2bat);  
                foreach (FileInfo listMainFile in MainFile)
                {
                    dir4muse.WriteLine("REM " + listMainFile.FullName);
                }
                dir4muse.Close();dir4muse.Dispose();
            }
            Util.Program.Pok_Process("cmd.exe", "/c " + txt2bat);
            FinishDownload2();  */
        }



        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 Update D:\BdPosVFP , D:\SampleVFP
			UyodLoad Z 2  = Zip ตัวupdate 
			UyodLoad Z 3  = ติดตั้ง D:\BdPosVFP 
			UyodLoad Z 4  = ติดตั้ง D:\SampleVFP
			*/
			// หาโพลเดอร์ Download 
			string userRoot = System.Environment.GetEnvironmentVariable("USERPROFILE");
			string downloadFolder = Path.Combine(userRoot, "Downloads");
            if (Directory.Exists(downloadFolder)) { MyGlobal.cpDir = downloadFolder + "\\"; }
            else { MyGlobal.cpDir = "C:\\Temp\\"; }
            
			if (MyGlobal.cpText1!="2") 
			{Console.WriteLine("Connect Web: uyod.net");Console.WriteLine("Open Folder "+MyGlobal.cpDir);}
			
			string cFolder,cFileZip;
			switch (MyGlobal.cpText1)
			{
			case ("1"):  // Download Update D:\BdPosVFP , D:\SampleVFP  
                SetTimer();
                cFileZip = "bdposvfp_update.rar";  
                DownloadFromWeb(0,1, "http://uyod.net/rar/bdposvfp_update.rar",cFileZip);
                DownloadFromWeb(0,1, "http://uyod.net/rar/bdposvfp_reportupdate.rar", "bdposvfp_reportupdate.rar"); // รอการปรับแล้วแต่เคส
                FinishDownload();
				cFileZip =MyGlobal.cpDir+cFileZip;
                if (File.Exists(cFileZip))
                 {
                        Thread.Sleep(3 * 1000);  // 3 วินาที
                        Util.Program.Pok_Process("unrar", "x -o+ " + cFileZip + " "); 
                        Thread.Sleep(3 * 1000);  // 3 วินาที
                        File.Delete(cFileZip);
                    }
                    else { UyodMess("ไม่พบไฟล์ bdposvfp_update.rar ลองดาว์นโหลดใหม่" + 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(cFileZip, "rar", "bdposvfp_update.rar");
                break;
			case ("3"): // ติดตั้ง D:\BdPosVFP  
                VFPsetup(@"D:\BdPosVFP","bdposvfp.rar","Uyod Team",104800000); 
                break;
			case ("4"): // ติดตั้ง D:\SampleVFP  Pipop
                VFPsetup(@"D:\SampleVFP","samplevfp.rar","DEMO", 110400000);
				break;
			}
			FontsInstall();  // ตรวจสอบ Font ที่ไม่มีบน Windows
        }

        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 VFPsetup(string cFSetup,string cFileRar, string cSubSortcut,double ApproxSize)   
		{
            // VFPsetup(@"D:\BdPosVFP","bdposvfp.rar","Uyod Team"); 
            cFSetup = cFSetup.Trim();
            if (!Util.Program.Pok_CheckDRV(cFSetup)) { UyodMess("ไม่พบ "+cFSetup+" ทำงานต่อไม่ได้");return; }
            SetTimer();
            if (!Directory.Exists(cFSetup))
            {
                DownloadFromWeb(0, 0, "http://uyod.net/exe/UnRAR.exe", "UnRAR.exe");
                DownloadFromWeb(0, 1, "http://uyod.net/rar/c.rar", "c.rar");
                DownloadFromWeb(0, 1, "http://uyod.net/rar/bdposvfp_update.rar", "bdposvfp_update.rar");
                DownloadFromWeb(0, 1, "http://uyod.net/rar/bdposvfp_reportupdate.rar", "bdposvfp_reportupdate.rar");
                DownloadFromWeb(0, 1, "http://uyod.net/rar/" + cFileRar, cFileRar);  // PipopU
                // รอไฟล์ Download เนื่องจากมีขนาดใหญ่
                do
                {
                    Console.WriteLine(cFileRar+" --> "+ Util.Program.CalFileSize(cFileRar)+" Byte.");
                } while (Util.Program.CalFileSize(cFileRar) > ApproxSize);
                Waitsecond(30);
                Directory.SetCurrentDirectory(MyGlobal.cpDir);
                if (File.Exists(cFileRar)) { Process2Arg("UnRAR.exe", "x " + cFileRar + " D:\\"); }
                Waitsecond(120); // 120 วินาที ต้องรอเผื่อ Unrar ไม่ทัน 
                if (File.Exists("c.rar") && Directory.Exists(cFSetup)) { Process2Arg("UnRAR.exe", "x -o+ c " + cFSetup); }
                Waitsecond(30); // 30 วินาที ต้องรอเผื่อ Unrar ไม่ทัน  
                if (File.Exists("bdposvfp_update.rar") && Directory.Exists(cFSetup)) { Process2Arg("UnRAR.exe", "x -o+ bdposvfp_update " + cFSetup); }
                Waitsecond(30); // 30 วินาที ต้องรอเผื่อ Unrar ไม่ทัน  
                if (File.Exists("bdposvfp_reportupdate.rar") && Directory.Exists(cFSetup)) { Process2Arg("UnRAR.exe", "x -o+ bdposvfp_reportupdate " + cFSetup); }
                Waitsecond(30); // 30 วินาที ต้องรอเผื่อ Unrar ไม่ทัน  
            }
            else
            {
                Console.Beep();
                FinishDownload2();
                txtInYellow("Have File Setup " + cFSetup+" Is Ready"+Environment.NewLine+"Is Not Setup Again");
                Layout.Program.LayOut.ShowMessage(0, "", "โปรแกรม " + cFileRar, "ติดตั้งแล้วครับ", "**ไม่สามารถติดตั้งได้อีก", "ขอบคุณครับในการใช้โปรแกรม"); 
                return;
            }

            // ตรวจสอบว่า สร้างสำเร็จหรือไม่ 
            if (Directory.Exists(cFSetup)) { Console.WriteLine("Create Folder " + cFSetup + " Complete"); }
            else { txtInYellow("Error : Don't Create Folder " + cFSetup + " Please Setup Again"); return;}
            Waitsecond(10); // 10 วินาที 

            //  Console.WriteLine("Press Any Key For Create ICON");Console.Read();
            try 
			{
                // สร้าง Short-Cut โปรแกรม 
                if (Directory.Exists(cFSetup+ "\\_desktop"))
                {
                    cTemp = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\"+cSubSortcut;
                    if (!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp);}
                    DirectoryInfo cFolderIcon = new DirectoryInfo(cFSetup + "\\_desktop");
                    FileInfo[] FilesPack = cFolderIcon.GetFiles("*.*", SearchOption.TopDirectoryOnly);
                    foreach (FileInfo cFileIcon in FilesPack)
                    {File.Copy(cFileIcon.FullName, cTemp + "\\" + cFileIcon, true); }
                }
                // สร้าง Short-Cut คู่มือ
                if (Directory.Exists(cFSetup + "\\_desktop\\Manaul"))
                {
                    cTemp = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"\\"+ cSubSortcut+"\\คู่มือ\\";
                    if (!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp); }

                    DirectoryInfo cFolderIconM = new DirectoryInfo(cFSetup + "\\_desktop\\Manaul");
                    FileInfo[] FilesPackM = cFolderIconM.GetFiles("*.*", SearchOption.TopDirectoryOnly);
                    foreach (FileInfo cFileIconM in FilesPackM)
                    { File.Copy(cFileIconM.FullName, cTemp + "\\" + cFileIconM, true); }
                }
                // สร้าง Short-Cut ช่วยงาน
                if (Directory.Exists(cFSetup + "\\_desktop\\Help"))
                {
                    cTemp = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + cSubSortcut + "\\ช่วยงาน\\";
                    if (!Directory.Exists(cTemp)) { Directory.CreateDirectory(cTemp); }

                    DirectoryInfo cFolderIconM = new DirectoryInfo(cFSetup + "\\_desktop\\Help");
                    FileInfo[] FilesPackM = cFolderIconM.GetFiles("*.*", SearchOption.TopDirectoryOnly);
                    foreach (FileInfo cFileIconM in FilesPackM)
                    { File.Copy(cFileIconM.FullName, cTemp + "\\" + cFileIconM, true); }
                }
            }
            catch (Exception e) {UyodMess("พบปัญหาในการติดตั้ง "+e.Message);return;}
            Console.Beep();
            FinishDownload();
            txtInYellow("Finnish");
            if (!Directory.Exists(cFSetup + "\\_desktop")) // ตรวจสอบอีกรอบ
            { UyodMess("ถ้าไม่มีไอคอนในการทำงาน ให้คลิกไฟล์สำหรับติดตั้งอีกครั้ง"); }
            else { MyGlobal.cpDir = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + cSubSortcut; }
            OpenFolder(1);
            Layout.Program.LayOut.ShowMessage(0, "", "Download และติดตั้งโปรแกรม", "ชุด "+cFileRar, "สำเร็จแล้วครับ", "ขอบคุณครับในการใช้โปรแกรม");
        }

		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(MyGlobal.cpText1)) { Util.Program.Pok_Mess("ไม่พบไฟล์ "+ MyGlobal.cpText1); return; }
            bool lLoad = true;
            string cSubfix;
            FileInfo cFileLoad;
            cFileLoad = new FileInfo(MyGlobal.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(cFileLoad.FullName, cSubfix, cFileLoad.Name); }
            else { Util.Program.Pok_Mess("รองรับนามสกุล rar zip exe html"); }
        }


        static void Test_Prog() 
        {
            // public static void ShowMessage(int nOption, string ctext1, string ctext2, string ctext3, string ctext4, string ctext5)
            // string cFileRar = "samplevfp";
            MyGlobal.cpText1 = "4";
            // VFPupLoad(); 
            // Console.WriteLine("ทดลองโปรแกรม"); Console.ReadLine();
            // MyGlobal.cpText1 = @"R:\Temp\test.html5";
            // FileUpLoad();
            // BdPos4Register();
        }


        static void Main(string[] args)
        {
            if (MyApp.Substring(0,2)=="V:") { insource = true ; } // Run กับ Source  
            if (Directory.Exists("R:\\Temp")) { inharddisk = true; }  // Run กับ Harddisk   
            MyGlobal.cpDir = "";
            MyGlobal.cpFlag = "";
            MyGlobal.cpText1 = "";
            MyGlobal.cpText2 = "";
            string cpTextActive = ""; //  สำหรับการทำงานโดยอัตโนมัติ PipopU 
            // UyodLoad Z 3 = ติดตั้ง D:\BdPosVFP    MyGlobal.cpFlag = "Z"; MyGlobal.cpText1 = "3"; MyGlobal.cpText2 = "";
            // UyodLoad Z 4 = ติดตั้ง D:\SampleVFP  MyGlobal.cpFlag = "Z"; MyGlobal.cpText1 = "4"; MyGlobal.cpText2 = "";
            if (cpTextActive !="") { MyGlobal.cpFlag = "Z"; MyGlobal.cpText1 = "4"; MyGlobal.cpText2 = ""; }  // สำหรับทำงานอัตโนมัติ 
            else
            {
                //  cpTextActive=""
                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();
                        MyGlobal.cpFlag = cLine0.Substring(0, 1);
                        MyGlobal.cpText1 = cLine0.Substring(1, 1);
                        MyGlobal.cpText2 = cLine0.Substring(2);
                        Console.WriteLine("Read File : " + MyApp);
                        Console.WriteLine("Read File : " + cTemp);
                        Console.WriteLine("Option : " + MyGlobal.cpFlag + " " + MyGlobal.cpText1 + " " + MyGlobal.cpText2);
                        if (pressYesNO("Install From Rar File")) { File.Delete(cTemp); }
                        else { return; } // bdposvfpsetup.txt					 
                    }
                    else //  args.Length == 0
                    {
                        if (insource) { MyGlobal.cpFlag = "T"; } // PipopU  ทดลอง 
                        else { CallInDOS(); ReadMe(); return; }  // สร้างBatch File และ Readme
                    }
                }
                else
                {
                    MyGlobal.cpFlag = args[0]; MyGlobal.cpText1 = "";
                    for (int i = 1; i < args.Length; i++)  //  เริ่มตั้งแต่ หลักที่ 2
                    { MyGlobal.cpText1 = MyGlobal.cpText1 + " " + args[i]; }
                    for (int i = 2; i < args.Length; i++)  //  เริ่มตั้งแต่ หลักที่ 3
                    { MyGlobal.cpText2 = MyGlobal.cpText2 + " " + args[i]; }
                }
                MyGlobal.cpFlag = MyGlobal.cpFlag.ToUpper().Trim();
                MyGlobal.cpText1 = MyGlobal.cpText1.Trim();
                MyGlobal.cpText2 = MyGlobal.cpText2.Trim();
                // cpTextActive=""
            }
            PreparVar();
            switch (MyGlobal.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
            }
        }
    }
}

