This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
/
patch-config.sh
executable file
·60 lines (50 loc) · 2 KB
/
patch-config.sh
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
#!/usr/bin/env bash
OLD_FILE="${1}"
NEW_FILE="${2}"
# The path to where the "patch instructions" are
PATCH_FILE="${3}"
# Note: set_kernel_config and unset_kernel_config are courtesy of @sakaki- on Github
# https://github.com/sakaki-/bcm2711-kernel-bis/blob/master/conform_config.sh.
# Slight modifications have been made to fit this context
set_kernel_config() {
# flag as $1, value to set as $2, config file as $3
local TGT="CONFIG_${1#CONFIG_}"
local REP="${2//\//\\/}"
local FILE=${3}
if grep -q "${TGT}" ${FILE}; then
sed "s/^\(${TGT}=.*\|# ${TGT} is not set\)/${TGT}=${REP}/" ${FILE} > ${FILE}.replaced
mv ${FILE}.replaced ${FILE}
else
echo "${TGT}=${REP}" >> ${FILE}
fi
}
unset_kernel_config() {
# unsets flag with the value of $1, config file as $2
local TGT="CONFIG_${1#CONFIG_}"
local FILE=${3}
sed "s/^${TGT}=.*/# ${TGT} is not set/" ${NEW_CONFIG_FILE} > ${NEW_CONFIG_FILE}.replaced
mv ${NEW_CONFIG_FILE}.replaced ${NEW_CONFIG_FILE}
}
patch_file() {
# patches a config file $1 according to the recipe of $PATCH_FILE
config_file=$1
echo "Patching ${config_file}..."
for line in $(cat ${PATCH_FILE} | grep -v "#"); do
# From https://stackoverflow.com/questions/10638538/split-string-with-bash-with-symbol
config_key=${line%=*}
config_value=${line#*=}
echo " Applying: ${config_key}=${config_value}"
if [[ ${config_value} == "n" ]]; then
unset_kernel_config ${config_key} ${config_file}
else
set_kernel_config ${config_key} ${config_value} ${config_file}
fi
done
}
# Copy the old config file to the new (overwrite if present), and patch the new one in-place
cp ${OLD_FILE} ${NEW_FILE}
# Add an extra newline to the upstream file if it hasn't got it
# From https://backreference.org/2010/05/23/sanitizing-files-with-no-trailing-newline/
tail -c1 "${NEW_FILE}" | read -r _ || echo >> "${NEW_FILE}"
# Apply patches to the new file
patch_file ${NEW_FILE}