summaryrefslogtreecommitdiff
path: root/win32/ifchange.bat
blob: f3fc9ea37c62030b0d49dfca229b1023866fcc69 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
@echo off
:: usage: ifchange target temporary

@setlocal EnableExtensions DisableDelayedExpansion || exit /b -1

:: @set PROMPT=$T:$S
for %%I in (%0) do set progname=%%~nI
set timestamp=
set keepsuffix=
set empty=
set color=auto
:optloop
set optarg=
:optnext
for %%I in (%1) do set opt=%%~I
    if not "%opt:~0,2%" == "--" (
        if not "%optarg%" == "" (
            call set %optarg%=%%opt%%
            shift
            goto :optloop
        )
        goto :optend
    )
    if "%opt%" == "--" (
        shift
        goto :optend
    )
    if "%opt%" == "--timestamp" (
        set timestamp=.
        set optarg=timestamp
        shift
        goto :optnext
    )
    if "%opt:~0,12%" == "--timestamp=" (
        set timestamp=%opt:~12%
        shift
        goto :optloop
    )
    if "%opt%" == "--keep" (
        set keepsuffix=.old
        set optarg=keep
        shift
        goto :optnext
    )
    if "%opt:~0,7%" == "--keep=" (
        set keepsuffix=%opt:~7%
        shift
        goto :optloop
    )
    if "%opt%" == "--empty" (
        set empty=yes
        shift
        goto :optloop
    )
    if "%opt%" == "--color" (
        set color=always
        set optarg=color
        shift
        goto :optnext
    )
    if "%opt:~0,8%" == "--color=" (
        set color=%opt:~8%
        shift
        goto :optloop
    )
    if "%opt%" == "--debug" (
        shift
        echo on
        goto :optloop
    )
    if "%opt%" == "--help" (
        call :help
        exit /b
    )
    echo %progname%: unknown option: %1 1>&2
    exit /b 1
:optend

if "%2" == "" (
    call :help 1>&2
    exit /b 1
)

set dest=%1
set src=%2
set dest=%dest:/=\%
set src=%src:/=\%

if not "%src%" == "-" goto :srcfile
    if not "%TMPDIR%" == "" (
        set src=%TMPDIR%\ifchange%RANDOM%.tmp
    ) else if not "%TEMP%" == "" (
        set src=%TEMP%\ifchange%RANDOM%.tmp
    ) else if not "%TMP%" == "" (
        set src=%TMP%\ifchange%RANDOM%.tmp
    ) else (
        set src=.\ifchange%RANDOM%.tmp
    )
    findstr -r -c:"^" > "%src%"
:srcfile

if exist %dest% (
    if not exist %src% goto :nt_unchanged1
    if not "%empty%" == "" for %%I in (%src%) do if %%~zI == 0 goto :nt_unchanged
    fc.exe %dest% %src% > nul && (
      :nt_unchanged
	del %src%
      :nt_unchanged1
	for %%I in (%1) do echo %%~I unchanged
	goto :nt_end
    )
)
for %%I in (%1) do echo %%~I updated
del /f %dest% 2> nul
copy %src% %dest% > nul
del %src%

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

:help
    for %%I in (
        "usage: %progname% [options] target new-file"
        "options:"
        "   --timestamp[=file] touch timestamp file. (default: prefixed with '.time')"
        "                      under the directory of the target)"
        "   --keep[=suffix]    keep old file with suffix. (default: '.old')"
        "   --empty            assume unchanged if the new file is empty."
        "   --color[=always|auto|never] colorize output."
    ) do echo.%%~I
    goto :eof

:end