-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmaybe_update
55 lines (44 loc) · 1.32 KB
/
maybe_update
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
#!/bin/bash
# First parameter is file name.
# Second parameter is desired content.
decho () {
if [[ "${debug:-0}" == "1" ]]; then
echo "$@"
return
fi
}
updatefile () {
local FILENAME
local DIRNAME
local FILEALREADYMATCHING
local CURRENTFILECONTENT
if [[ $# -ne 2 ]]; then
decho returning #echo if debug=1
return 99
fi
FILENAME=$(basename "$1")
DIRNAME=$(dirname "${FILENAME}")
FILECONTENT="$2"
mkdir -p "${DIRNAME}"
if [[ -d "${DIRNAME}" ]] ; then
FILEALREADYMATCHING=true
if ! [[ -f "${FILENAME}" ]] ; then
FILEALREADYMATCHING=false
decho "file doesn't exist" #echo if debug=1
else
CURRENTFILECONTENT="$(IFS= cat "${FILENAME}")X"
CURRENTFILECONTENT=${CURRENTFILECONTENT%?}
decho "file exists" #echo if debug=1
if [[ "${CURRENTFILECONTENT}" != "${FILECONTENT}" ]] ; then
decho "content is different" #echo if debug=1
FILEALREADYMATCHING=false
fi
fi
if [[ "${FILEALREADYMATCHING}" != "true" ]] ; then
decho "file doesnt match, making it match now" #echo if debug=1
decho "file is ${FILENAME} by the way" #echo if debug=1
printf '%s' "$2" > "${FILENAME}"
fi
fi
}
updatefile "$1" "$2"