@echo off
@REM  TODO: simplify this logic by updating install log to allow easier extraction
@REM  TODO: also fix case of multiple FreeDOS installations! (easily done by above)

if not exist %cdrom%\FREEDOS\FDOS\BIN\find.exe goto error

rem This file detects the FreeDOS installation directory to be able
rem to create sample \fdconfig.sys and installdir\fdauto.bat
rem Drive A: better be writeable! (it is, in fact, with MEMDISK, on cdrom)

rem Doesn't a utility exist which can replace variables by their contents???
rem Then all we have to do is:           copy x:\sample.sys c:\fdconfig.sys; patch.exe c:\fdconfig.sys destvar %destvar%
rem and copy x:\sample.bt %destvar%\fdauto.bat ; patch.exe %destvar%\fdauto.bat destvar %destvar%
rem what this patch.exe does is accept 3 parameters: file in which to replace strings, string variable to replace,
rem and contents to be inserted instead of string variable. eg c:\dosdir.bat homepath c:\testuser -or- c:\dosdir.bat homepath %homebase%

set destvar=
if [%1]==[of] goto next
goto end

:next
SET destvar=%2
rem echo Destination directory has been set to %destvar%!
goto makeconf

:error
ECHO FIND.EXE not found! press a key to abort post-configuration
pause
EXIT

:makeconf
cls
if exist c:\fdconfig.sys echo Skipped updating C:\FDCONFIG.SYS
if exist c:\fdconfig.sys goto makeauto
echo Creating system configuration file . . .
echo Creating multimenu templates for C:\FDCONFIG.SYS

echo MENU 1 - Load FreeDOS and XMS-memory driver                         > c:\fdconfig.sys
echo MENU 2 - Load FreeDOS without drivers                              >> c:\fdconfig.sys
echo MENUDEFAULT=1,5                                                    >> c:\fdconfig.sys
echo 1?DEVICE=%destvar%\BIN\FDXXMS.SYS                                  >> c:\fdconfig.sys
echo 1?DOS=HIGH                                                         >> c:\fdconfig.sys
echo 12?SHELL=%destvar%\BIN\COMMAND.COM /P=%destvar%\fdauto.bat         >> c:\fdconfig.sys
echo !LASTDRIVE=Z                                                       >> c:\fdconfig.sys
echo ECHO Loading remaining configuration files . . .                   >> c:\fdconfig.sys

goto makeauto



:makeauto
if exist %destvar%\fdauto.bat echo Skipped updating %destvar%\FDAUTO.BAT
if exist %destvar%\fdauto.bat %destvar%\fdauto.bat
echo Creating system startup automation file [%destvar%\fdauto.bat]
echo @echo off                                                                          > %destvar%\fdauto.bat
echo @set lang=%lang%                                                                  >> %destvar%\fdauto.bat
echo @set path=C:\;%destvar%;%destvar%\bin                                             >> %destvar%\fdauto.bat
type %cdrom%\FREEDOS\INSTALL\autofill.txt>>%destvar%\fdauto.bat
echo echo BootFiles: C:\FDCONFIG.SYS, %destvar%\FDAUTO.BAT , %DESTVAR%\BIN\COMMAND.COM >> %destvar%\fdauto.bat
echo echo.                                                                             >> %destvar%\fdauto.bat
echo echo Welcome to FreeDOS                                                           >> %destvar%\fdauto.bat 
rem adding more files
cls
REM for easy adding to possible bootloader:
rem SYS CONFIG A: C:\FDBTSECT.BIN ; 
echo @echo Welcome to FreeDOS. Run SYS if you want to make harddisk bootable, and reboot to use FreeDOS > a:\temp2.bat
a:\temp2.bat



:end
rem output to temp1.bat is:  Directory of C:\DOS
rem So we do a trick whereby we execute 'Directory' of C:\DOS
rem but since we are DOS this ends up being DIRECTOR (aka this batch)
rem with %1 being 'of' and %2 being 'C:\DOS' or user's installed dir.
rem locate sets the current directory to where the file is found
rem dir / find displays the line that shows the current directory
rem then we exec the temp file which in turn calls us again but
rem this time %2 will be the path we believe the user installed fdos to.

rem Detecting sample config.sys in freedos install destination dir,
rem UPDATE THE FILESIZE NUMBER in line below (4749) if editing sample
rem before distribution!!!!!!!!!!!!!!!!!!!!!!!

%cdrom%\FREEDOS\FDOS\BIN\locate.com config.sys /S:4749! /O:&D /N /F:1 /G
dir | %cdrom%\FREEDOS\FDOS\BIN\find.exe "Directory of" > a:\temp1.bat
rem if not [%0]==[a:\director.bat] copy /y %0 a:\director.bat
if not exist a:\temp1.bat EXIT
cls
rem reset current directory to where DIRECTOR.BAT is
%cdrom%
cd \FREEDOS\INSTALL
a:\temp1.bat
