summaryrefslogtreecommitdiff
path: root/win32/ifchange.bat
blob: 9f6092747b51d0cf1a401fd47abb358d2332661d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@echo off
:: usage: ifchange target temporary

if "%1" == "" goto :end

set timestamp=
if "%1" == "--timestamp" (
    set timestamp=yes
    shift
)
set dest=%1
set src=%2
set dest=%dest:/=\%
set src=%src:/=\%
if not "%dest%" == "" if not "%dest%" == "%%dest:/=\%%" goto :nt

if not exist %2 goto :end

:: check if fc.exe works.
echo foo > conftst1.tmp
echo bar > conftst2.tmp
fc.exe conftst1.tmp conftst2.tmp > nul
if not errorlevel 1 goto :brokenfc
del conftst1.tmp > nul
del conftst2.tmp > nul

:: target does not exist or new file differs from it.
if not exist %1 goto :update
fc.exe %1 %2 > nul
if errorlevel 1 goto :update

:unchange
echo %1 unchanged.
del %2
goto :end

:brokenfc
del conftest1.tmp > nul
del conftest2.tmp > nul
echo FC.EXE does not work properly.
echo assuming %1 should be changed.

:update
echo %1 updated.
:: if exist %1 del %1
dir /b %2
copy %2 %1
del %2
goto :end

:nt
if not exist %src% goto :end
if exist %dest% (
    fc.exe %dest% %src% > nul && (
	echo %1 unchanged.
	del %src%
	goto :nt_end
    )
)
echo %1 updated.
copy %src% %dest% > nul
del %src%

:nt_end
if "%timestamp%" == "" goto :end
    for %%I in ("%dest%") do set timestamp=%%~dpI\.time.%%~nxI
    if not exist "%timestamp%" copy nul "%timestamp%" > nul
    goto :end >> "%timestamp%"
:end