#include <io.h>
#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char * argv[]) {
FILE *out;

     randomize();
     if (argc<1) {
	printf("SRCDROP <programname> and no extension \n");
	printf("	Drops a modified programname.asm into current direnctory.\n");
	printf("	JUL-10-2009 - Chris Evans 1-916-410-7677\n");
	} else {
	if ((out = fopen(argv[2]+".asm", "wb")) == NULL) {
	   fprintf(stderr, "Cant open output.\n");
	   return 1; }
	fprintf(out, "; ===========================================================================\n");
	fprintf(out, "; %s.asm - \n",argv[1]);
	fprintf(out, "; -------------\n");
	fprintf(out, ";\n");
	fprintf(out, ";-[version]-+-[date              ]-+-[notes                                ]=\n");
	fprintf(out, ";\n");
	fprintf(out, "; ===========================================================================\n");
	fprintf(out, "EXE     EQU 0       ; 1 = program.EXE\n");
fprintf(out, "IF (EXE)
fprintf(out, "_stack  segment word stack 'STACK'\n");
fprintf(out, "        db      0100h DUP (05h)\n");
fprintf(out, "_stack  ends\n");
fprintf(out, "\n");
fprintf(out, "data    segment    ; data things here \n");
fprintf(out, "        include \nxdos\src\tbx_data.inc\n");
fprintf(out, "data    ends\n");
fprintf(out, "ENDIF\n");
fprintf(out, "\n");
fprintf(out, "code    segment byte public\n");
fprintf(out, "IF (EXE)\n");
fprintf(out, "        assume cs:code, ds:data, es:data, ss:_stack\n");
fprintf(out, "ELSE\n");
fprintf(out, "        assume  cs:code, ds:code\n");
fprintf(out, "ENDIF\n");
fprintf(out, "\n");
fprintf(out, "         org     100h\n");
fprintf(out, "\n");
fprintf(out, "program  proc    far\n");
fprintf(out, "\n");
fprintf(out, "start:   jmp     initialize\n");
fprintf(out, "progname db '%s',0\n",argv[1]);
fprintf(out, "\n");
fprintf(out, "IF (EXE)\n");
fprintf(out, "        include \nxdos\src\tbx_code.inc\n");
fprintf(out, "ELSE\n");
fprintf(out, "        include toolbox.asm\n");
fprintf(out, "ENDIF\n");
fprintf(out, "\n");
fprintf(out, "initialize:\n");
fprintf(out, "IF (EXE)\n");
fprintf(out, "         mov  ax, data    ; Establish data and extra segment addressability\n");
fprintf(out, "         mov  ds, ax      ; aka  setup the stack n' data segments\n");
fprintf(out, "         assume 	ds:data\n");
fprintf(out, "         mov  es, ax\n");
fprintf(out, "         assume  es:data\n");
fprintf(out, "ELSE\n"); 
fprintf(out, "         ;jmp short kmain\n");
fprintf(out, "         push cs\n");
fprintf(out, "         pop  es\n");
fprintf(out, "         mov  bx, ProgramEnd\n");
fprintf(out, "         call resize\n");
fprintf(out, "         jc   kexitptr\n");
fprintf(out, "ENDIF\n");
fprintf(out, "kmain:\n");
fprintf(out, "         call cs:main\n");
fprintf(out, "kexitptr:\n");
fprintf(out, "         call dosexit\n");
fprintf(out, "         int 20h\n");
fprintf(out, "\n");
fprintf(out, "program    endp\n");
fprintf(out, "; ===========================================================================\n");
fprintf(out, "main       proc far\n");
fprintf(out, "; ===========================================================================\n");
fprintf(out, "           include %s.inc\n",argv[1]);
fprintf(out, "           ret\n");
fprintf(out, "main       endp\n");
fprintf(out, "\n");
fprintf(out, "kdata:\n");
fprintf(out, "; --- Your data goes here :) ------------------------------------------------\n");
fprintf(out, "\n");
fprintf(out, "code       ends\n");
fprintf(out, "\n");
fprintf(out, "ProgramEnd EQU $-01h\n");
fprintf(out, "\n");
fprintf(out, "	   end start ;  \n");
        fflush(out);
	fclose(out);
	}
  return 0;
}

