@echo off
REM #     cfgdisk-ata.cmd  - script to configure ATA disks
REM #
set logfile=%DeploymentStore%\cfgdiska.log
set wce=0
REM set dev=/dev/hda
set ADDLOG=call addLogRecord
REM set ADDLOG=echo
set DPTMP=dp.tmp
set DPOUT=dp.out
set RET=5

echo cfgdiska >%logfile%
REM # get list of ATA disks
echo  list disk     > %DPTMP%
echo  list volume  >> %DPTMP%
diskpart /s %DPTMP%  > %DPOUT%
findstr /C:"Disk " %DPOUT% |findstr /V /C:"Disk #" >%DPTMP%
type %DPTMP%  >>%logfile%
FOR /F "tokens=1,2 delims= " %%a in ('type %DPTMP%') DO (
    echo %%a %%b >NUL
)

FOR %%x in (a b c d e f g h i j k l m n o p q r s t u v w x y z) DO CALL :SEARCHDRIVES %%x

del %DPTMP% %DPOUT%
echo cfgdisk-ata(wce=%wce%) done, ret=%RET%
exit /B %RET%

:SEARCHDRIVES
   set dev=/dev/hd%1
   hdparm -I %dev% >NUL 2>%DPOUT%
   findstr /C:"No such file" %DPOUT% >NUL
   IF %ERRORLEVEL% == 0 goto :EOF
   REM Really want to end the loop if No such file
   findstr failed %DPOUT%  >NUL
   IF errorlevel 1 ( 
      REM # configure the disks to turn off write cache
      hdparm -I %dev% >>%logfile%
      echo hdparm -W%wce% %dev% >>%logfile% 
      hdparm -W%wce% %dev%   >>%logfile% 2>&1
      if errorlevel 1 (
        set RET=%ERRORLEVEL%
        %ADDLOG% CFGDISK ERROR "Error %ERRORLEVEL%: hdparm -W%wce% %dev%"
        goto :EOF
      )
      set RET=0
      %ADDLOG% CFGDISK INFO "Success: hdparm -W%wce% %dev%"
   )
   goto :EOF

