#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA707_I34E_I34M
long F8049_(char* path)
{
        FILE* L2793_ps_;
        long L2794_l_;


        if ((L2793_ps_ = fopen(path, "r")) == 0) {
                return 0;
        }
        fseek(L2793_ps_, 0L, SEEK_END);
        L2794_l_ = ftell(L2793_ps_);
        fclose(L2793_ps_);
        return L2794_l_;
}

long F8050_(int16_t P3095_i_)
{
        struct dfree L2795_s_;


        getdfree(P3095_i_, &L2795_s_);
        return (long)L2795_s_.df_avail * L2795_s_.df_sclus * L2795_s_.df_bsec;
}

int16_t F8051_(char* P3096_pc_, char* P3097_pc_, char* P3098_pc_)
{
        FILE* L2796_ps_;
        FILE* L2797_ps_;
        char L2798_ac_[16384];
        char L2799_ac_[256];
        char L2800_ac_[256];
        int16_t L2801_i_;


        strcpy(L2799_ac_, P3097_pc_);
        if ((L2799_ac_ - 1)[strlen(L2799_ac_)] != '\\') {
                strcat(L2799_ac_, "\\");
        }
        strcat(L2799_ac_, P3098_pc_);
        strcpy(L2800_ac_, P3096_pc_);
        if ((L2800_ac_ - 1)[strlen(L2800_ac_)] != '\\') {
                strcat(L2800_ac_, "\\");
        }
        strcat(L2800_ac_, P3098_pc_);
        if ((L2796_ps_ = fopen(L2799_ac_, "rb")) == NULL) {
                return 1;
        }
        if ((L2797_ps_ = fopen(L2800_ac_, "wb")) == NULL) {
                fclose(L2796_ps_);
                return 1;
        }
        while ((L2801_i_ = fread(L2798_ac_, 1, 16384, L2796_ps_)) != 0) {
                if (fwrite(L2798_ac_, 1, L2801_i_, L2797_ps_) != L2801_i_) {
                        break;
                }
        }
        fclose(L2796_ps_);
        fclose(L2797_ps_);
        return 0;
}

int16_t F8052_DOS_KeyboardInput(
)
{
        char L2378_ac_[4];
        char* L2379_pc_;


        L2379_pc_ = L2378_ac_;
        L2378_ac_[0] = 2; /* Maximum size the DOS call can use in the specified L2379_pc_ buffer (1 character + the <enter> key) */
        asm     mov     ah, 0Ah
        asm     push    ds
        asm     lds     dx, L2379_pc_
        asm     int     21h             /* DOS - BUFFERED KEYBOARD INPUT */
                                        /* DS:DX -> buffer */
        asm     pop     ds
        if (L2378_ac_[1] != 1) { /* Length of the string returned by DOS in the buffer */
                return '\0';
        } else {
                return toupper(L2378_ac_[2]); /* Character returned by DOS */
        }
}

int16_t F8053_(int16_t P3099_i_)
{
        unsigned char L2804_uc_;


        asm     mov     ah, 1Ch
        asm     mov     dx, P3099_i_
        asm     xor     dh, dh
        asm     push    ds
        asm     int     21h             /* DOS - GET ALLOCATION TABLE INFORMATION FOR SPECIFIC DRIVE */
                                        /* DL = drive number to check (0=default, 1=A, etc) */
        asm     pop     ds
        asm     mov     L2804_uc_, al
        if (L2804_uc_ == 255) {
                return 1;
        } else {
                return 0;
        }
}

void F8054_DOS_PrintString(char* P2737_pc_)
{
        char* L2373_pc_;
        char L2374_ac_[80];
        int16_t L2375_i_;


        L2373_pc_ = L2374_ac_;
        for (L2375_i_ = 0; P2737_pc_[L2375_i_] != '\0'; L2375_i_++) {
                L2374_ac_[L2375_i_] = P2737_pc_[L2375_i_];
        }
        L2374_ac_[L2375_i_] = '$';
        asm     push    ds
        asm     lds     dx, L2373_pc_
        asm     mov     ah, 9
        asm     int     21h             /* DOS - PRINT STRING */
                                        /* DS:DX -> string terminated by "$" */
        asm     pop     ds
}

void F8055_(long P3101_l_)
{
        char L2808_ac_[20];


        F8054_DOS_PrintString(ltoa(P3101_l_, L2808_ac_, 10));
}

#ifdef MEDIA701_I34E
char* G8014_[14] = {
#endif
#ifdef MEDIA733_I34M
char* G8014_[16] = {
#endif
        "dm.exe",
        "swoosh",
        "ibmio",
        "vga",
        "ega",
        "tandy",
        "fires",
        "selector",
        "anim",
        "title",
        "end",
#ifdef MEDIA701_I34E
        "data\\dungeon.dat",
        "data\\graphics.dat",
        "data\\song.dat" };
#endif
#ifdef MEDIA733_I34M
        "eudata\\dungeon.dat",
        "eudata\\dungeonf.dat",
        "eudata\\dungeong.dat",
        "eudata\\graphics.dat",
        "eudata\\song.dat" };
#endif

void main(int16_t argc, char* argv[])
{
#ifdef MEDIA701_I34E
        int16_t L2809_i_ = 14;
#endif
#ifdef MEDIA733_I34M
        int16_t L2809_i_ = 16;
#endif
        int16_t L2810_i_;
        int16_t L2811_i_;
        int16_t L2812_i_;
        int16_t L2813_i_;
        unsigned long L2814_ul_;
        long L2815_l_;
        unsigned long L2816_ul_;
        long L2817_l_;
        long L2818_l_;
        char L2819_ac_[7] = "A:\\ftl";
#ifdef MEDIA701_I34E
        char L2820_ac_[12] = "A:\\ftl\\data";
#endif
#ifdef MEDIA733_I34M
        char L2820_ac_[14] = "A:\\ftl\\eudata";
#endif
        char L2821_ac_[4] = "'?'";
        char L2822_ac_[20];
        char L2823_ac_[246];
        int16_t L2824_i_;


        F8054_DOS_PrintString("\n\rDungeon Master Install Program, Version 1.0\n\r");
        if (argc < 2) {
                F8054_DOS_PrintString("\n\rType the letter of the drive to install to ");
                F8054_DOS_PrintString("(for example C, D, etc.): ");
                L2821_ac_[1] = L2813_i_ = F8052_DOS_KeyboardInput();
        } else {
                L2810_i_ = strlen(argv[1]);
                if ((L2810_i_ > 2) || ((L2810_i_ == 2) && (argv[1][1] != ':'))) {
                        F8054_DOS_PrintString("\n\rYou must specify a drive as either a single ");
                        F8054_DOS_PrintString("letter or a single letter\n\rfollowed by ':'");
                        exit(0);
                }
                L2821_ac_[1] = L2813_i_ = toupper(argv[1][0]);
        }
        if ((L2813_i_ < 'A') || (L2813_i_ > 'Z')) {
                F8054_DOS_PrintString("\n\r");
                F8054_DOS_PrintString(L2821_ac_);
                F8054_DOS_PrintString(" is not a valid letter for a drive.");
                exit(0);
        }
        L2812_i_ = L2813_i_ - 'A' + 1;
        if (F8053_(L2812_i_)) {
                F8054_DOS_PrintString("\n\rYour system does not have a drive ");
                F8054_DOS_PrintString(L2821_ac_);
                F8054_DOS_PrintString(".");
                exit(0);
        }
        L2811_i_ = getdisk() + 1;
        if (L2812_i_ == L2811_i_) {
                F8054_DOS_PrintString("\n\rYou must install to a different drive.");
                exit(0);
        }
        L2816_ul_ = 0;
        for (L2824_i_ = 0; L2824_i_ < L2809_i_; L2824_i_++) {
                L2816_ul_ += (L2815_l_ = F8049_(G8014_[L2824_i_]));
                if (L2815_l_ == 0) {
                        F8054_DOS_PrintString("\n\rCannot install.  File '");
                        F8054_DOS_PrintString(G8014_[L2824_i_]);
                        F8054_DOS_PrintString("' is missing from master disk.");
                        exit(0);
                }
        }
        L2816_ul_ += 10000;
        if (L2816_ul_ > (L2814_ul_ = F8050_(L2812_i_))) {
                F8054_DOS_PrintString("\n\rDrive ");
                F8054_DOS_PrintString(L2821_ac_);
                F8054_DOS_PrintString("does not have enough space available to install ");
                F8054_DOS_PrintString("Dungeon Master.\n\rYou will need ");
                F8055_(L2816_ul_ - L2814_ul_);
                F8054_DOS_PrintString(" more bytes.");
                exit(0);
        }
        getcwd(L2823_ac_, 255);
        L2819_ac_[0] = L2820_ac_[0] = L2812_i_ + 'a' - 1;
        F8054_DOS_PrintString("\n\r\n\rReady to install to '");
        F8054_DOS_PrintString(L2819_ac_);
        F8054_DOS_PrintString("'.  Installation will require ");
        F8055_(L2816_ul_);
        F8054_DOS_PrintString(" bytes (");
        L2817_l_ = (L2816_ul_ * 100) / L2814_ul_;
        if (L2817_l_ == 0) {
                F8054_DOS_PrintString("<1");
        } else {
                F8055_(L2817_l_);
        }
        F8054_DOS_PrintString("%) of\n\rthe remaining disk space and will leave ");
        L2818_l_ = (L2814_ul_ - L2816_ul_) >> 20;
        if (L2818_l_ == 0) {
                F8054_DOS_PrintString("<1");
        } else {
                F8054_DOS_PrintString("approx ");
                F8055_(L2818_l_);
        }
        F8054_DOS_PrintString(" Meg free.");
        F8054_DOS_PrintString("\n\rPress Y to install, N to cancel installation: ");
        if (F8052_DOS_KeyboardInput() == 'Y') {
                if (chdir(L2819_ac_)) {
                        if (mkdir(L2819_ac_)) {
                                F8054_DOS_PrintString("\n\rUnable to create directory '");
                                F8054_DOS_PrintString(L2819_ac_);
                                F8054_DOS_PrintString("'");
                                exit(0);
                        }
                }
                if (chdir(L2820_ac_)) {
                        if (mkdir(L2820_ac_)) {
                                F8054_DOS_PrintString("\n\rUnable to create subdirectory '");
                                F8054_DOS_PrintString(L2820_ac_);
                                F8054_DOS_PrintString("'");
                                exit(0);
                        }
                }
                chdir(L2823_ac_);
                F8054_DOS_PrintString("\n\r");
                for (L2824_i_ = 0; L2824_i_ < L2809_i_; L2824_i_++) {
                        F8054_DOS_PrintString("\r                                   \rcopying ");
                        F8054_DOS_PrintString(G8014_[L2824_i_]);
                        if (F8051_(L2819_ac_, L2823_ac_, G8014_[L2824_i_])) {
                                F8054_DOS_PrintString("\n\rError copying file '");
                                F8054_DOS_PrintString(G8014_[L2824_i_]);
                                F8054_DOS_PrintString("'");
                                exit(0);
                        }
                }
                F8054_DOS_PrintString("\r                                 ");
                setdisk(L2812_i_ - 1);
                chdir(L2819_ac_);
                F8054_DOS_PrintString("\n\rInstallation was successful.  ");
                F8054_DOS_PrintString("To play type the following commands:\n\r");
                F8054_DOS_PrintString("  ");
                L2822_ac_[0] = L2821_ac_[1];
                L2822_ac_[1] = '\0';
                F8054_DOS_PrintString(L2822_ac_);
                F8054_DOS_PrintString(":\n\r  cd \\ftl\n\r  dm");
        }
}
#endif
