@echo off

if "%1"=="" (
  echo Usage: cmt_lockfile.bat file
)

SETLOCAL ENABLEDELAYEDEXPANSION
if not "%1"=="" (

  dir %~dp1 /a:d >nul 2>&1
  if errorlevel 1 (
    echo Giving up on "%~1"
  ) else (

    fs examine -path %~dp1 >nul 2>&1
    if not errorlevel 1 (
      set fstype=afs
    )

:lock
    if "!fstype!"=="afs" (
      fs flush -path %~dp1
    )
    mkdir %1 2>nul
    if errorlevel 1 (
      ping -n 9 localhost >nul
      goto lock
    )

  )
)
ENDLOCAL
