@echo off
rem
rem	needs two args, e/7/a, dir of llibc?p.lib
rem

if "%2" == "" goto usage
if "%1" == "e" goto dosetup
if "%1" == "7" goto dosetup
if "%1" == "a" goto dosetup

:usage
echo usage: mkdll [e/7/a] [dir of llibc?p.lib]
goto done

:dosetup
set pldir=%2
set model=%1

:doemu
rem link dll
if exist crtdll7.dll goto dllok
echo * linking CRT DLL...
echo /noi /noe crtdef.lib, crtdll7.dll,/map,/nod phapi oldnames llibcap, crtdll.def >>__tmp.lnk
link /nologo @__tmp.lnk >nul
if not exist crtdll7.map goto linkerr
del crtdll7.map
del __tmp.lnk

:dllok
if not exist %pldir%\llibc%model%p.lib goto liberr

:libok
rem build import library
if exist crtdll.lib goto implibok
echo * building CRT DLL import library...
implib /nologo /noi crtdll.lib crtxdll.def >nul
if errorlevel 1 goto impliberr

:implibok
rem add stuff to import library base
if exist crtdll%model%.lib erase crtdll%model%.lib
rem extract modules from base lib
echo * extracting modules from %pldir%\llibc%model%p...
lib %pldir%\llibc%model%p.lib @crtlib%model%.ext >nul

rem build library build file
echo crtdll%model%.lib >crtlib%model%.hdr
echo +%pldir%\apisim.lib ; >crtlib%model%.ftr

copy crtlib%model%.hdr+crtlib%model%.ixt+crtlib%model%.ftr tmp.ixt >nul

echo * adding modules to CRT DLL import library
lib /nologo /noi @tmp.ixt >nul

echo * copying CRTDLL7.DLL to the PharLap bin directory...
if not exist ..\..\bin\run286.exe goto nocopy
copy crtdll7.dll ..\..\bin
if not exist ..\..\bin\crtdll7.dll goto nocopy
goto goon2
:nocopy
echo Sorry, COPY failed.  The BIN directory wasn't where expected.
:goon2
echo * cleaning up...
call rm%model%objs
del tmp.ixt
del crtlib%model%.hdr
del crtlib%model%.ftr
goto okdone

:okdone
echo Build completed successfully.  Make sure that CRTDLL7.DLL is copied to
echo directory on your PATH (drive:\run286\bin ?), and CRTDLL%model%.LIB is
echo copied to a directory on your library path (drive:\run286\msc700\lib ?).
goto done

:linkerr
echo Error linking CRTDLL7.DLL.  Make sure LINK.EXE is on the path.
goto done

:liberr
echo Error finding %pldir%\llibc%model%p.lib.  Make sure that the
echo X:\run286\msc700 library directory is correct.
goto done

:impliberr
echo Error building import library from crtdll7.dll.  Make sure IMPLIB.EXE
echo is on the path.
goto done

:done
set model=
set pldir=
