/** FDISK.C     - Fixed Disk Partition Tool for NX-DOS
 ** ------------- (c) 1998-2005 Christopher Robert Evans
 ** -------------------------------------------------------------------------
 ** Portions from Master Class Assembler by WROX Press and Ralph Browns
 ** Interrupt List.
 ** SYS.C, the NX-DOS system files transfer tool, 3-7-2004 Turbo C 2.01
 ** -------------------------------------------------------------------------
 **/

#ifdef __TURBOC__
#define FAR far
#else
#define FAR _far
#endif

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <bios.h>
#include <mem.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

#include "\nxdos\src\toolbox.h"
#include "\nxdos\src\utils\fdisk\fdisk.h"

#define TITLE "Fixed Disk Partition Tool for NX-DOS - JUL-10-2006"
#define COPYR "(c) 1998-2005 DMSOFT; Christopher Robert Evans"
#define BOOT   "BOOT.BIN"
#define BOOTSECT 0
#define KERNEL "NXDOS.BIN"
#define KERNELSECT 1

void help(void) {
        printf("Copies the NX-DOS system files to specified disk.\n\n");
        printf("FDISK [drive1:]\n\n");
	printf("Drive1 specifies the drive the files are top be copied to.\n\n");
        printf("Configures a hard disk for use with NX-DOS use.\n\n");
        printf("FDISK [/STATUS] /X\n\n");
        printf("/STATUS   Displays partition information.\n");
        printf("/X        Ignores extended disk-access support. Use this switch if you\n");
        printf("          receive disk access or stack overflow messages.\n");
        printf("/MBR      Write the MBR to first harddrive. (Caution!)\n");
        printf("          Rewrites the Master Boot Record with new boot code.\n");
        printf("          I coded this to overwrite everything but the partition\n");
        printf("          table (last 64bytes) with NX-DOS MBR Loader.\n");
	printf("          WARNING: process will repair the bootstrap code and\n");
        printf("          the boot signature.\n\n");
}

int main(int argc, char *argv[]) {
int error,i,j;
int fixeddisk;
char ch;
/* ---------------- */
int size;
int drivenum;
int filesize;
char filename[85];
FILE *fp;
unsigned char buffer[65535];
char *s[127];
/* ---------------- */

    strcpy(s, argv[1]);
    if (strcmp(s,"/?")==0) { help(); } else
       {
    if (argc<2) {
        /* printf("Required parameter missing.\n"); */
        fixeddisk = 128;
        loadpartition(fixeddisk);
        save_mbr("MBR.BIN");
        while (ch!=27) {
                clrscr();
                printf(TITLE); printf("\n\r");
                printf(COPYR); printf("\n\r\n\r");
                printf("Current fixed disk drive: %d\n\r\n\r",(fixeddisk-127));
                display_partitions();
                printf("\n\r");
                printf("\t(C)reate a partition or logical DOS drive\n");
                printf("\t(S)et active partition\n");
                printf("\t(D)elete partition or Logical DOS drive\n");
                printf("\t(R)estore partitions\n");
                printf("\n\r\n\rChoose option: ");
                ch = getch();
                switch(ch) {
                        case 'C' : { changes=1;
                                     printf("Partition: ");
                                     scanf("%i", activepart);
                                     if (partition[activepart].pnSYSCODE == 0x00) {
                                        printf("/n/rPartition type:");
                                        scanf("%i", i);
                                        partition[activepart].pnSYSCODE == i;
                                        printf("/n/rPartition size in bytes:");
                                        scanf("%i", i);
                                        partition[activepart].pnSIZE == i;

                                        /* 4-6-2005 -CE needs to calculate start/end heads etc...  */

                                        } else {
                                        printf("Please, delete the partition first.\n\r");
                                        }
                                     break; }
                        case 'S' : {
                                     changes=1;
                                     partition[activepart].pnBOOT == 0x00;
                                     printf("Partition: ");
                                     scanf("%i", activepart);
                                     if (partition[i].pnSYSCODE != 0x00) {
                                        partition[activepart].pnBOOT == 0x80;
                                        } else {
					  highvideo();
					  printf("/n/rNot a valid partition.");
					  lowvideo();
					  getche();
					  }
                                     break; }
                        case 'D' : {
                                     changes=1;
                                     printf("Partition: ");
                                     scanf("%i", i);
                                     printf("/n/rAre you sure? [y/N]");
                                     ch = getche();
                                     if (toupper(ch)=='Y') {
                                     partition[i].pnSYSCODE=0x00; }
                                     break; }
                        case 'R' : {
                                     restore_mbr("MBR.BIN");
                                     extract_entries();
                                     changes=0;
                                     break; }
                           }
                       } /* while */
        if (changes!=0) {
                savepartition(fixeddisk);
                clrscr();
                printf("System will now restart if you press ENTER./n/rESC will exit back to DOS./n/r");
                ch = getche();
                if (ch==13) reboot(0x1234);
                }
        unlink("MBR.BIN");
        } else {
        strcpy(s, argv[1]);
        if (strcmp(s,"A:")==0) { drivenum==0; }
        if (strcmp(s,"B:")==0) { drivenum==1; }
        printf("Transfering system files to drive %c:",(drivenum+65));
        strcpy(filename, BOOT);
        if (fp = fopen(filename,"rb")!=NULL) {
           fread(&buffer, 512, 1, fp);
           fclose(fp);
           /* memcpy(buffer, bootblock, 512); */
           abswrite(drivenum,1,0,buffer);
           }
        strcpy(filename,KERNEL);
        if (fp = fopen(filename,"rb")!=NULL) {
           fseek(fp, 0, SEEK_END);
           filesize = ftell(fp);
           fseek(fp, 0, SEEK_SET);
           fread(&buffer, filesize, 1, buffer);
           fclose(fp);
           abswrite(drivenum,(filesize/512),1,buffer);
           }
        /*
                unsigned char bb[512];
                blocks *= 8;
                memcpy(bb,bootblock,512);
                bb[2] = (blocks & 0x00FF);
                bb[3] = (blocks & 0xFF00) >> 8;
                fwrite(bb,512,1,fp); }
        */
       } /* argc() */
    } /* help() */
return(error); }
