1@echo off
2:: usage: ifchange target temporary
3
4set timestamp=
5set keepsuffix=
6set empty=
7set color=auto
8:optloop
9for %%I in (%1) do set opt=%%~I
10if "%opt%" == "--timestamp" (
11    set timestamp=.
12    shift
13    goto :optloop
14) else if "%opt:~0,12%" == "--timestamp=" (
15    set timestamp=%opt:~12%
16    shift
17    goto :optloop
18) else if "%opt%" == "--keep" (
19    set keepsuffix=.old
20    shift
21    goto :optloop
22) else if "%opt:~0,7%" == "--keep=" (
23    set keepsuffix=%opt:~7%
24    shift
25    goto :optloop
26) else if "%opt%" == "--empty" (
27    set empty=yes
28    shift
29    goto :optloop
30) else if "%opt%" == "--color" (
31    set color=always
32    shift
33    goto :optloop
34) else if "%opt:~0,8%" == "--color=" (
35    set color=%opt:~8%
36    shift
37    goto :optloop
38) else if "%opt%" == "--debug" (
39    shift
40    echo on
41    goto :optloop
42)
43if "%opt%" == "" goto :end
44
45set dest=%1
46set src=%2
47set dest=%dest:/=\%
48set src=%src:/=\%
49
50goto :nt
51
52:unchange
53echo %1 unchanged.
54del %2
55goto :end
56
57:update
58echo %1 updated.
59:: if exist %1 del %1
60dir /b %2
61if "%keepsuffix%" != "" %1 %1%keepsuffix%
62copy %2 %1
63del %2
64goto :end
65
66:nt
67if exist %dest% (
68    if not exist %src% goto :nt_unchanged1
69    if "%empty%" == "" for %%I in (%src%) do if %%~zI == 0 goto :nt_unchanged
70    fc.exe %dest% %src% > nul && (
71      :nt_unchanged
72	del %src%
73      :nt_unchanged1
74	for %%I in (%1) do echo %%~I unchanged
75	goto :nt_end
76    )
77)
78for %%I in (%1) do echo %%~I updated
79copy %src% %dest% > nul
80del %src%
81
82:nt_end
83if "%timestamp%" == "" goto :end
84    if "%timestamp%" == "." (
85        for %%I in ("%dest%") do set timestamp=%%~dpI.time.%%~nxI
86    )
87    goto :end > "%timestamp%"
88:end
89